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

18 icons imageicon dart flutter flutter-bottomnavigation

我在我的颤振项目中使用 bottomNavigationBar 我是颤振的新手,我不知道分页和使用资产图像图标而不是 iconData。我在过去 2 天里搜索了它,但没有得到满意的结果。请帮我......

我在这里使用了带有 fab 按钮的底部导航栏 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

但没有成功

我只想更改图标并想知道如何使用分页。我可以在最后一个分页示例代码中做哪些更改。

Pra*_*ndo 43

以下是如何使用资产中的图标

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

Run Code Online (Sandbox Code Playgroud)

试试这个例子用于BottomNavBar点击

所以你要替换的是 BottomNavigationBarItem

 new BottomNavigationBarItem(
           icon: Icon(Icons.home),
           title: Text('Home'),
         ),
Run Code Online (Sandbox Code Playgroud)

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

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

更新 这是您要求的示例。

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

我们的导航方式是,当我们按下一个选项卡项时,我们使用 onTabTapped 函数设置它的索引。


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'))
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)