flutter - 如何在Flutter中使用图像图标(来自 Assets )而不是IconData和分页bottomNavigationBar

标签 flutter dart icons imageicon flutter-bottomnavigation

我在flutter项目中使用bottomNavigationBar,是flutter的新手,我不知道分页,而是使用素材资源图片图标而不是iconData。我搜索了过去两天,但没有得到满意的结果。请帮我......
我从这里使用了带有fab按钮的bottomNavigationBar
https://medium.com/coding-with-flutter/flutter-bottomappbar-navigation-with-fab-8b962bb55013
https://github.com/bizz84/bottom_bar_fab_flutter
我也尝试从这里使用自定义图标
https://medium.com/flutterpub/how-to-use-custom-icons-in-flutter-834a079d977
但没有成功
我只想更改图标,并想知道如何使用分页。在上一个分页示例代码中,我可以做些什么。

最佳答案

这是如何使用 Assets 中的图标的方法

ImageIcon(
     AssetImage("images/icon_more.png"),
     color: Color(0xFF3A5A98),
),


为BottomNavBar尝试此示例click

因此,您要替换的是BottomNavigationBarItem
 new BottomNavigationBarItem(
           icon: Icon(Icons.home),
           title: Text('Home'),
         ),


 new BottomNavigationBarItem(
           icon: ImageIcon(
               AssetImage("images/icon_more.png"),
                    color: Color(0xFF3A5A98),
               ),
           title: Text('Home'),
         ),

您可以从我分享的文章中了解导航

更新
这是您所要求的示例。

因此,这里_children变量包含您要根据BottomNavBarItem的选择导航的页面列表。

我们的导航方式是当我们按下选项卡项目时,使用onTabTapped函数设置其索引。当索引更改时, View 也相应更改,因为我们指示主体显示子级的当前索引。

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _currentIndex = 0;
  final List<Widget> _children = [
    Container(
      color: Colors.red,
    ),
    Container(
      color: Colors.blue,
    ),
    Container(
      color: Colors.green,
    )
  ];

  void onTabTapped(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _children[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        onTap: onTabTapped, // new
        currentIndex: _currentIndex, // new
        items: [
          new BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: Text('Home'),
          ),
          new BottomNavigationBarItem(
            icon: Icon(Icons.mail),
            title: Text('Messages'),
          ),
          new BottomNavigationBarItem(
              icon: Icon(Icons.person), title: Text('Profile'))
        ],
      ),
    );
  }
}

关于flutter - 如何在Flutter中使用图像图标(来自 Assets )而不是IconData和分页bottomNavigationBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56438647/

相关文章:

http - 在服务器端的 Dart 中,如何在 HttpClient 中设置 header

dart - Flutter 中的可拖动 FloatingActionButton

ios - Swift 和 Xcode - 如何创建自定义标签栏图标

android - Prestashop 语言标志图标在移动设备上看起来很糟糕

android - 如何在文本小部件 flutter 中显示 html 字符串

android - flutter 中一个屏幕上有两个 TabBar

android-studio - 如何更改 FlutterLogo 图像

c++ - 如何在C++中制作一个图标按钮

android - Flutter - BloC Cubit 函数不发射状态

flutter - Dart Flutter 抛出 FlutterError.fromParts(<DiagnosticsNode>[ 错误