gpt4 book ai didi

flutter - Tabbar抖动中的向后处理按钮

转载 作者:行者123 更新时间:2023-12-03 04:17:29 25 4
gpt4 key购买 nike

我有一个TabBar,可以显示总共16个动态显示的类别,
用例:当我单击类别中的任何类别时,假设我单击类别5,那么如果我按电话中的后退按钮,则需要转到类别1,默认情况下,如果我按后退按钮2次,我也可以显示类别1也需要关闭应用程序
但是当我按下后退按钮时我需要显示类别1,我该怎么做,我已经尝试过willpopScope,但是什么也没有发生,这是我到目前为止尝试过的

WillPopScope(
onWillPop: _onWillPop,
child: category_list.length != 0
? DefaultTabController(
length: category_list.length,
initialIndex: 1,
child: Column(
children: <Widget>[
Column(
children: [
Container(
constraints: BoxConstraints.expand(height: 40),
child: TabBar(
controller: _tabController,
isScrollable: true,
indicatorColor: Color(0xff00ADEE),
labelColor: Color(0xff00ADEE),
unselectedLabelColor: Colors.black,
tabs: getTab(),
),
),
],
),
Expanded(
child: Container(
child: TabBarView(
controller: _tabController,
children: createDynamicslugWIdget()),
),
)
],
),
)
: Center(
child: CircularProgressIndicator(),
),
)
尝试返回索引1
Future<bool> _onWillPop() async {
return _tabController.index==1;
}

最佳答案

您可以在下面复制粘贴运行完整代码
我用官方的例子来模拟这种情况
您可以设置_tabController.index程式码片段

Future<bool> _onWillPop() async {
print("on will pop");
if (_tabController.index == 0) {
await SystemNavigator.pop();
}

Future.delayed(Duration(milliseconds: 200), () {
print("set index");
_tabController.index = 0;
});

print("return");
return _tabController.index == 0;
}
工作演示
enter image description here
完整的代码
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class MyTabbedPage extends StatefulWidget {
const MyTabbedPage({Key key}) : super(key: key);
@override
_MyTabbedPageState createState() => _MyTabbedPageState();
}

class _MyTabbedPageState extends State<MyTabbedPage>
with SingleTickerProviderStateMixin {
final List<Tab> myTabs = <Tab>[
Tab(text: '0'),
Tab(text: '1'),
Tab(text: '2'),
Tab(text: '3'),
Tab(text: '4'),
];

TabController _tabController;

@override
void initState() {
super.initState();
_tabController = TabController(vsync: this, length: myTabs.length);
}

@override
void dispose() {
_tabController.dispose();
super.dispose();
}

Future<bool> _onWillPop() async {
print("on will pop");
if (_tabController.index == 0) {
await SystemNavigator.pop();
}

Future.delayed(Duration(milliseconds: 200), () {
print("set index");
_tabController.index = 0;
});

print("return");
return _tabController.index == 0;
}

@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _onWillPop,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
controller: _tabController,
tabs: myTabs,
),
),
body: TabBarView(
controller: _tabController,
children: myTabs.map((Tab tab) {
final String label = tab.text.toLowerCase();
return Center(
child: Text(
'This is the $label tab',
style: const TextStyle(fontSize: 36),
),
);
}).toList(),
),
),
);
}
}

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyTabbedPage(),
);
}
}

关于flutter - Tabbar抖动中的向后处理按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64781805/

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