Skip to content

Commit

Permalink
add initial start index
Browse files Browse the repository at this point in the history
  • Loading branch information
avenwu committed Feb 12, 2020
1 parent fcd3d20 commit fada4ed
Show file tree
Hide file tree
Showing 2 changed files with 15 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

0 comments on commit fada4ed

Please sign in to comment.