積累知識蓝角,勝過積蓄金銀
1.現(xiàn)在有個目標(biāo) 做一個APP 常用的主頁 那么阱穗,在Flutter中,代替 Android 中的ViewPager組件是PageView使鹅,而且揪阶,這個PageView相比ViewPager擴(kuò)展性更高。
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int currentIndex = 0;
final List<BottomNavigationBarItem> bottomNavItems = [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text("首頁"),
),
BottomNavigationBarItem(
icon: Icon(Icons.message),
title: Text("消息"),
),
BottomNavigationBarItem(
icon: Icon(Icons.shopping_cart),
title: Text("購物車"),
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
title: Text("個人中心"),
),
];
PageController pageController ;
@override
void initState() {
// TODO: implement initState
super.initState();
pageController = PageController(initialPage: 0);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('PageView + BottomNavigationBar')),
body: PageView(
physics: NeverScrollableScrollPhysics(),
controller: pageController,
children: <Widget>[
Center(child: Text("首頁"),),
Center(child: Text("消息"),),
Center(child: Text("購物車"),),
Center(child: Text("個人中心"),),
],
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: currentIndex,
items: bottomNavItems,
onTap: (index) {
pageController.jumpToPage(index);
currentIndex = index;
setState(() {
});
},
),
);
}
}