如何解决如何改变主题数据?
class _NavigationBarState extends State<NavigationBar> {
int _currentIndex = 0;
final List<Widget> tabs = [
CustomerAccountPage(),HomePage(),AppInforamtionPage(),CategoriesPage(),];
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.light().copyWith(
primaryColor: Colors.amber,accentColor: Colors.black
),debugShowCheckedModeBanner: false,home: Scaffold(
bottomNavigationBar: BottomNavigationBar(
showUnselectedLabels: true,unselectedItemColor: Theme.of(context).primaryColor,selectedItemColor: Theme.of(context).accentColor,currentIndex: _currentIndex,items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),title: Text("account"),),BottomNavigationBarItem(
icon: Icon(Icons.play_for_work),title: Text("shop"),BottomNavigationBarItem(
icon: Icon(Icons.all_out),title: Text("more info "),title: Text(" categories"),],onTap: (index) {
setState(() {
_currentIndex = index;
});
}),centerTitle: true,backgroundColor: Colors.white,body: tabs[_currentIndex],floatingActionButton: FloatingActionButton(onPressed: null,backgroundColor: Theme.of(context).primaryColor,);
}
}
我正在使用flutter,并尝试设置主题数据,但是它不起作用。
我尝试过以这种方式更改主题,但我的应用程序看不到结果,我不知道这是什么问题,有人可以帮我吗?
stackoverflow想让我解释更多,但是我没有其他要解释的了,谢谢!!
解决方法
Theme.of(context).primaryColor
中使用的上下文不是正确的上下文。您需要将MaterialApp
放在另一个包装当前小部件的小部件中,例如,
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,theme: ThemeData.light()
.copyWith(primaryColor: Colors.amber,accentColor: Colors.black),home: NavigationBar(),);
}
}
还有build
的{{1}}方法:
_NavigationBarState
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。