【问题标题】:how to get a bool in a stateful widget to change a counter in it's parent stateful widget如何在有状态小部件中获取布尔值以更改其父有状态小部件中的计数器
【发布时间】:2023-01-13 19:57:18
【问题描述】:

我对 flutter 还很陌生,我正在努力解决将变量传递到小部件树的问题。我已经编写了一个非常简单的代码来演示我正在努力实现的目标,我希望有人可以为我拼写出来。

我有一个带有计数器的父有状态小部件:

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

  @override
  State<ParentWidget> createState() => _ParentWidgetState();
}

class _ParentWidgetState extends State<ParentWidget> {
  int Counter = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          children: [
            SizedBox(
              height: 100,
            ),
            Button(),
          ],
        ),
      ),
    );
  }
}

然后我有另一个带有按钮和布尔值的有状态小部件:

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

  @override
  State<Button> createState() => _ButtonState();
}

class _ButtonState extends State<Button> {
  bool buttonPressed = false;
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        setState(() {
          buttonPressed = !buttonPressed;
          print(buttonPressed);
        });
      },
      child: Container(
        color: kWhite,
        height: 50,
        width: 50,
      ),
    );
  }
}

我看过其他一些答案(使用回调?),但我很难理解它的实际工作原理以及我将如何将其实现到我的代码中

如何将 bool 变量向上传递到树中以更改计数器?

非常感谢,任何帮助将不胜感激

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    这是如何使用回调将数据传递给父窗口小部件,

    class ParentWidget extends StatefulWidget {
      const ParentWidget({Key? key}) : super(key: key);
    
      @override
      State<ParentWidget> createState() => _ParentWidgetState();
    }
    
    class _ParentWidgetState extends State<ParentWidget> {
      int Counter = 0;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: Column(
              children: [
                SizedBox(
                  height: 100,
                ),
                Button(
                  onBtnPressed: (val) {
                    print(val);
                  },
                ),
              ],
            ),
          ),
        );
      }
    }
    
    class Button extends StatefulWidget {
      const Button({required this.onBtnPressed, Key? key}) : super(key: key);
    
      final Function onBtnPressed;
    
      @override
      State<Button> createState() => _ButtonState();
    }
    
    class _ButtonState extends State<Button> {
      bool buttonPressed = false;
    
      @override
      Widget build(BuildContext context) {
        return InkWell(
          onTap: () {
            setState(() {
              buttonPressed = !buttonPressed;
              print(buttonPressed);
            });
            widget.onBtnPressed(buttonPressed);
          },
          child: Container(
            color: kWhite,
            height: 50,
            width: 50,
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-25
      • 2019-08-19
      • 2019-10-06
      • 2020-03-18
      • 2021-08-27
      • 2021-06-11
      • 2018-09-01
      • 2020-08-01
      • 2018-10-10
      相关资源
      最近更新 更多