在这篇文章里,我将向你展现在 BottomNavigationBar 里如何使用 Flutter 的 Provider。git
Provider 是 Flutter 团队推荐的新的状态管理方法。github
虽然 setState 能够用于大多数场景,可是建议你不要用。尤为是你用了 FlutterBuilder,而后在用 setState,就会使你的代码很混乱,会形成不少问题。 bash
接下来让咱们看如何在 BottomNavigationBar 里使用 Provider。less
provider: ^3.0.0+1
复制代码
class BottomNavigationBarProvider with ChangeNotifier {
int _currentIndex = 0;
get currentIndex => _currentIndex;
set currentIndex(int index) {
_currentIndex = index;
notifyListeners();
}
}
复制代码
在这个 Provider 里,我存储了 BottomNavigationBar 的当前表示展现第几个页面的索引的值,当设置新的索引的值以后,BottomNavigationBar 将会受到最新的值,而且刷新 tab。ide
home: ChangeNotifierProvider<BottomNavigationBarProvider>(
child: BottomNavigationBarExample(),
builder: (BuildContext context) => BottomNavigationBarProvider(),
),
复制代码
由于使用 ChangeNotifierProvider 包了 Widget,因此当 ChangeNotifierProvider 里的 _currentIndex 值发生变化的时候,Widget 就会收到通知。ui
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
alignment: Alignment.center,
height: 300,
width: 300,
child: Text(
"Home",
style: TextStyle(color: Colors.white, fontSize: 30),
),
color: Colors.amber,
)),
);
}
}
复制代码
class Profile extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
alignment: Alignment.center,
height: 300,
width: 300,
child: Text(
"Profile",
style: TextStyle(color: Colors.white, fontSize: 30),
),
color: Colors.blue,
),
),
);
}
}
复制代码
class Setting extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
alignment: Alignment.center,
height: 300,
width: 300,
child: Text(
"Settings",
style: TextStyle(color: Colors.white, fontSize: 30),
),
color: Colors.cyan,
)),
);
}
}
复制代码
这里建立里 BottomNavigationBar 里要使用的 3 个 tabs。spa
class BottomNavigationBarExample extends StatefulWidget {
@override
_BottomNavigationBarExampleState createState() =>
_BottomNavigationBarExampleState();
}
class _BottomNavigationBarExampleState
extends State<BottomNavigationBarExample> {
var currentTab = [
Home(),
Profile(),
Setting(),
];
@override
Widget build(BuildContext context) {
var provider = Provider.of<BottomNavigationBarProvider>(context);
return Scaffold(
body: currentTab[provider.currentIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: provider.currentIndex,
onTap: (index) {
provider.currentIndex = index;
},
items: [
BottomNavigationBarItem(
icon: new Icon(Icons.home),
title: new Text('Home'),
),
BottomNavigationBarItem(
icon: new Icon(Icons.person),
title: new Text('Profile'),
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
title: Text('Settings'),
)
],
),
);
}
}
复制代码
这里使用 Provider 的代码为:code
@override
Widget build(BuildContext context) {
var provider = Provider.of<BottomNavigationBarProvider>(context);
return Scaffold(
...
bottomNavigationBar: BottomNavigationBar(
...
onTap: (index) {
provider.currentIndex = index;
},
...
)
)
}
复制代码
至此,建立了一个使用底部导航栏切换页面的应用,并且底部导航栏的切换使用到的当前页面的索引的值由 Provider 更新。cdn
这里是本篇文章涉及到的代码:github.com/flutter-dev…blog
Provide 不须要使用 setState 就能够变化显示的 tabs,可是若是你想持久化当前页面的状态,例如,下次打开 APP 时记住上次打开的是哪一个页面,可使用 PageStorageBucket,能够看我另外的一个实例,地址是: github.com/tensor-prog…