flutter - 如何改变主题数据?

标签 flutter dart mobile

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 "),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.all_out),
                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_NavigationBarState方法:
 @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
          showUnselectedLabels: true,
          unselectedItemColor: Theme.of(context).primaryColor,
          selectedItemColor: Theme.of(context).accentColor,
...

关于flutter - 如何改变主题数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63376299/

相关文章:

installation - flutter doctor 在 "Updating flutter tool..."后挂起

flutter - 如何从RenderBox知道Positioned属性?

dart - Flutter 隔离与 future

javascript - 如何检测手机浏览器的屏幕尺寸?

javascript - 使用 window.Touch 判断当前设备是否为移动设备

flutter - “Positional arguments must occur before named arguments. Try moving all of the positional arguments before the named arguments”错误抖动

java - 如何从 flutter 中正确调用 android native 代码(包括 android/IOS)?

java - Thawte 签名的 jar 文件在 Micromax 移动版中不起作用?

ios - App Center 上的 Flutter iOS 构建失败并出现错误 : "Invalid Podfile file: Generated. xcconfig must exist

dart - Chrome 应用程序中的 "Refused to load the image"