Skip to content

Commit

Permalink
fix DefaultTabController compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
avenwu committed Feb 17, 2020
1 parent c1b3c01 commit 01147f2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 42 deletions.
51 changes: 22 additions & 29 deletions example/lib/custom_appbar_sample.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,31 @@ class _State extends State<CustomAppBarDemo>
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,
),
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion example/lib/default_appbar_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class _State extends State<DefaultAppBarDemo>
body: TabBarView(
controller: _tabController,
children: _tabItems.value
.map((i) => i.title == 'Home'
.map((i) => i.title == 'Home' || i.title == 'Happy'
? ListView(
children: options,
)
Expand Down
23 changes: 11 additions & 12 deletions lib/src/bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,6 @@ class _State extends State<ConvexAppBar> 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();
}
Expand Down Expand Up @@ -360,12 +348,23 @@ class _State extends State<ConvexAppBar> 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
Expand Down

0 comments on commit 01147f2

Please sign in to comment.