forked from pedromassango/bottom_navy_bar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bottom_navy_bar.dart
247 lines (217 loc) · 7.81 KB
/
bottom_navy_bar.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
library bottom_navy_bar;
import 'package:flutter/material.dart';
/// A beautiful and animated bottom navigation that paints a rounded shape
/// around its [items] to provide a wonderful look.
///
/// Update [selectedIndex] to change the selected item.
/// [selectedIndex] is required and must not be null.
class BottomNavyBar extends StatelessWidget {
BottomNavyBar({
Key? key,
this.selectedIndex = 0,
this.showElevation = true,
this.iconSize = 24,
this.backgroundColor,
this.shadowColor = Colors.black12,
this.itemCornerRadius = 50,
this.containerHeight = 56,
this.blurRadius = 2,
this.spreadRadius = 0,
this.borderRadius,
this.shadowOffset = Offset.zero,
this.itemPadding = const EdgeInsets.symmetric(horizontal: 4),
this.animationDuration = const Duration(milliseconds: 270),
this.mainAxisAlignment = MainAxisAlignment.spaceBetween,
required this.items,
required this.onItemSelected,
this.curve = Curves.linear,
}) : assert(items.length >= 2 && items.length <= 5),
super(key: key);
/// The selected item is index. Changing this property will change and animate
/// the item being selected. Defaults to zero.
final int selectedIndex;
/// The icon size of all items. Defaults to 24.
final double iconSize;
/// The background color of the navigation bar. It defaults to
/// [ThemeData.bottomAppBarColor] if not provided.
final Color? backgroundColor;
/// Defines the shadow color of the navigation bar. Defaults to [Colors.black12].
final Color shadowColor;
/// Whether this navigation bar should show a elevation. Defaults to true.
final bool showElevation;
/// Use this to change the item's animation duration. Defaults to 270ms.
final Duration animationDuration;
/// Defines the appearance of the buttons that are displayed in the bottom
/// navigation bar. This should have at least two items and five at most.
final List<BottomNavyBarItem> items;
/// A callback that will be called when a item is pressed.
final ValueChanged<int> onItemSelected;
/// Defines the alignment of the items.
/// Defaults to [MainAxisAlignment.spaceBetween].
final MainAxisAlignment mainAxisAlignment;
/// The [items] corner radius, if not set, it defaults to 50.
final double itemCornerRadius;
/// Defines the bottom navigation bar height. Defaults to 56.
final double containerHeight;
/// Used to configure the blurRadius of the [BoxShadow]. Defaults to 2.
final double blurRadius;
/// Used to configure the spreadRadius of the [BoxShadow]. Defaults to 0.
final double spreadRadius;
/// Used to configure the offset of the [BoxShadow]. Defaults to null.
final Offset shadowOffset;
/// Used to configure the borderRadius of the [BottomNavyBar]. Defaults to null.
final BorderRadiusGeometry? borderRadius;
/// Used to configure the padding of the [BottomNavyBarItem] [items].
/// Defaults to EdgeInsets.symmetric(horizontal: 4).
final EdgeInsets itemPadding;
/// Used to configure the animation curve. Defaults to [Curves.linear].
final Curve curve;
@override
Widget build(BuildContext context) {
final bgColor = backgroundColor ?? Theme.of(context).bottomAppBarColor;
return Container(
decoration: BoxDecoration(
color: bgColor,
boxShadow: [
if (showElevation)
BoxShadow(
color: shadowColor,
blurRadius: blurRadius,
spreadRadius: spreadRadius,
offset: shadowOffset,
),
],
borderRadius: borderRadius,
),
child: SafeArea(
child: Container(
width: double.infinity,
height: containerHeight,
padding: const EdgeInsets.symmetric(vertical: 6, horizontal: 8),
child: Row(
mainAxisAlignment: mainAxisAlignment,
children: items.map((item) {
var index = items.indexOf(item);
return GestureDetector(
onTap: () => onItemSelected(index),
child: _ItemWidget(
item: item,
iconSize: iconSize,
isSelected: index == selectedIndex,
backgroundColor: bgColor,
itemCornerRadius: itemCornerRadius,
animationDuration: animationDuration,
itemPadding: itemPadding,
curve: curve,
),
);
}).toList(),
),
),
),
);
}
}
class _ItemWidget extends StatelessWidget {
final double iconSize;
final bool isSelected;
final BottomNavyBarItem item;
final Color backgroundColor;
final double itemCornerRadius;
final Duration animationDuration;
final EdgeInsets itemPadding;
final Curve curve;
const _ItemWidget({
Key? key,
required this.iconSize,
required this.isSelected,
required this.item,
required this.backgroundColor,
required this.itemCornerRadius,
required this.animationDuration,
required this.itemPadding,
this.curve = Curves.linear,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Semantics(
container: true,
selected: isSelected,
child: AnimatedContainer(
width: isSelected ? 130 : 50,
height: double.maxFinite,
duration: animationDuration,
curve: curve,
decoration: BoxDecoration(
color:
isSelected ? item.activeColor.withOpacity(0.2) : backgroundColor,
borderRadius: BorderRadius.circular(itemCornerRadius),
),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
physics: NeverScrollableScrollPhysics(),
child: Container(
width: isSelected ? 130 : 50,
padding: EdgeInsets.symmetric(horizontal: 8),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
IconTheme(
data: IconThemeData(
size: iconSize,
color: isSelected
? item.activeColor.withOpacity(1)
: item.inactiveColor == null
? item.activeColor
: item.inactiveColor,
),
child: item.icon,
),
if (isSelected)
Expanded(
child: Container(
padding: itemPadding,
child: DefaultTextStyle.merge(
style: TextStyle(
color: item.activeColor,
fontWeight: FontWeight.bold,
),
maxLines: 1,
textAlign: item.textAlign,
child: item.title,
),
),
),
],
),
),
),
),
);
}
}
/// The [BottomNavyBar.items] definition.
class BottomNavyBarItem {
BottomNavyBarItem({
required this.icon,
required this.title,
this.activeColor = Colors.blue,
this.textAlign,
this.inactiveColor,
});
/// Defines this item's icon which is placed in the right side of the [title].
final Widget icon;
/// Defines this item's title which placed in the left side of the [icon].
final Widget title;
/// The [icon] and [title] color defined when this item is selected. Defaults
/// to [Colors.blue].
final Color activeColor;
/// The [icon] and [title] color defined when this item is not selected.
final Color? inactiveColor;
/// The alignment for the [title].
///
/// This will take effect only if [title] it a [Text] widget.
final TextAlign? textAlign;
}