我想在水平 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'),),
),
],
);
}
}
Run Code Online (Sandbox Code Playgroud)
好的,我相信我知道你在寻找什么。为此,我建议NotificationListener在你的小部件树中使用类似 a 的东西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')),
],
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
Roh*_*ngh -2
Tabbar 有一个名为 onTap 的属性。在 onTap() 中,您可以获取索引并处理综合浏览的内容。例子:
TabBar(
onTap: (int tabIndex) {
// you can perform any action here using index
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2607 次 |
| 最近记录: |