我实现了一个非常简单的系统:
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => MainState();
}
class MainState extends State<MyApp> {
@override
void initState() {
futureData = getModelsList();
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home:Scaffold(
bottomNavigationBar: BottomNavigationBar(
type:BottomNavigationBarType.shifting,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home,
color: Colors.white,
),label: "",
backgroundColor: Colors.grey,
),
BottomNavigationBarItem(
icon: Icon(Icons.description,
color: Colors.white,
),label: "",
backgroundColor: Colors.grey,
),
BottomNavigationBarItem(
icon: Icon(Icons.calculate,
color: Colors.white,
),label: "",
backgroundColor: Colors.grey,
),
BottomNavigationBarItem(
icon: Icon(Icons.question_answer,
color: Colors.white,
),label: "",
backgroundColor: Colors.grey,
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: (newIndex) {
setState(() {
_selectedIndex = newIndex;
pageController.animateToPage(_selectedIndex, duration: Duration(milliseconds: 300), curve: Curves.ease);
});
},
),
body: PageView(
controller: pageController,
onPageChanged: (newIndex){
setState(() {
_selectedIndex = newIndex;
});
},
children: [
buildPage1(),
buildPage1(),
buildPage1(),
buildPage1(),
]
),
)
发生的情况是,如果 buildPage1 是一个简单的页面,比如只有一个 Text('Page1') ,那么一切都运行良好。但是,如果我使该页面变得更复杂,从实际页面到所选页面的导航或动画就会很奇怪。我不知道用英语怎么说,但运行不顺利,就像一个滞后......
我正在使用 setState 使用新选定的索引重建屏幕。和我在网上看到的方法是一样的。所以我确信我在这里失去了一些东西。
您在 BottomNavigationBar 和 PageView 设置中的页面之间转换时似乎遇到了性能问题。您共享的代码看起来不错,但是您可以考虑一些优化和建议,以潜在地提高导航和动画的流畅度:
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => MainState();
}
class MainState extends State<MyApp> {
late Future<List<Model>> futureData;
@override
void initState() {
futureData = getModelsList();
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.shifting,
// ... BottomNavigationBarItem items ...
onTap: (newIndex) {
setState(() {
_selectedIndex = newIndex;
pageController.animateToPage(
_selectedIndex,
duration: Duration(milliseconds: 200),
curve: Curves.easeInOut,
);
});
},
),
body: PageView(
controller: pageController,
onPageChanged: (newIndex) {
setState(() {
_selectedIndex = newIndex;
});
},
children: [
KeepAlivePage(buildPage1()),
KeepAlivePage(buildPage1()),
KeepAlivePage(buildPage1()),
KeepAlivePage(buildPage1()),
],
),
),
);
}
}
class KeepAlivePage extends StatefulWidget {
const KeepAlivePage(this.child);
final Widget child;
@override
_KeepAlivePageState createState() => _KeepAlivePageState();
}
class _KeepAlivePageState extends State<KeepAlivePage>
with AutomaticKeepAliveClientMixin {
@override
bool get wantKeepAlive => true;
@override
Widget build(BuildContext context) {
super.build(context); // To keep the state alive
return widget.child;
}
}