我想在水平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'),),
),
],
);
}
}
答案 0 :(得分:0)
Tabbar具有一个名为onTap的属性。在onTap()中,您可以获取索引并处理综合浏览量的内容。示例:
TabBar(
onTap: (int tabIndex) {
// you can perform any action here using index
})
答案 1 :(得分:0)
好的,我相信我已经知道了您要寻找的东西。为此,我建议您在小部件树中使用类似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')),
],
),
);
}
}