【问题标题】:How to add icon inside a TabBar in Flutter?如何在 Flutter 的 TabBar 内添加图标?
【发布时间】:2021-03-27 07:26:52
【问题描述】:

如何在 Flutter 的 TabBar 中添加图标? 感谢您的帮助

【问题讨论】:

    标签: flutter tabbar


    【解决方案1】:

    如果您所说的图标是指 Icon 小部件,则可以使用其 icon 属性将它们直接添加到 Tab 小部件。

    Tab(icon: Icon(Icons.verified), text: 'Tab 1', ),
    

    一个完整的小部件示例:

    class HomePage extends StatelessWidget {
      HomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      Widget build(BuildContext context) {
        return DefaultTabController(
            length: 2,
            child: Scaffold(
              appBar: AppBar(
                title: Text(title),
                bottom: TabBar(
                  tabs: <Tab>[
                    Tab(
                      icon: Icon(Icons.verified),
                      text: 'Tab 1',
                    ),
                    Tab(
                      icon: Icon(Icons.new_releases),
                      text: 'Tab 2',
                    ),
                  ],
                ),
              ),
              backgroundColor: Colors.black,
              body: TabBarView(children: <ExampleTab>[
                ExampleTab(),
                ExampleTab()
              ]),
            ));
      }
    }
    

    如果您指的是图片而不是文本,例如您链接的图片,您可以使用child 属性添加您喜欢的任何小部件,例如可以是Image 小部件。

    Tab(child: Image.network("https://placeimg.com/50/50")),
    
    class HomePage extends StatelessWidget {
      HomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      Widget build(BuildContext context) {
        return DefaultTabController(
            length: 2,
            child: Scaffold(
              appBar: AppBar(
                title: Text(title),
                bottom: TabBar(
                  tabs: <Tab>[
                    Tab(
                      child: Image.network("https://placeimg.com/50/50"),
                    ),
                    Tab(
                      icon: Icon(Icons.new_releases),
                      text: 'Tab 2',
                    ),
                  ],
                ),
              ),
              backgroundColor: Colors.black,
              body: TabBarView(children: <ExampleTab>[
                ExampleTab(),
                ExampleTab()
              ]),
            ));
      }
    }
    

    您可以在Container 中添加将Image 包裹起来的边框、圆角或其他样式。

    【讨论】:

      猜你喜欢
      • 2018-12-19
      • 1970-01-01
      • 2020-03-01
      • 2017-08-11
      • 1970-01-01
      • 2019-09-19
      • 2021-04-27
      • 2020-12-30
      • 2020-01-16
      相关资源
      最近更新 更多