Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to swipe from an TabBarView content area to an adjacent PageView page?

Tags:

flutter

I'd like to place a tabs inside a horizontal PageView and be able to swipe out of the tabs. Within the content area, I can swipe into the tabs from a page, but not out of the tabs to another page. If I swipe on the TabBar, then I can get out of the tabs and go to the adjacent page.

Is there a way to allow me to swipe from a TabView content area and have it move to the adjacent PageView page?

Here is my test code:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return PageView(
      children: <Widget>[
        Scaffold(
          appBar: AppBar(title: Text('Page 1'),),
          body: Center(child: Text('hi'),),
        ),
        DefaultTabController(
          length: 3,
          child: Scaffold(
            appBar: AppBar(
              title: Text(widget.title),
              bottom: TabBar(
                tabs: [
                  Tab(icon: Icon(Icons.directions_car)),
                  Tab(icon: Icon(Icons.directions_transit)),
                  Tab(icon: Icon(Icons.directions_bike)),
                ],
              ),
            ),
            body: TabBarView(
              children: [
                Icon(Icons.directions_car),
                Icon(Icons.directions_transit),
                Icon(Icons.directions_bike),
              ],
            ),
          ),
        ),
        Scaffold(
          appBar: AppBar(title: Text('Page 3'),),
          body: Center(child: Text('bye'),),
        ),
      ],
    );
  }
}
like image 902
jibbers42 Avatar asked Jul 05 '19 18:07

jibbers42


1 Answers

Ok, I believe I got the idea of what you're looking for. For this, I suggest using something like a NotificationListener in your widget tree that will catch the OverscrollNotification and you can either scroll to left or right, depending of the overscroll side (less than 0 for left, over 0 for right).

I made it animate linearly (250 ms) for each side, but you can tweak it to your needs.

example

class Example extends StatefulWidget {
  Example({Key key}) : super(key: key);

  @override
  _ExampleState createState() => _ExampleState();
}

class _ExampleState extends State<Example> with SingleTickerProviderStateMixin {
  TabController _tabController;
  final PageController _pageController = PageController();

  @override
  void initState() {
    super.initState();
    _tabController = TabController(vsync: this, length: 3);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Example'),
      ),
      body: PageView(
        controller: _pageController,
        children: <Widget>[
          Center(child: Text('Page 1')),
          Column(
            children: <Widget>[
              TabPageSelector(controller: _tabController),
              Text('Page 2'),
              NotificationListener(
                onNotification: (overscroll) {
                  if (overscroll is OverscrollNotification && overscroll.overscroll != 0 && overscroll.dragDetails != null) {
                    _pageController.animateToPage(overscroll.overscroll < 0 ? 0 : 2,
                        curve: Curves.ease, duration: Duration(milliseconds: 250));
                  }
                  return true;
                },
                child: Expanded(
                  child: TabBarView(
                    controller: _tabController,
                    children: <Widget>[
                      Center(child: Text('Tab 1')),
                      Center(child: Text('Tab 2')),
                      Center(child: Text('Tab 3')),
                    ],
                  ),
                ),
              ),
            ],
          ),
          Center(child: Text('Page 3')),
        ],
      ),
    );
  }
}
like image 185
Miguel Ruivo Avatar answered Nov 08 '22 11:11

Miguel Ruivo