Skip to content

Instantly share code, notes, and snippets.

@collinjackson
Last active October 21, 2022 10:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save collinjackson/61b928f11bd6598cd3ab62e80a7c618d to your computer and use it in GitHub Desktop.
Save collinjackson/61b928f11bd6598cd3ab62e80a7c618d to your computer and use it in GitHub Desktop.
Workaround for flutter/flutter#10969
import 'package:flutter/material.dart';
void main() {
runApp(new MaterialApp(
home: new MyHomePage(),
));
}
class TabbedPage extends StatefulWidget {
TabbedPage({ Key key, this.pageIndex, this.tabCount }) : super(key: key);
final int pageIndex;
final int tabCount;
_TabbedPageState createState() => new _TabbedPageState();
}
class _TabbedPageState extends State<TabbedPage> with TickerProviderStateMixin {
TabController _tabController;
@override
void initState() {
_tabController = new TabController(
length: widget.tabCount,
vsync: this,
initialIndex: PageStorage.of(context).readState(context) ?? 0,
)..addListener(() {
PageStorage.of(context).writeState(context, _tabController.index);
});
super.initState();
}
Widget build(BuildContext context) {
return new Column(
children: <Widget>[
new Container(
color: Theme.of(context).primaryColor,
child: new TabBar(
key: new PageStorageKey<Type>(TabBar),
controller: _tabController,
isScrollable: true,
tabs: new List<Tab>.generate(widget.tabCount, (int tabIndex) {
return new Tab(
text: 'Tab ${widget.pageIndex}-${tabIndex}',
);
}),
),
),
new Expanded(
child: new TabBarView(
key: new PageStorageKey<Type>(TabBarView),
controller: _tabController,
children: new List<Widget>.generate(widget.tabCount, (int tabIndex) {
return new Center(
child: new Text('View ${widget.pageIndex}-${tabIndex}'),
);
}),
),
),
],
);
}
}
class MyHomePage extends StatefulWidget {
_MyHomePageState createState() => new _MyHomePageState();
}
const List<int> tabCounts = const <int>[13, 17, 21];
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
PageController _controller = new PageController();
Widget build(BuildContext context) {
return new PageView(
controller: _controller,
children: new List<Widget>.generate(tabCounts.length, (int index) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Page $index'),
),
body: new TabbedPage(
key: new PageStorageKey<int>(index),
pageIndex: index,
tabCount: tabCounts[index],
),
);
}),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment