【问题标题】:Flutter: Using image instead of iconFlutter:使用图像而不是图标
【发布时间】:2021-07-09 10:37:40
【问题描述】:

我有一个无线电流播放器的示例代码。 该代码提供了一个图标来表示其状态(暂停或播放)。 我想显示图像而不是图标。

作为一个新手/菜鸟,我被卡住了。 有人能把我推向正确的方向吗?

这是当前代码:

...........    
default:
                        return Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                              IconButton(
                                  onPressed: () async {
                                    print("button press data: " +
                                        snapshot.data.toString());
                                    await _flutterRadioPlayer.playOrPause();
                                  },
                                  icon: snapshot.data ==
                                          FlutterRadioPlayer
                                              .flutter_radio_playing
                                      ? Icon(Icons.pause)
                                      : Icon(Icons.play_arrow))
                            ]);
                        break;
                    }
...............

简而言之,我想使用图像,“pause.png”用于 Icons.pause,“play.png”用于 Icons.play_arrow。

【问题讨论】:

    标签: image flutter icons


    【解决方案1】:

    删除那个 IconButton 东西并将其用作 Future Builder 的子项 未来的建造者将返回这个

    Image.asset('pause.png'):Image.asset('play.png')
    

    【讨论】:

      【解决方案2】:

      图标是 Widget 类型,因此您可以传递例如 Image.asset([path to asset]) 而不是 Icon()

      【讨论】:

        【解决方案3】:

        这里是例子

        return Row(
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    mainAxisAlignment: MainAxisAlignment.center,
        
                                    // replace Icon button to this custom button and wrap with Inkwell 
                                    children: <Widget>[
                                      InkWell(
                                          onTap: () async {
                                            print("button press data: " +
                                                snapshot.data.toString());
                                            await _flutterRadioPlayer.playOrPause();
                                          },
                                          child : snapshot.data ==
                                                  FlutterRadioPlayer
                                                      .flutter_radio_playing
                                              ? Image.asset("image path")
                                              : Image.asset("image path "))
                                    ]);
                                break;
                            }
        
        
        
        

        【讨论】:

          猜你喜欢
          • 2016-10-29
          • 2012-02-24
          • 2021-05-27
          • 2012-10-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-26
          • 2020-07-10
          相关资源
          最近更新 更多