在Flutter中,可以通过使用StatefulWidget来维护TabBarView中的页面状态。下面是一个示例:
class TabBarViewPage extends StatefulWidget {
@override
_TabBarViewPageState createState() => _TabBarViewPageState();
}
class _TabBarViewPageState extends State<TabBarViewPage> {
int _currentIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('TabBarView Example'),
),
body: Column(
children: [
TabBar(
tabs: [
Tab(text: 'Tab 1'),
Tab(text: 'Tab 2'),
Tab(text: 'Tab 3'),
],
onTap: (index) {
setState(() {
_currentIndex = index;
});
},
),
Expanded(
child: TabBarView(
children: [
Page1(),
Page2(),
Page3(),
],
controller: TabController(
length: 3,
initialIndex: _currentIndex,
vsync: this,
),
),
),
],
),
);
}
}
通过以上步骤,可以在Flutter中维护TabBarView中的页面状态。
领取专属 10元无门槛券
手把手带您无忧上云