Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class _MyAppState extends State<MyApp> with TickerProviderStateMixin {
- static int _noOfTabs = 0;
- static List<Widget> _tabList;
- static List<Widget> _tabViewList;
- TabController _controller;
- @override
- void initState() {
- super.initState();
- // buttonPressed();
- }
- @override
- void dispose() {
- _controller.dispose();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- home: Scaffold(
- appBar: new AppBar(
- title: Text('TabBar Preview'),
- bottom: (_noOfTabs > 0)
- ? PreferredSize(
- child: Draggable(
- child: TabBar(
- controller: _controller,
- isScrollable: true,
- tabs: _tabList),
- feedback: Container(),
- onDragStarted: () {
- print("stareted");
- },
- onDragCompleted: () {
- print("completed");
- },
- onDragEnd: (dragDetails){
- print(dragDetails.velocity.pixelsPerSecond.direction);
- print(dragDetails.velocity.pixelsPerSecond.distance);
- if(dragDetails.velocity.pixelsPerSecond.distance > 0.5){
- int index = _controller.index;
- if(dragDetails.velocity.pixelsPerSecond.direction < 0 && index <_tabList.length+1){ //Right, figure out how much better for you
- Widget tabViewTemp = _tabViewList[index+1];
- _tabViewList[index+1] = _tabViewList[index];
- _tabViewList[index] = tabViewTemp;
- Widget tabText = _tabList[index+1];
- _tabList[index+1] = _tabList[index];
- _tabList[index] = tabText;
- } else if(index > 0){ //left
- Widget tabViewTemp = _tabViewList[index-1];
- _tabViewList[index-1] = _tabViewList[index];
- _tabViewList[index] = tabViewTemp;
- Widget tabText = _tabList[index-1];
- _tabList[index-1] = _tabList[index];
- _tabList[index] = tabText;
- }
- _controller = TabController(length: _noOfTabs, vsync: this);
- _controller.animateTo(index+1);
- setState(() {
- });
- }
- },
- ),
- preferredSize: Size(double.infinity, 24.0),
- )
- : null,
- ),
- body: (_noOfTabs > 0)
- ? TabBarView(
- controller: _controller,
- children: _tabViewList,
- )
- : Center(
- child: Text('No tabs'),
- ),
- floatingActionButton: FloatingActionButton(
- child: Icon(Icons.add), onPressed: buttonPressed),
- ),
- );
- }
- void buttonPressed() {
- setState(() {
- _noOfTabs++;
- });
- _tabList = new List<Widget>();
- _tabViewList = new List<Widget>();
- for (var i = 0; i < _noOfTabs; i++) {
- _tabList.add(Tab(text: 'Tab ' + (i + 1).toString()));
- _tabViewList.add(Center(child: Text('Tab ' + (i + 1).toString())));
- }
- _controller = TabController(length: _noOfTabs, vsync: this);
- _controller.animateTo(_noOfTabs - 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement