Hoa*_*yen 4 state-management flutter
我有一个TabBarView3 TabBar。当我在选项卡 1 中时我做了一些事情,然后当我回到选项卡 1 时导航到选项卡 2 我希望选项卡 1 的先前状态不会改变。
我如何在 Flutter 中实现这一点?
下面是我的代码截图
class _LandingPageState extends State<LandingPage> with SingleTickerProviderStateMixin {
int _selectedIndex = 0;
PageController pageController;
TabController tabController;
@override
void initState() {
tabController = TabController(length: 3, vsync: this, initialIndex: 0);
pageController = PageController(initialPage: 0)
..addListener(() {
setState(() {
_selectedIndex = pageController.page.floor();
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
currentIndex: _selectedIndex,
onTap: (index) {
setState(() {
_selectedIndex = index;
tabController.animateTo(index,
duration: Duration(microseconds: 300),
curve: Curves.bounceIn);
});
},
items: [
BottomNavigationBarItem(
icon: Icon(Icons.assignment), title: Text("Các yêu c?u")),
BottomNavigationBarItem(
icon: Icon(Icons.history), title: Text("L?ch s?")),
BottomNavigationBarItem(
icon: Icon(Icons.person), title: Text("H? s?")),
]),
body: TabBarView(
controller: tabController,
children: [
RequestPage(key: PageStorageKey<String>("request_page"),),
HistoryPage(key: PageStorageKey<String>("history_page")),
ProfilePage(key: PageStorageKey<String>("profile_page"))])
),
);
}
Run Code Online (Sandbox Code Playgroud)
Mig*_*ivo 13
确保您的所有TabBarView孩子都是StatefulWidgets,然后在所有孩子中添加一个AutomaticKeepAliveClientMixin,例如,对于您的RequestPage,它应该如下所示:
class RequestPage extends StatefulWidget {
RequestPage({Key key}) : super(key: key);
@override
_RequestPageState createState() => _RequestPageState();
}
class _RequestPageState extends State<RequestPage> with AutomaticKeepAliveClientMixin{
@override
Widget build(BuildContext context) {
super.build(context);
return // Your widget tree
}
@override
bool get wantKeepAlive => true;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1994 次 |
| 最近记录: |