首页 > 解决方案 > unselectedLabelstyle 在 Flutter 中不起作用

问题描述

为什么我的对我unselectedLabelStyle不起作用?在这个 unselectedLabelStyle 中使用 textStyle 属性保存我的更改后,我什么都不会改变:/

class _NavBarState extends State<NavBar> {
  int _selectedPage = 0;

  PageController pageController = PageController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: pageController,
        children: <Widget>[
          HomeScreen(),
          MapScreen(),
          SearchScreen(),
          AccountScreen(),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _selectedPage,
        backgroundColor: Colors.white,
        selectedIconTheme: IconThemeData(color: Colors.black, size: 32),
        // selectedLabelStyle: TextStyle(fontWeight: FontWeight.bold),
        unselectedIconTheme: IconThemeData(size: 26, color: Colors.grey.shade300),
        unselectedLabelStyle: TextStyle(color: Colors.grey.shade300),//HEEEEEERRRRRRRRREEEEEE
        onTap: (int index) {
          setState(() {
            _selectedPage = index;
          });

          pageController.animateToPage(
            index,
            duration: Duration(milliseconds: 500),
            curve:
                Curves.easeInOutExpo, 
          );
        },
        items: [
          BottomNavigationBarItem(
              icon: Icon(
                Icons.home,
              ),
              title: Text('Główna', style: TextStyle(color: Colors.black))),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.location_on,
              ),
              title: Text('Mapa', style: TextStyle(color: Colors.black))),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.search,
              ),
              title: Text('Szukaj', style: TextStyle(color: Colors.black))),
          BottomNavigationBarItem(
            icon: Icon(
              Icons.person,
            ),
            title: Text('Konto', style: TextStyle(color: Colors.black)),
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    pageController.dispose();
    super.dispose();
  }
}

标签: flutterdartnavbar

解决方案


您通过直接在标题中提供标题样式来重叠 textStyle,此外,您可以使用unselectedItemColor属性来更改未选择项目的颜色。

unselectedItemColor: Colors.grey,

更改标题

 title: Text('Główna', style: TextStyle(color: Colors.black))),

至:

 title: Text('Główna')),

此外,我认为你应该在flutter sdk中提交错误,因为它没有像你提到的那样工作。

 /// If [selectedLabelStyle.color] and [unselectedLabelStyle.color] values
  /// are non-null, they will be used instead of [selectedItemColor] and
  /// [unselectedItemColor].

根据以下文档注释,如果您指定 unselectedLabelStyle 的颜色,那么它应该使用该颜色但它没有使用它。


推荐阅读