【发布时间】:2020-03-11 18:44:40
【问题描述】:
我有一个在其主页上使用 PageView 的应用程序。今天,我被分配在其中一个页面中插入 TabBarView。问题是当我在最后一个选项卡中滚动选项卡之间时,向左滚动不会滚动 PageView。
我需要一种方法来使页面视图的滚动在 tabbarview 的开始或结束时滚动。
我发现了一个关于倒置问题的问题:flutter PageView inside TabBarView: scrolling to next tab at the end of page
但是,那里所说的方法不适合我的问题。
我做了一个最小的例子:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
title: 'TabBarView inside PageView',
home: MyHomePage(),
);
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final PageController _pageController = PageController();
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text('TabBarView inside PageView'),
),
body: PageView(
controller: _pageController,
children: <Widget>[
Container(color: Colors.red),
GreenShades(),
Container(color: Colors.yellow),
],
),
);
}
class GreenShades extends StatefulWidget {
@override
_GreenShadesState createState() => _GreenShadesState();
}
class _GreenShadesState extends State<GreenShades>
with SingleTickerProviderStateMixin {
TabController _tabController;
@override
void initState() {
this._tabController = TabController(length: 3, vsync: this);
super.initState();
}
@override
Widget build(BuildContext context) => Column(
children: <Widget>[
TabBar(
labelColor: Colors.green,
indicatorColor: Colors.green,
controller: _tabController,
tabs: <Tab>[
const Tab(text: "Dark"),
const Tab(text: "Normal"),
const Tab(text: "Light"),
],
),
Expanded(
child: TabBarView(
controller: _tabController,
children: <Widget>[
Container(color: Colors.green[800]),
Container(color: Colors.green),
Container(color: Colors.green[200]),
],
),
)
],
);
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
}
请注意,在此 MRE 中,如果拖动 TabBar 可能会到达第 3 页,但如果拖动 TabBarView 则不会。
我怎样才能实现这种行为?
编辑:
正如@Fethi 所说,有一个类似的问题: Is it possible to swipe from an TabBarView content area to an adjacent PageView page?
但是,这个问题没有得到满意的回答,因为给出的解决方案并没有真正“混合”卷轴,尽管行为与描述的相似。它不会自然滚动。
【问题讨论】: