【问题标题】:How to use image icon (from assets) instead of IconData and pagination bottomNavigationBar in Flutter如何在 Flutter 中使用图像图标(来自资产)而不是 IconData 和分页 bottomNavigationBar
【发布时间】:2019-06-04 06:44:44
【问题描述】:

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

我从这里使用了带有 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

但没有成功

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

【问题讨论】:

标签: flutter dart icons imageicon flutter-bottomnavigation


【解决方案1】:

以下是使用资产图标的方法

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

试试这个底部导航栏的例子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'))
        ],
      ),
    );
  }
}

【讨论】:

  • @stackoverflow.com/users/7865296/praneeth-dhanushka-fernando 谢谢,这对我很有帮助,我用它解决了我的问题,你能解释一下分页。如何从我使用的 GitHub 代码中的选项卡打开不同的页面。再次感谢....
  • 如何将颜色设置为默认值,如图像的颜色
  • 您还需要在 pubspec.yaml 文件的 assets 部分列出图片文件,否则 AssetImage 将无法找到它们。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    在 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 作为 icon 示例

    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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-07
      • 2021-10-26
      • 2021-03-30
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      相关资源
      最近更新 更多