前言
本來想把之前自己的ReactNative
的Demo
項(xiàng)目拿來變成Flutter
版本,但是這幾年一直搞Native
開發(fā)回過頭去看RN
的項(xiàng)目好多都不明白了,真是開發(fā)者的噩夢(mèng),接觸好多東西不使用的話漸漸地都模糊了,所以還是得把有的東西重點(diǎn)記下來非区,就相當(dāng)于是自己的筆記一樣沒事翻著看看,這次Flutter
就選擇豆瓣去嘗試下盹廷,畢竟豆瓣也有開放的API
供開發(fā)者使用
底部導(dǎo)航
微信圖片_20220529161305.jpg
首頁 書影音 小組 市集 我
這部分選擇使用BottomNavigationBar
去做底部導(dǎo)航欄的實(shí)現(xiàn)
搭建項(xiàng)目
創(chuàng)建一個(gè)新的Flutter
項(xiàng)目
將HomePage
相關(guān)抽離出來到另一個(gè)Dart
文件中征绸,并更改部分命名為MainPage
使用Route
管理頁面切換,默認(rèn)進(jìn)入Main
頁面
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.green,
),
routes: {"/main": (BuildContext context) => MainPage(title: "Main")},
initialRoute: "/main",
);
}
}
使用BottomNavigationBar
實(shí)現(xiàn)底部導(dǎo)航UI
俄占,并為底部導(dǎo)航添加事件
class MainPage extends StatefulWidget {
MainPage({Key? key, required this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
int currentIndex = 0;
void onNavigationChanged(int index) {
setState(() {
currentIndex = index;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
print('build');
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(icon: Icon(Icons.home), label: '首頁'),
BottomNavigationBarItem(icon: Icon(Icons.book), label: '書影音'),
BottomNavigationBarItem(icon: Icon(Icons.group), label: '小組'),
BottomNavigationBarItem(icon: Icon(Icons.store), label: '市集'),
BottomNavigationBarItem(icon: Icon(Icons.person), label: '我')
],
currentIndex: currentIndex,
unselectedItemColor: Colors.grey,
selectedItemColor: Colors.green,
type: BottomNavigationBarType.fixed,
onTap: (index)=>{
onNavigationChanged(index)
},
),
);
}
}
這里默認(rèn)使用了有狀態(tài)可變的StatefulWidget
是因?yàn)槲覀兊捻撁媸强勺兊墓艿。谖覀冞x中其他底部功能時(shí)頁面是需要刷新的
onTap
函數(shù)調(diào)用之后,我們更改了屬性currentIndex
缸榄,同時(shí)調(diào)用了
setState(() {
currentIndex = index;
});
然后界面就會(huì)刷新渤弛,build
函數(shù)會(huì)重新去渲染已得到更新的頁面,細(xì)節(jié)后面我們可以一起看看setState
函數(shù)的實(shí)現(xiàn)
效果
微信圖片_20220529173942.jpg
歡迎關(guān)注Mike的簡(jiǎn)書
Android 知識(shí)整理