Advertisement
Guest User

dartTabDrag

a guest
Jul 4th, 2019
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.65 KB | None | 0 0
  1. class _MyAppState extends State<MyApp> with TickerProviderStateMixin {
  2.   static int _noOfTabs = 0;
  3.   static List<Widget> _tabList;
  4.   static List<Widget> _tabViewList;
  5.  
  6.   TabController _controller;
  7.  
  8.   @override
  9.   void initState() {
  10.     super.initState();
  11.    // buttonPressed();
  12.   }
  13.  
  14.   @override
  15.   void dispose() {
  16.     _controller.dispose();
  17.  
  18.     super.dispose();
  19.   }
  20.  
  21.   @override
  22.   Widget build(BuildContext context) {
  23.     return MaterialApp(
  24.       home: Scaffold(
  25.         appBar: new AppBar(
  26.           title: Text('TabBar Preview'),
  27.           bottom: (_noOfTabs > 0)
  28.               ? PreferredSize(
  29.                   child: Draggable(
  30.                     child: TabBar(
  31.                         controller: _controller,
  32.                         isScrollable: true,
  33.                         tabs: _tabList),
  34.                     feedback: Container(),
  35.                     onDragStarted: () {
  36.                       print("stareted");
  37.                     },
  38.                     onDragCompleted: () {
  39.                       print("completed");
  40.                     },
  41.                     onDragEnd: (dragDetails){
  42.  
  43.                       print(dragDetails.velocity.pixelsPerSecond.direction);
  44.                       print(dragDetails.velocity.pixelsPerSecond.distance);
  45.  
  46.  
  47.                       if(dragDetails.velocity.pixelsPerSecond.distance > 0.5){
  48.  
  49.                         int index = _controller.index;
  50.  
  51.                         if(dragDetails.velocity.pixelsPerSecond.direction < 0 && index <_tabList.length+1){ //Right, figure out how much better for you
  52.  
  53.                           Widget tabViewTemp = _tabViewList[index+1];
  54.                           _tabViewList[index+1] = _tabViewList[index];
  55.                           _tabViewList[index] = tabViewTemp;
  56.  
  57.                           Widget tabText = _tabList[index+1];
  58.                           _tabList[index+1] = _tabList[index];
  59.                           _tabList[index] = tabText;
  60.  
  61.                         } else if(index > 0){ //left
  62.  
  63.                           Widget tabViewTemp = _tabViewList[index-1];
  64.                           _tabViewList[index-1] = _tabViewList[index];
  65.                           _tabViewList[index] = tabViewTemp;
  66.  
  67.                           Widget tabText = _tabList[index-1];
  68.                           _tabList[index-1] = _tabList[index];
  69.                           _tabList[index] = tabText;
  70.  
  71.                         }
  72.  
  73.                           _controller = TabController(length: _noOfTabs, vsync: this);
  74.                           _controller.animateTo(index+1);
  75.  
  76.                           setState(() {
  77.  
  78.                           });
  79.  
  80.                       }
  81.                     },
  82.  
  83.                   ),
  84.                  preferredSize: Size(double.infinity, 24.0),
  85.                 )
  86.               : null,
  87.         ),
  88.         body:  (_noOfTabs > 0)
  89.               ? TabBarView(
  90.                   controller: _controller,
  91.                   children: _tabViewList,
  92.                 )
  93.               : Center(
  94.                   child: Text('No tabs'),
  95.                 ),
  96.  
  97.         floatingActionButton: FloatingActionButton(
  98.             child: Icon(Icons.add), onPressed: buttonPressed),
  99.       ),
  100.     );
  101.   }
  102.  
  103.   void buttonPressed() {
  104.     setState(() {
  105.       _noOfTabs++;
  106.     });
  107.  
  108.     _tabList = new List<Widget>();
  109.     _tabViewList = new List<Widget>();
  110.  
  111.     for (var i = 0; i < _noOfTabs; i++) {
  112.       _tabList.add(Tab(text: 'Tab ' + (i + 1).toString()));
  113.       _tabViewList.add(Center(child: Text('Tab ' + (i + 1).toString())));
  114.     }
  115.  
  116.     _controller = TabController(length: _noOfTabs, vsync: this);
  117.     _controller.animateTo(_noOfTabs - 1);
  118.   }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement