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

时间:2019-06-04 06:44:44

标签: flutter dart

我在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

但没有成功

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

3 个答案:

答案 0 :(得分:1)

对我来说,上面提到的 ImageIcon 选项不起作用。我使用 Image.asset

BottomNavigationBarItem(
              title:Text(AppLocalizations.of(context).converter), icon: Image.asset(
            "images/convertericon.png",
          )),

答案 1 :(得分:0)

在这里,您可以使用资产中的图标

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函数设置其索引。当索引更改时,视图也相应更改,因为我们指示主体显示子级的当前索引


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'))
        ],
      ),
    );
  }
}

答案 2 :(得分:0)

在Flutter中,您可以使用名为ImageIcon的小部件从图像创建图标。您只需要提供ImageProvider的实例,例如AssetImageNetworkImageMemoryImageResizeImage。以下示例使用AssetImage加载图像。假设您有一个副本并将图像加载到pubspec.yaml中,则使用图像资源创建ImageIcon就像下面的代码一样简单。

要创建ImageIcon,您需要调用构造函数

  const ImageIcon(
    this.image, {
    Key key,
    this.size,
    this.color,
    this.semanticLabel,
  })

您只需要传递图像。所有命名参数都是可选的。

要将图像设置为显示图标,您需要传递一个ImageProvider实例。为此,您需要创建ImageProvider派生的任何类的实例,例如AssetImage

 ImageIcon(
   AssetImage('assets/images/pikachu.png'),
   size: 150,
   color: Colors.yellow,
 )

使用 ImageIcon 作为图标示例

BottomNavigationBarItem(
    title: Text(“Cartoon”),
    icon: ImageIcon(
      AssetImage('assets/images/pikachu.png'),
    )
)

您也可以使用此功能

  BottomNavigationBarItem(
      title: Text("Cartoon"),
      icon: Image.asset("assets/images/pikachu.png", height: 30, width: 30, color: Colors.grey))