diff --git a/example/lib/custom_appbar_sample.dart b/example/lib/custom_appbar_sample.dart index c5366aa..1429672 100644 --- a/example/lib/custom_appbar_sample.dart +++ b/example/lib/custom_appbar_sample.dart @@ -41,38 +41,31 @@ class _State extends State Color(0xFF607D8B), ]; Color _tabBackgroundColor = paletteColors[5]; - TabController _tabController; - - @override - void initState() { - super.initState(); - _tabController = - TabController(initialIndex: 2, length: items.length, vsync: this); - } @override Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar(title: const Text('Custom ConvexAppBar')), - body: TabBarView( - controller: _tabController, - children: items - .map((i) => i.title == 'Discovery' - ? paletteBody() - : Center( - child: Text( - '<\t\t${i.title}\t\t>', - style: Theme.of(context).textTheme.display1, - ))) - .toList(growable: false), - ), - bottomNavigationBar: ConvexAppBar.builder( - itemBuilder: _CustomBuilder(items, _tabBackgroundColor), - count: items.length, - tabController: _tabController, - initialActiveIndex: 1, - backgroundColor: _tabBackgroundColor, - style: TabStyle.fixed, + return DefaultTabController( + length: items.length, + initialIndex: 2, + child: Scaffold( + appBar: AppBar(title: const Text('Custom ConvexAppBar')), + body: TabBarView( + children: items + .map((i) => i.title == 'Discovery' + ? paletteBody() + : Center( + child: Text( + '<\t\t${i.title}\t\t>', + style: Theme.of(context).textTheme.display1, + ))) + .toList(growable: false), + ), + bottomNavigationBar: ConvexAppBar.builder( + itemBuilder: _CustomBuilder(items, _tabBackgroundColor), + count: items.length, + backgroundColor: _tabBackgroundColor, + style: TabStyle.fixed, + ), ), ); } diff --git a/example/lib/default_appbar_demo.dart b/example/lib/default_appbar_demo.dart index 1bca9ff..854fb3d 100644 --- a/example/lib/default_appbar_demo.dart +++ b/example/lib/default_appbar_demo.dart @@ -129,7 +129,7 @@ class _State extends State body: TabBarView( controller: _tabController, children: _tabItems.value - .map((i) => i.title == 'Home' + .map((i) => i.title == 'Home' || i.title == 'Happy' ? ListView( children: options, ) diff --git a/lib/src/bar.dart b/lib/src/bar.dart index 872b686..b3d2933 100644 --- a/lib/src/bar.dart +++ b/lib/src/bar.dart @@ -294,18 +294,6 @@ class _State extends State with TickerProviderStateMixin { @override void initState() { super.initState(); - _updateTabController(); - _currentIndex = widget.initialActiveIndex ?? _tabController?.index ?? 0; - - /// When both ConvexAppBar and TabController are configured with initial index, there can be conflict; - /// We use ConvexAppBar's value; - if (widget.initialActiveIndex != null && - _tabController != null && - widget.initialActiveIndex != _tabController.index) { - WidgetsBinding.instance.addPostFrameCallback((_) { - _tabController.index = _currentIndex; - }); - } if (!isFixed()) { _initAnimation(); } @@ -360,12 +348,23 @@ class _State extends State with TickerProviderStateMixin { _tabController?.removeListener(_handleTabControllerAnimationTick); _tabController = newController; _tabController?.addListener(_handleTabControllerAnimationTick); + _currentIndex = widget.initialActiveIndex ?? _tabController?.index ?? 0; } @override void didChangeDependencies() { super.didChangeDependencies(); _updateTabController(); + + /// When both ConvexAppBar and TabController are configured with initial index, there can be conflict; + /// We use ConvexAppBar's value; + if (widget.initialActiveIndex != null && + _tabController != null && + widget.initialActiveIndex != _tabController.index) { + WidgetsBinding.instance.addPostFrameCallback((_) { + _tabController.index = _currentIndex; + }); + } } @override