简体   繁体   English

Flutter,使用布尔值将图标状态从一个小部件更改为另一个

[英]Flutter, change Icon state with boolean from one widget to another

I have two widgets.我有两个小部件。 First is the ParticipantScreen() that displays the second widget in GridView.builder() - ParticipantCard().首先是 ParticipantScreen(),它在 GridView.builder() 中显示第二个小部件 - ParticipantCard()。 Both widgets have mic icons to mute participants.这两个小部件都有麦克风图标,可以让参与者静音。 When I mute local participant I click mic icon on ParticipantScreen(), and regarding to that I want to change mic icon state in ParticipantCard() too to the same boolean value, but only for the first item in list that is used to create GridView with ParticipantCards.当我使本地参与者静音时,我单击 ParticipantScreen() 上的麦克风图标,关于这一点,我也想将 ParticipantCard() 中的麦克风图标状态更改为相同的布尔值,但仅适用于列表中用于创建 GridView 的第一项与参与者卡。 Is it possible to pass boolean value from one widget to another?是否可以将布尔值从一个小部件传递到另一个小部件?

You can use callback function(can be typedef) for this like您可以为此使用回调函数(可以是 typedef)

final Function(bool isMuted) onChanged;

Play with this widget玩这个小部件

class ParentWidget extends StatefulWidget {
  const ParentWidget({Key? key}) : super(key: key);

  @override
  State<ParentWidget> createState() => _ParentWidgetState();
}
class _ParentWidgetState extends State<ParentWidget> {
  bool _isMuted = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Switch(
              value: _isMuted,
              onChanged: (v) {
                setState(() {
                  _isMuted = !_isMuted;
                });
              }),
          ChildC(
            isMuted: _isMuted,
            onChanged: (isMuted) {
              setState(() {
                _isMuted = isMuted;
              });
            },
          ),
        ],
      ),
    );
  }
}

class ChildC extends StatefulWidget {
  final bool isMuted;
  final Function(bool isMuted) onChanged;

  const ChildC({
    Key? key,
    required this.isMuted,
    required this.onChanged,
  }) : super(key: key);

  @override
  State<ChildC> createState() => _ChildCState();
}

class _ChildCState extends State<ChildC> {
  @override
  Widget build(BuildContext context) {
    return Switch(
        value: widget.isMuted,
        onChanged: (v) {
          widget.onChanged(v);
        });
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Flutter 中从另一个有状态小部件更改一个有状态小部件的状态? - How to change state of one Stateful Widget from another Stateful Widget in Flutter? 从另一个小部件 Flutter 更改文本字段的 textSize - Change textSize of textfield from another widget Flutter Flutter - 无法从父小部件更改子状态 - Flutter - Can't change child state from parent widget 如何在 Flutter 中将事件从一个有状态小部件广播到另一个小部件 - How To Broadcast An Event From One Stateful Widget To Another In Flutter Flutter:通过调用 STF 小部件将变量从一个页面发送到另一个页面 - Flutter: Sending variable from one page to another by calling STF widget 从一个片段移动到另一个片段时更改底部图标 - Change bottom icon when move from one fragment to another 如何基于另一个窗口小部件中的动作更新UI或更改窗口小部件中的状态? - How to update UI or change state in a widget based on an action in another widget? 在颤动中单击另一个小部件中的按钮时为一个小部件设置动画 - Animate one widget on clicking button in another widget in flutter 将变量值从 state 传递到另一个小部件 - Passing variable value from state to another widget 从活动手动更改小部件状态 - Manually change widget state from Activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM