Skip to content

Commit

Permalink
Merge pull request #27 from hacktons/f-support-start-index
Browse files Browse the repository at this point in the history
add initial start index
  • Loading branch information
avenwu committed Feb 12, 2020
2 parents fcd3d20 + 9621829 commit ec7e110
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions example/lib/default_appbar_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class _State extends State<DefaultAppBarDemo> {
bottomNavigationBar: _badge == null
? ConvexAppBar(
items: _tabItems.value,
initialActiveIndex: 2,
style: _style.value,
curve: _curve.value,
backgroundColor: _babColor,
Expand Down
17 changes: 14 additions & 3 deletions lib/src/bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class ConvexAppBar extends StatefulWidget {
/// ![](https://github.com/hacktons/convex_bottom_bar/raw/master/doc/appbar-gradient.gif)
final Gradient gradient;

/// The initial active index, default as 0 if not provided;
final int initialActiveIndex;

/// Tab count
final int count;

Expand Down Expand Up @@ -125,6 +128,7 @@ class ConvexAppBar extends StatefulWidget {
ConvexAppBar({
Key key,
@required List<TabItem> items,
int initialActiveIndex,
GestureTapIndexCallback onTap,
Color color,
Color activeColor,
Expand All @@ -150,6 +154,7 @@ class ConvexAppBar extends StatefulWidget {
onTap: onTap,
backgroundColor: backgroundColor ?? Colors.blue,
count: items.length,
initialActiveIndex: initialActiveIndex,
gradient: gradient,
height: height,
curveSize: curveSize,
Expand All @@ -165,6 +170,7 @@ class ConvexAppBar extends StatefulWidget {
Key key,
@required this.itemBuilder,
@required this.count,
this.initialActiveIndex,
this.onTap,
this.backgroundColor,
this.gradient,
Expand All @@ -177,6 +183,8 @@ class ConvexAppBar extends StatefulWidget {
this.chipBuilder,
}) : assert(top == null || top <= 0, 'top should be negative'),
assert(itemBuilder != null, 'provide custom buidler'),
assert(initialActiveIndex == null || initialActiveIndex < count,
'initial index should < $count'),
super(key: key);

/// Construct a new appbar with badge
Expand Down Expand Up @@ -209,6 +217,7 @@ class ConvexAppBar extends StatefulWidget {
double badgeBorderRadius,
// parameter for appbar
List<TabItem> items,
int initialActiveIndex,
GestureTapIndexCallback onTap,
Color color,
Color activeColor,
Expand All @@ -232,8 +241,9 @@ class ConvexAppBar extends StatefulWidget {
);
}
return ConvexAppBar(
items: items,
key: key,
items: items,
initialActiveIndex: initialActiveIndex,
onTap: onTap,
color: color,
activeColor: activeColor,
Expand Down Expand Up @@ -267,12 +277,13 @@ abstract class DelegateBuilder {
}

class _State extends State<ConvexAppBar> with TickerProviderStateMixin {
int _currentSelectedIndex = 0;
int _currentSelectedIndex;
Animation<double> _animation;
AnimationController _controller;

@override
void initState() {
_currentSelectedIndex = widget.initialActiveIndex ?? 0;
if (!isFixed()) {
_initAnimation();
}
Expand All @@ -283,7 +294,7 @@ class _State extends State<ConvexAppBar> with TickerProviderStateMixin {
if (from != null && (from == to)) {
return _animation;
}
from ??= 0;
from ??= widget.initialActiveIndex ?? 0;
to ??= from;
var lower = (2 * from + 1) / (2 * widget.count);
var upper = (2 * to + 1) / (2 * widget.count);
Expand Down
16 changes: 16 additions & 0 deletions test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void main() {
TabItem(title: 'Tab B', icon: Icons.near_me),
TabItem(title: 'Tab C', icon: Icons.web),
],
initialActiveIndex: 2,
style: TabStyle.fixed,
)));
final tabAText = find.text('Tab A');
Expand Down Expand Up @@ -176,6 +177,21 @@ void main() {
await tester.tap(find.text('TAB 1'));
await tester.pumpAndSettle(Duration(milliseconds: 300));
});

test('test invalid builder', () {
try {
ConvexAppBar.builder(
itemBuilder: null,
count: 3,
top: -20,
onTap: (i) {
assert(i == 1);
},
);
} catch (e) {
assert(true, e.toString().contains('provide custom buidler'));
}
});
}

class Builder extends DelegateBuilder {
Expand Down

0 comments on commit ec7e110

Please sign in to comment.