簡體   English   中英

是否可以從TabBarView內容區域滑動到相鄰的PageView頁面?

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

我想在水平PageView中放置標簽,並能夠從標簽中滑動。 在內容區域內,我可以從頁面滑入選項卡,但不能滑出選項卡到另一個頁面。 如果我在TabBar上滑動,則可以離開這些標簽並轉到相鄰的頁面。

有沒有一種方法可以讓我從TabView內容區域滑動,然后將其移至相鄰的PageView頁面?

這是我的測試代碼:

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'),),
        ),
      ],
    );
  }
}

Tabbar具有一個名為onTap的屬性。 在onTap()中,您可以獲取索引並處理綜合瀏覽量的內容。 例:

    TabBar(
          onTap: (int tabIndex) {
// you can perform any action here using index
       })

好的,我相信我已經知道您要尋找什么。 為此,我建議在小部件樹中使用類似於NotificationListener東西,它將捕獲OverscrollNotification並且您可以向左或向右滾動,具體取決於過度滾動的一側(左小於0,右大於0)。

我對它的每一側進行了線性動畫處理(250毫秒),但是您可以根據需要進行調整。

例

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')),
        ],
      ),
    );
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM