Flutter 如何使用flatter';什么是威尔波普?
我正在颤振项目中使用BottomNavigationBar。 此问题针对第30行“//TODO:返回第一个选项卡”。 当用户在SecondTab或ThirdTab中并且按下Android设备中的BackButton时,我希望用户转到第一个选项卡。 现在,在onWillpopScope中,有一个进程可以随时弹出。当用户处于下一页时使用。 然后,当用户不在第一个选项卡(第二个选项卡或第三个选项卡)中且不在下一个选项卡中时,我想将其移动到onWillpopScope中的第一个选项卡。(我想强制底部导航栏切换。) 我该如何描述它?请告诉我Flutter 如何使用flatter';什么是威尔波普?,flutter,dart,flutter-layout,flutter-cupertino,flutter-bottomnavigation,Flutter,Dart,Flutter Layout,Flutter Cupertino,Flutter Bottomnavigation,我正在颤振项目中使用BottomNavigationBar。 此问题针对第30行“//TODO:返回第一个选项卡”。 当用户在SecondTab或ThirdTab中并且按下Android设备中的BackButton时,我希望用户转到第一个选项卡。 现在,在onWillpopScope中,有一个进程可以随时弹出。当用户处于下一页时使用。 然后,当用户不在第一个选项卡(第二个选项卡或第三个选项卡)中且不在下一个选项卡中时,我想将其移动到onWillpopScope中的第一个选项卡。(我想强制底部导航
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<GlobalKey<NavigatorState>> navigatorKeys = [
GlobalKey<NavigatorState>(),
GlobalKey<NavigatorState>(),
GlobalKey<NavigatorState>(),
];
@override
Widget build(BuildContext context) {
int currentIndex = 0;
return MaterialApp(
home: WillPopScope(
onWillPop: () async {
final isFirstRouteInCurrentTab = await navigatorKeys[currentIndex].currentState.maybePop();
if (isFirstRouteInCurrentTab) {
if (currentIndex != 0) {
//TODO: back to the FirstTab
return false;
}
}
return isFirstRouteInCurrentTab;
},
child: CupertinoTabScaffold(
tabBar: CupertinoTabBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(label: 'Home', icon: Icon(Icons.home)),
BottomNavigationBarItem(label: 'Search', icon: Icon(Icons.search)),
BottomNavigationBarItem(label: 'Setting', icon: Icon(Icons.settings)),
],
onTap: (index) {
// back home only if not switching tab
if (currentIndex == index) {
switch (index) {
case 0:
navigatorKeys[index].currentState.popUntil((route) => route.isFirst);
break;
case 1:
navigatorKeys[index].currentState.popUntil((route) => route.isFirst);
break;
case 2:
navigatorKeys[index].currentState.popUntil((route) => route.isFirst);
break;
}
}
currentIndex = index;
},
currentIndex: currentIndex,
),
tabBuilder: (BuildContext context, int index) {
return CupertinoTabView(
navigatorKey: navigatorKeys[index],
builder: (BuildContext context) {
switch (index) {
case 0:
return FirstTab();
case 1:
return SecondTab();
case 2:
return ThirdTab();
default:
return FirstTab();
}
},
);
},
),
),
);
}
}
class FirstTab extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text('first page now'),
),
backgroundColor: Colors.red[200],
child: Center(
child: CupertinoButton(
child: const Text('Next'),
onPressed: () {
Navigator.of(context).push(CupertinoPageRoute(builder: (context) => NextPage()));
},
),
),
);
}
}
//Different color from Firsttab
class SecondTab extends StatelessWidget {}
//Different color from Firsttab
class ThirdTab extends StatelessWidget {}
class NextPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text('second page now'),
),
backgroundColor: Colors.white,
child: Center(
child: CupertinoButton(
child: const Text('Back'),
onPressed: () {
Navigator.of(context).pop();
},
),
),
);
}
}
导入“包装:颤振/材料.省道”;
进口“包装:颤振/cupertino.dart”;
void main(){
runApp(MyApp());
}
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
列表导航项=[
GlobalKey(),
GlobalKey(),
GlobalKey(),
];
@凌驾
小部件构建(构建上下文){
int currentIndex=0;
返回材料PP(
家:威尔波普(
onWillPop:()异步{
final isFirstRouteInCurrentTab=await navigatorKeys[currentIndex].currentState.maybePop();
如果(isFirstRouteInCurrentTab){
如果(currentIndex!=0){
//TODO:回到第一个选项卡
返回false;
}
}
返回isFirstRouteInCurrentTab;
},
孩子:库珀提诺巴塞弗德(
tabBar:CupertinoTabBar(
项目:[
BottomNavigationBarItem(标签:“主页”,图标:图标(Icons.Home)),
BottomNavigationBarItem(标签:“搜索”,图标:图标(Icons.Search)),
BottomNavigationBarItem(标签:“设置”,图标:图标(Icons.settings)),
],
onTap:(索引){
//仅当不切换选项卡时返回主页
如果(currentIndex==索引){
开关(索引){
案例0:
navigatorKeys[index].currentState.popintil((route)=>route.isFirst);
打破
案例1:
navigatorKeys[index].currentState.popintil((route)=>route.isFirst);
打破
案例2:
navigatorKeys[index].currentState.popintil((route)=>route.isFirst);
打破
}
}
currentIndex=索引;
},
currentIndex:currentIndex,
),
tabBuilder:(BuildContext,int-index){
返回CupertinoTabView(
导航工作:导航工作[索引],
生成器:(BuildContext上下文){
开关(索引){
案例0:
返回FirstTab();
案例1:
返回SecondTab();
案例2:
返回ThirdTab();
违约:
返回FirstTab();
}
},
);
},
),
),
);
}
}
类FirstTab扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回式CupertinoPageScaffold(
导航栏:CupertinoNavigationBar(
中间:文本(“现在是第一页”),
),
背景颜色:颜色。红色[200],
儿童:中心(
孩子:丘比特纽顿(
子项:常量文本(“下一个”),
已按下:(){
push(CupertinoPageRoute(builder:(context)=>NextPage());
},
),
),
);
}
}
//与Firsttab不同的颜色
类SecondTab扩展了无状态小部件{}
//与Firsttab不同的颜色
类ThirdTab扩展了无状态小部件{}
类NextPage扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回式CupertinoPageScaffold(
导航栏:CupertinoNavigationBar(
中间:文本(“现在是第二页”),
),
背景颜色:Colors.white,
儿童:中心(
孩子:丘比特纽顿(
子项:常量文本('Back'),
已按下:(){
Navigator.of(context.pop();
},
),
),
);
}
}
第一步
int currentIndex=0
;
给班级成员
下一个
你可以试试看
我在这里继续。首先,您需要创建一个控制器
final CupertinoTabController _controller = CupertinoTabController();
然后像这样加在你的杯子里
CupertinoTabScaffold(
...
controller: _controller,
)
最后,您可以这样更改页面:
_controller.index = 0,
(这就是在颤振中获取和设置工作的方式)
_controller.index = 0,