简体   繁体   中英

flutter PageView inside TabBarView: scrolling to next tab at the end of page

I have 3 tabs and each tab has a PageView inside. At the end of the PageView, I want to be able to scroll to the next tab.

Is there a way I can do TabBar scroll instead of PageView scroll if there's no more page to the direction? (only left or right scroll)

Here's the sample code. When I scroll to right at the last page of the 1st tab, I want to see the first page of the 2nd tab.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: ScrollableTabsDemo());
  }
}

class _Page {
  const _Page({ this.icon, this.text });
  final IconData icon;
  final String text;
}

const List<_Page> _allPages = <_Page>[
  _Page(icon: Icons.grade, text: 'TRIUMPH'),
  _Page(icon: Icons.playlist_add, text: 'NOTE'),
  _Page(icon: Icons.check_circle, text: 'SUCCESS'),
];

class ScrollableTabsDemo extends StatefulWidget {
  static const String routeName = '/material/scrollable-tabs';

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

class ScrollableTabsDemoState extends State<ScrollableTabsDemo> with SingleTickerProviderStateMixin {
  TabController _controller;

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

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final Color iconColor = Theme.of(context).accentColor;
    return Scaffold(
      appBar: AppBar(
        title: const Text('Scrollable tabs'),
        bottom: TabBar(
          controller: _controller,
          isScrollable: true,
          tabs: _allPages.map<Tab>((_Page page) {
            return Tab(text: page.text, icon: Icon(page.icon));
          }).toList(),
        ),
      ),
      body: TabBarView(
        controller: _controller,
        children: _allPages.map<Widget>((_Page page) {
          return SafeArea(
            top: false,
            bottom: false,
            child: PageView.builder(
              itemBuilder: (context, position)
              {
                return Container(child: Center(child: Text(position.toString())));
              },
              itemCount: 5,
            ),
          );
        }).toList(),
      ),
    );
  }
}

Add to the pageBuilder the onPageChange param. Then check if its the last page, if so, animate the tabController to the nextPage.


onPageChanged: (page) {
       if (page == _allPages.length &&
           (_controller.index + 1) < _controller.length) {
          _controller.animateTo(_controller.index + 1);
     }
 },
itemCount: _allPages.length + 1,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM