Flutter 颤振飞镖:底部导航儿童一次初始化,而不是第一次点击初始化
我有一个带有项目的底部导航,子项位于indexedItems中。但问题是应用程序启动时会初始化其所有子项。与iOS中的行为类似,它初始化第一个孩子,当第一次点击选项卡时,其他孩子将被初始化。之后,它们将不会重新初始化。我怎样才能达到同样的行为Flutter 颤振飞镖:底部导航儿童一次初始化,而不是第一次点击初始化,flutter,dart,bottomnavigationview,Flutter,Dart,Bottomnavigationview,我有一个带有项目的底部导航,子项位于indexedItems中。但问题是应用程序启动时会初始化其所有子项。与iOS中的行为类似,它初始化第一个孩子,当第一次点击选项卡时,其他孩子将被初始化。之后,它们将不会重新初始化。我怎样才能达到同样的行为 import '../../UI/Logout/LogoutScreen.dart'; import 'package:flutter/material.dart'; import '../MyTask/MyTaskListScreen.dart'; im
import '../../UI/Logout/LogoutScreen.dart';
import 'package:flutter/material.dart';
import '../MyTask/MyTaskListScreen.dart';
import './../Home/HomeScreen.dart';
import 'package:hexcolor/hexcolor.dart';
class RootScreen extends StatefulWidget {
@override
_RootScreenState createState() => _RootScreenState();
}
class _RootScreenState extends State<RootScreen> {
final List<Widget> childScreens = [
HomeScreen(),
MyTaskListScreen(),
LogoutScreen()
];
int _selectedIndex = 0;
void _selectPage(int index, context) {
if (index == 2) {
LogoutScreen().showLogoutView(context);
return;
}
this.setState(() {
this._selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: this._selectedIndex,
children: this.childScreens,
),
bottomNavigationBar: BottomNavigationBar(
onTap: (index) => this._selectPage(index, context),
backgroundColor: Hexcolor("#f2f2f2"),
unselectedItemColor: Colors.grey,
selectedItemColor: Colors.blue,
currentIndex: this._selectedIndex,
type: BottomNavigationBarType.fixed,
showSelectedLabels: false,
showUnselectedLabels: false,
items: [
BottomNavigationBarItem(
activeIcon: ImageIcon(
AssetImage(
"lib/Source/Resources/Assets/Root/registration/active/activeRegistration.png"),
color: Colors.black,
),
icon: ImageIcon(
AssetImage(
"lib/Source/Resources/Assets/Root/registration/default/registration.png"),
color: Colors.black,
),
label: ""),
BottomNavigationBarItem(
activeIcon: ImageIcon(
AssetImage(
"lib/Source/Resources/Assets/Root/myTask/active/myTask.png"),
color: Colors.black,
),
icon: ImageIcon(
AssetImage(
"lib/Source/Resources/Assets/Root/myTask/default/myTask.png"),
color: Colors.black,
),
label: ""),
BottomNavigationBarItem(
backgroundColor: Colors.lightBlue,
icon: Icon(Icons.logout),
label: "")
],
));
}
}
import'../UI/Logout/LogoutScreen.dart';
进口“包装:颤振/材料.省道”;
导入“../MyTask/MyTaskListScreen.dart”;
导入“/../Home/HomeScreen.dart”;
导入“package:hexcolor/hexcolor.dart”;
类RootScreen扩展StatefulWidget{
@凌驾
_RootScreenState createState()=>\u RootScreenState();
}
类_RootScreenState扩展状态{
最终列表子屏幕=[
主屏幕(),
MyTaskListScreen(),
注销屏幕()
];
int _selectedIndex=0;
void\u选择页面(整数索引、上下文){
如果(索引==2){
LogoutScreen().showLogoutView(上下文);
返回;
}
此.setState(){
这是.\u选择的索引=索引;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:IndexedStack(
索引:此。\u选择索引,
孩子们:这个,孩子们,
),
底部导航栏:底部导航栏(
onTap:(索引)=>此项。\u选择页面(索引、上下文),
背景颜色:Hexcolor(#f2f2”),
unselectedItemColor:Colors.grey,
选择编辑颜色:Colors.blue,
currentIndex:this.\u选择了索引,
类型:BottomNavigationBarType.fixed,
showSelectedLabels:false,
显示未选择的标签:false,
项目:[
底部导航气压计(
activeIcon:ImageIcon(
资产评估(
“lib/Source/Resources/Assets/Root/registration/active/activeRegistration.png”),
颜色:颜色,黑色,
),
图标:图像图标(
资产评估(
“lib/Source/Resources/Assets/Root/registration/default/registration.png”),
颜色:颜色,黑色,
),
标签:),
底部导航气压计(
activeIcon:ImageIcon(
资产评估(
“lib/Source/Resources/Assets/Root/myTask/active/myTask.png”),
颜色:颜色,黑色,
),
图标:图像图标(
资产评估(
“lib/Source/Resources/Assets/Root/myTask/default/myTask.png”),
颜色:颜色,黑色,
),
标签:),
底部导航气压计(
背景颜色:颜色。浅蓝色,
图标:图标(图标.注销),
标签:“”)
],
));
}
}
您要求什么样的初始化,我认为如果您想在每个页面导航上初始化一些网络请求,那么您可以通过在有状态小部件的initState中调用它来实现,或者在模型类中执行相同的操作,每次单击底部导航按钮时,您的小部件都将被实例化。