dart - 按下时更改 float 按钮图标的方法

有什么方法可以在按下时更改 float 按钮的图标? 代码:

var musicButton = new FloatingActionButton(
    onPressed: (){
      if(playerState == PlayerState.playing) {
        //setState(() => child = new Icon(Icons.pause));
        setState((){
          child: new Icon(Icons.play_arrow);
        });
        pause();
      } else if (playerState == PlayerState.paused){
        setState((){
          child: new Icon(Icons.pause);
        });
        play();
      }
    },
    tooltip: 'Play Music',
    child: new Icon(Icons.pause));

最佳答案

您可以只使用状态变量在图标之间切换。

例子:

bool playing = false;

然后

var musicButton = new FloatingActionButton(
    onPressed: (){
      if(playerState == PlayerState.playing) {
        setState((){
          playing = false;
        });
        pause();
      } else if (playerState == PlayerState.paused){
        setState((){
          playing = true;
        });
        play();
      }
    },
    tooltip: 'Play Music',
    child: playing?new Icon(Icons.pause):new Icon(Icons.play_arrow));

希望有所帮助!

https://stackoverflow.com/questions/48677140/

相关文章:

dart - flutter 解析 NetworkImage 时如何捕获引发的异常?

flutter - "Could not connect to lockdownd"尝试运行 flu

flutter - 在屏幕外构建小部件

dart - 如何显示 HTML Assets 文件?

dart - 使用 WidgetsApp 的应用导航示例

flutter - 如何在 flutter 中为下拉弹出窗口设置动态高度

android - 是否可以同时在 iOS 和 Android 模拟器上启用热重载?

dart - Flutter Dismissible 坚持必须从树中删除列表项

dart - 如何在 Flutter 的 ScrollView 中限制滚动距离?

string - 如何在本地保存用户名并在每个页面 flutter 中显示