视频 p41
页面切换部分
List<Widget> _pages = [ChtPage(),FriendsPage(),DiscoverPage()];
int currentIndex = 0;
final PageConroller _controller = PageController();
Widget build(BuildContext context){
return Scaffold(
body:PageView(
physics:NaverScrollableScrollPhysics(), //禁止左右拖拽
//左右拖拽时,也要改变 当前index
onPageChanged:(int index){
_currentIndex = index;
setState((){});
},
controller:_controller,
children:_pages,
),
bottomNavigationBar:BottomNavigationBar(
onTap:(index){
setState((){
_currentIndex = index;
_controller.jumpToPage(_currentIndex);
})
}
)
);
}
在各页面设置可以保持页面active 的属性
class ChatPage extends StatefulWidget{
_ChatPageState createState()=>_ChatPageState();
}
class _ChatPageState extends State<ChatPage>
with AutomatickeepAliveClientMixin<ChatPage>
{
bool get wantKeepAlive=>true;
Widget build(BuildContext context){
super.build(context);
return Scaffold();
}
}