我正在开发一个应用程序,使用 CupertinoApp 模型。我正在为应用程序使用 CupertinoTabView 模型。现在在开发过程中,标签索引会在热重载时重置。我尝试在选项卡控制器上设置状态,但它仍然会重置。
代码:
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
CupertinoTabController _controller = CupertinoTabController();
@override
void dispose() {
_controller.dispose();
super.dispose();
}
List<Widget> tabs = [
HomeScreen(_controller),
PlaySearchScreen(),
HomeScreen(_controller),
ProfileScreen(),
];
return CupertinoTabScaffold(
controller: _controller,
tabBar: CupertinoTabBar(
items: [
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.play),
label: 'Play Tennis',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.calendar),
label: ' My Schedule',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.person),
label: 'Profile',
),
],
),
tabBuilder: (context, index) {
return CupertinoTabView(
builder: (ctx) {
return GestureDetector(
child: tabs[index],
onTap: () => setState(
() => _controller.index = index,
),
);
},
);
});
}
}
class HomeScreen extends StatefulWidget {
HomeScreen(
this.controller, {
Key key,
}) : super(key: key);
final CupertinoTabController controller;
@override
_HomeScreenState createState() => _HomeScreenState();
}
有没有办法不重置?