【问题标题】:BottomSheet value does not update using button flutterBottomSheet 值不使用按钮颤动更新
【发布时间】:2021-04-17 13:16:19
【问题描述】:

我有一个底部工作表,其中包含一组按钮。我使用按钮更改pinString 的值和显示pinString 的文本。单击按钮时,文本的值不会更新。如何解决这个问题

  showNumberPad(BuildContext context) {
    showModalBottomSheet(
      context: context,
      builder: (context) {
        return StatefulBuilder(builder: (context, setState) {
          return Container(
            height: 550.0,
            child: Column(
              children: <Widget>[
                Text(
                  "$pinString",
                ),
                KeyboardNumber(
                  n: 1,
                  onPressed: () {
                     pinIndexSetup("1");
                     setState1() {
                       pinString = "New Pin";
                     }
                  },
                ),
              ],
            ),
          );
        });
      },
    );
  }

KeyboardNumber 是一个自定义的有状态小部件,我想将 onPressed 作为参数传递。 键盘编号代码:


class KeyboardNumber extends StatefulWidget {
  final int n;
  final onPressed;

  const KeyboardNumber({Key key, this.n, this.onPressed}) : super(key: key);

  @override
  _KeyboardNumberState createState() => _KeyboardNumberState();
}

class _KeyboardNumberState extends State<KeyboardNumber> {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 60.0,
      height: 60.0,
      decoration: BoxDecoration(
        color: teal2,
        borderRadius: BorderRadius.all(
          Radius.circular(10),
        ),
      ),
      alignment: Alignment.center,
      child: FlatButton(
        padding: EdgeInsets.all(8.0),
        onPressed: widget.onPressed,
        height: 90.0,
        child: Text(
          "${widget.n}",
          textAlign: TextAlign.center,
          style: TextStyle(
            fontSize: 16,
            color: Colors.white,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter button bottom-sheet statefulwidget


    【解决方案1】:

    请注意,新的setState 将覆盖您的主小部件setState,但请确保您可以重命名它以便能够设置父小部件和模式的状态

    这是更新后的代码。

    showNumberPad(BuildContext context) {
        showModalBottomSheet(
          context: context,
          builder: (context) {
            return StatefulBuilder(builder: (context, SetState1) {
              return Container(
                height: 550.0,
                child: Column(
                  children: <Widget>[
                    Text(
                      "$pinString",
                    ),
                    FlatButton(
                      child: Text("Update"),
                      onPressed: () {
                        SetState1(() {
                          pinString = "New Pin";
                        });
                      },
                    ),
                  ],
                ),
              );
            });
          },
        );
      }
    

    【讨论】:

    • 我使用自定义 Widget 而不是 FlatButton,它显示相同的错误“未引用声明 'setState1'。尝试删除 'setState1' 的声明”
    【解决方案2】:

    你在另一个状态下定义pinString并在bottomeSheet状态改变它,你必须在这一行定义它:

        showModalBottomSheet(
          context: context,
          builder: (context) {
            String pinString = 'hi';
            return StatefulBuilder(builder: (BuildContext context, StateSetter setState){
              return Container(
                height: 550.0,
                child: Column(
                  children: <Widget>[
                    Text(
                      "$pinString",
                    ),
                    FlatButton(
                      child: Text("Update"),
                      onPressed: () {
                        setState(() => pinString = 'new');
                      },
                    ),
                  ],
                ),
              );
            });
          },
        );
      }
    

    【讨论】:

    • 此错误显示 ``` 未引用声明 'setState'。尝试删除“setState”的声明。 ```
    • 我测试它并且没有错误地做这个把戏,你的颤振版本是什么
    • @Nehal 请检查我的答案。我测试了它和它的工作原理。
    • @Nehal The declaration 'setState' isn't referenced. 出现是因为您试图在无状态小部件中调用 setState,请确保您使用的是有状态的,请查看这篇文章 stackoverflow.com/questions/49597189/…
    • 我使用自定义 Widget 而不是 FlatButton,它显示相同的错误“未引用声明 'setState1'。尝试删除 'setState1' 的声明”。我已经更新了我的问题
    猜你喜欢
    • 2020-03-14
    • 2020-11-22
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    相关资源
    最近更新 更多