gpt4 book ai didi

flutter - 是否可以从 TabBarView 内容区域滑动到相邻的 PageView 页面?

转载 作者:IT王子 更新时间:2023-10-29 06:44:48 24 4
gpt4 key购买 nike

我想在水平 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'),),
),
],
);
}
}

最佳答案

好的,我相信我知道您在寻找什么。为此,我建议在您的小部件树中使用类似 NotificationListener 的东西来捕获 OverscrollNotification 并且您可以向左或向右滚动,具体取决于过度滚动的一侧(较少左边大于 0,右边大于 0)。

我让它的每一侧都呈线性(250 毫秒)动画,但您可以根据需要对其进行调整。

example

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

关于flutter - 是否可以从 TabBarView 内容区域滑动到相邻的 PageView 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56907970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com