【问题标题】:How to style BottomNavigationBarItem label如何设置 BottomNavigationBarItem 标签的样式
【发布时间】:2021-02-04 15:56:36
【问题描述】:

在我的应用程序中,我有一个按钮导航栏。

我正在使用底部导航栏实现多导航,如下所示; https://medium.com/coding-with-flutter/flutter-case-study-multiple-navigators-with-bottomnavigationbar-90eb6caa6dbf

我已经把标题文字这样放了。

  BottomNavigationBarItem _buildItem(
      {TabItem tabItem, String tabText, IconData iconData}) {
    return BottomNavigationBarItem(
      icon: Icon(
        iconData,
        color: widget.currentTab == tabItem
            ? active_button_color
            : Colors.grey,
      ),
      //label: tabText,

      title: Text(
        tabText,
        style: widget.currentTab == tabItem
            ? Archivo_12_0xff002245
            : Archivo_12_grey,
      ),
    );

我收到消息标题已弃用。

当我使用 label 参数时,我如何设置它的样式?

【问题讨论】:

    标签: flutter flutter-bottomnavigation


    【解决方案1】:

    您可以使用 BottomNavigationBar 上的属性对其进行样式设置。 示例:

    bottomNavigationBar: BottomNavigationBar(
      items: const <BottomNavigationBarItem>[
        BottomNavigationBarItem(
          icon: Icon(Icons.home),
          label: 'First',
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.exit_to_app),
          label: 'Second',
        ),
      ],
      selectedLabelStyle: TextStyle(fontSize: 22),
      selectedItemColor: Colors.red,
    ),
    

    当然还有更多的属性。检查文档:https://api.flutter.dev/flutter/material/BottomNavigationBar-class.html

    【讨论】:

    • 我想我应该提到我使用多导航器,如下所示;medium.com/coding-with-flutter/…
    • selectedLabelStyle 在这种情况下似乎不起作用
    • 为什么它似乎不起作用?是颜色属性不起作用吗?如果是这样,这就是我添加 selectedItemColor 部分的原因,因为颤振中存在潜在的错误。 (github.com/flutter/flutter/issues/67897)
    • 即使我更改了标签,我加载的第一个标签标签的选定颜色仍然存在。其他标签标签即使被选中也不会改变颜色
    • 用同样的方法但是selectedItemColor: some == expression? Colors.red : Colors.blue
    【解决方案2】:

    由于消息已经表明“标题”属性已被弃用,因此已将其替换为“标签”。

    关于选中的颜色不改变,需要添加一个函数调用'onItemTapped'通过设置状态来改变颜色。

    以下是工作代码:

    import 'package:flutter/material.dart';
    
    void main() => runApp(const MyApp());
    
    class MyApp extends StatelessWidget {
      const MyApp({Key key}) : super(key: key);
    
      static const String _title = 'TEST';
    
      @override
      Widget build(BuildContext context) {
        return const MaterialApp(
          title: _title,
          home: MyStatefulWidget(),
        );
      }
    }
    
    class MyStatefulWidget extends StatefulWidget {
      const MyStatefulWidget({Key key}) : super(key: key);
    
      @override
      _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
    }
    
    class _MyStatefulWidgetState extends State<MyStatefulWidget> {
      int _selectedIndex = 0;
      static const TextStyle optionStyle =
          TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
      static const List<Widget> _widgetOptions = <Widget>[
        Text(
          'Index 0: First',
          style: optionStyle,
        ),
        Text(
          'Index 1: Second',
          style: optionStyle,
        ),
      ];
    
      //METHOD TO SET STATE
      void _onItemTapped(int index) {
        setState(() {
          _selectedIndex = index;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('BottomNavigationBar'),
          ),
          body: Center(
            child: _widgetOptions.elementAt(_selectedIndex),
          ),
          bottomNavigationBar: BottomNavigationBar(
              items: const <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  icon: Icon(Icons.home),
                  label: 'First',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.exit_to_app),
                  label: 'Second',
                ),
              ],
              selectedLabelStyle: TextStyle(fontSize: 22),
              selectedItemColor: Colors.red,
    
              //THIS METHOD NEEDS TO BE CALLED TO CHANGE THE STATE
              onTap: _onItemTapped,
    
              currentIndex: _selectedIndex),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-17
      • 2018-10-05
      • 1970-01-01
      • 2019-01-15
      • 1970-01-01
      相关资源
      最近更新 更多