所有列表项目上的扑改变状态

发布于 2025-01-24 21:34:58 字数 1868 浏览 3 评论 0原文

嘿,我是Flutter的新手,我正在尝试构建一个采访无线电应用程序,

我面临的问题是将音乐从一个站点变为另一个站的状态时,“播放”和图标仍然不会改变以前单击对象,但是新站开始播放

​播放和图标更改应该切换到现在玩的一个,然后从中切换的一个应该显示图标作为其余的

代码

class ItemWidget extends StatefulWidget {
  final Item item;

  ItemWidget({Key? key, required this.item}) : super(key: key);

  @override
  State<ItemWidget> createState() => _ItemWidgetState();
}

class _ItemWidgetState extends State<ItemWidget> {
  static AudioPlayer player = AudioPlayer();
  static String name = "";
  static bool isPlaying = false;
  static String error = '';

  initRadioPlayer(namepassed, url) async {
    try {
      if (name == namepassed) {
        player.stop();
        name = "";
        isPlaying = false;
        setState(() {});
      } else if (name != namepassed) {
        if (isPlaying == true) {
          await player.stop();
        }
        await player.setUrl(url);
        player.play();
        name = namepassed;
        isPlaying = true;
        setState(() {});
      }
    } catch (err) {
      error = err.toString();
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      child: ListTile(
        onTap: () {
          initRadioPlayer(widget.item.name, widget.item.url);
        },
        title: Text(widget.item.name),
        subtitle: name == widget.item.name
            ? isPlaying
                ? Text('Playing')
                : Text('')
            : Text(''),
        trailing: Icon(isPlaying
            ? name == widget.item.name
                ? CupertinoIcons.stop_circle
                : CupertinoIcons.play_circle
            : CupertinoIcons.play_circle),
        textColor: Color.fromARGB(255, 31, 22, 22),
      ),
    );
  }
}

Hey I am a newbie in flutter, I am trying to build an interview radio app

The issue I am facing is when changing state on switching music from one station to another, the "playing" and icon still don't change for the previously clicked object, but the new station starts playing

Here the station has switched but playing has not yet gone

Here the station has switched but playing text has not yet gone, as well as the icon changed

Ideally, I want when switched, the playing and icon change should be switched to the one playing now, and one that's switched from should have icon showing as the rest

My code

class ItemWidget extends StatefulWidget {
  final Item item;

  ItemWidget({Key? key, required this.item}) : super(key: key);

  @override
  State<ItemWidget> createState() => _ItemWidgetState();
}

class _ItemWidgetState extends State<ItemWidget> {
  static AudioPlayer player = AudioPlayer();
  static String name = "";
  static bool isPlaying = false;
  static String error = '';

  initRadioPlayer(namepassed, url) async {
    try {
      if (name == namepassed) {
        player.stop();
        name = "";
        isPlaying = false;
        setState(() {});
      } else if (name != namepassed) {
        if (isPlaying == true) {
          await player.stop();
        }
        await player.setUrl(url);
        player.play();
        name = namepassed;
        isPlaying = true;
        setState(() {});
      }
    } catch (err) {
      error = err.toString();
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      child: ListTile(
        onTap: () {
          initRadioPlayer(widget.item.name, widget.item.url);
        },
        title: Text(widget.item.name),
        subtitle: name == widget.item.name
            ? isPlaying
                ? Text('Playing')
                : Text('')
            : Text(''),
        trailing: Icon(isPlaying
            ? name == widget.item.name
                ? CupertinoIcons.stop_circle
                : CupertinoIcons.play_circle
            : CupertinoIcons.play_circle),
        textColor: Color.fromARGB(255, 31, 22, 22),
      ),
    );
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠我空喜 2025-01-31 21:34:58

我使用了回调函数,并保留了一个人在父母中扮演的状态,就像 ivo Becker建议

void _update(String name, bool isPlaying) {
  setState(() {
    _name = name;
    _isPlaying = isPlaying;
  });
}

这是在父母中进行的,然后将其传递给孩子并从孩子

class ItemWidget extends StatefulWidget {
  final Item item;
  final String name;
  final bool isPlaying;
  Function callback;

那里打来:

widget.callback(station_name, true);

I used a callback function and kept the state of which one is playing in the parent just like Ivo Becker suggested:

void _update(String name, bool isPlaying) {
  setState(() {
    _name = name;
    _isPlaying = isPlaying;
  });
}

This goes in the parent and then is passed on to and called from the child:

class ItemWidget extends StatefulWidget {
  final Item item;
  final String name;
  final bool isPlaying;
  Function callback;

and on tap you call it:

widget.callback(station_name, true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文