Flutter 如何在更新子窗口小部件的同时从其子窗口小部件更新父窗口小部件的状态';它的状态如何?

Flutter 如何在更新子窗口小部件的同时从其子窗口小部件更新父窗口小部件的状态';它的状态如何?,flutter,Flutter,我想请你帮忙 下面的示例代码旨在从子窗口小部件更新父窗口小部件的状态,同时也更新子窗口小部件的状态。父窗口小部件的文本值将更新,同时更改子窗口小部件按钮的颜色 import 'package:flutter/material.dart'; void main() => runApp(AppIndex()); class AppIndex extends StatelessWidget { @override Widget build(BuildContext context)

我想请你帮忙

下面的示例代码旨在从子窗口小部件更新父窗口小部件的状态,同时也更新子窗口小部件的状态。父窗口小部件的文本值将更新,同时更改子窗口小部件按钮的颜色

import 'package:flutter/material.dart';

void main() => runApp(AppIndex());

class AppIndex extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: ParentWidget());
  }
}

class ParentWidget extends StatefulWidget {
  @override
  _ParentWidgetState createState() => _ParentWidgetState();
}

class _ParentWidgetState extends State<ParentWidget> {
  String _textValue = 'Old Value';

callback(newValue){
  setState(() {
    _textValue = newValue;
  });
}

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Text(_textValue),
        ChildWidget(),
      ],
    );
  }
}

class ChildWidget extends StatefulWidget {
  String textValue;
  Function callback;

  ChildWidget({this.textValue, this.callback});

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

class _ChildWidgetState extends State<ChildWidget> {
  bool buttonState = false;

  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      color: buttonState == true ? Colors.greenAccent : Colors.redAccent,
      child: Text('Update State'),
      onPressed: () {
        setState(() {
          if (buttonState == false) {
            buttonState = true;
          } else if (buttonState == true) {
            buttonState = false;
          }
        });

        widget.callback('New Value');
      },
    );
  }
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(AppIndex());
类AppIndex扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回MaterialApp(主页:ParentWidget());
}
}
类ParentWidget扩展了StatefulWidget{
@凌驾
_ParentWidgetState createState()=>\u ParentWidgetState();
}
类_ParentWidgetState扩展状态{
字符串_textValue='旧值';
回调(newValue){
设置状态(){
_textValue=newValue;
});
}
@凌驾
小部件构建(构建上下文){
返回列(
儿童:[
文本(_textValue),
ChildWidget(),
],
);
}
}
类ChildWidget扩展StatefulWidget{
字符串文本值;
函数回调;
ChildWidget({this.textValue,this.callback});
@凌驾
_ChildWidgetState createState()=>\u ChildWidgetState();
}
类_ChildWidgetState扩展状态{
bool buttonState=false;
@凌驾
小部件构建(构建上下文){
返回上升按钮(
color:buttonState==true?Colors.greenAccent:Colors.redAccent,
子项:文本(“更新状态”),
已按下:(){
设置状态(){
如果(buttonState==false){
buttonState=true;
}否则如果(buttonState==true){
buttonState=false;
}
});
回调(“新值”);
},
);
}
}
到目前为止,它能够做的是更新子小部件的颜色/状态,然而,父小部件似乎没有更新其状态。我的原始代码(这是从中提取的)实际上只更新父对象的状态,而不同时更新父对象和子对象(相同的概念和关注点)

上面的示例代码是我在这里提问之前的初步研究结果的派生

我在想,我的代码结构一定有问题,或者这在基于我如何制作的颤振架构中是不可能的。如果可能的话,你能给我一些同样行为的建议吗?(更新父状态和子状态)


很抱歉,我不熟悉这种编程和体系结构。

欢迎使用Flatter!看起来您没有将回调传递到子小部件中。当实例化ChildWidget时,您应该执行
新建ChildWidget(回调:this.callback)除此之外,在我扫描之后,我没有发现您的代码有任何错误,看起来您已经正确设置了它。您还需要将该回调函数向下传播到子小部件中的build方法。现在,在有状态小部件中定义了
函数回调
,但在
状态
中,需要再次定义该回调,并将值从有状态小部件传递到
createState方法中的实际状态类

另外,值得注意的是,您应该尽最大努力避免使用
函数
类型,因为它实际上没有提供函数将返回什么或它将作为什么参数的信息。为此,Dart使用
typedef
关键字。例如,对于接受日期字符串并返回日期的函数(这是一个有点做作的示例,假定Darts内置了对日期的出色支持),您可以执行以下操作:

typedef日期字符串toDateParser(字符串日期字符串)

然后,在类中,可以使用
StringToDateParser
作为类型

import 'package:flutter/material.dart';

void main() => runApp(AppIndex());

class AppIndex extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: ParentWidget());
  }
}

class ParentWidget extends StatefulWidget {
  @override
  _ParentWidgetState createState() => _ParentWidgetState();
}

class _ParentWidgetState extends State<ParentWidget> {
  String _textValue = 'Old Value';

  callback(newValue){
    setState(() {
      _textValue = newValue;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Text(_textValue),
        ChildWidget(callback: this.callback, this._text),
      ],
    );
  }
}

class ChildWidget extends StatefulWidget {
  String textValue;
  Function callback;

  ChildWidget({this.textValue, this.callback});

  @override
  _ChildWidgetState createState() => _ChildWidgetState(callback: this.callback, textValue: this.textValue);
}

class _ChildWidgetState extends State<ChildWidget> {
  bool buttonState = false;
  Function callback;
  String textValue;
  _ChildWidgetState({this.callback, this.textValue});

  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      color: buttonState == true ? Colors.greenAccent : Colors.redAccent,
      child: Text('Update State'),
      onPressed: () {
        setState(() {
          if (buttonState == false) {
            buttonState = true;
          } else if (buttonState == true) {
            buttonState = false;
          }
        });

        this.callback('New Value');
      },
    );
  }
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(AppIndex());
类AppIndex扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回MaterialApp(主页:ParentWidget());
}
}
类ParentWidget扩展了StatefulWidget{
@凌驾
_ParentWidgetState createState()=>\u ParentWidgetState();
}
类_ParentWidgetState扩展状态{
字符串_textValue='旧值';
回调(newValue){
设置状态(){
_textValue=newValue;
});
}
@凌驾
小部件构建(构建上下文){
返回列(
儿童:[
文本(_textValue),
ChildWidget(回调:this.callback,this.\u text),
],
);
}
}
类ChildWidget扩展StatefulWidget{
字符串文本值;
函数回调;
ChildWidget({this.textValue,this.callback});
@凌驾
_ChildWidgetState createState()=>\u ChildWidgetState(回调:this.callback,textValue:this.textValue);
}
类_ChildWidgetState扩展状态{
bool buttonState=false;
函数回调;
字符串文本值;
_ChildWidgetState({this.callback,this.textValue});
@凌驾
小部件构建(构建上下文){
返回上升按钮(
color:buttonState==true?Colors.greenAccent:Colors.redAccent,
子项:文本(“更新状态”),
已按下:(){
设置状态(){
如果(buttonState==false){
buttonState=true;
}否则如果(buttonState==true){
buttonState=false;
}
});
回调('New Value');
},
);
}
}

非常感谢您!那很快。我现在意识到了我的错误,你知道,我发誓同样的事情在我的实际代码(即概念)中似乎不起作用。您的推荐让我再次查看了它(在已经进行了大量查找之后),并发现它是有效的。希望这不会太多,但您能通过您提到的状态传播给我更多启发吗?:)哈哈哈,这就是编程的诅咒,代码永远不会工作,直到别人看到它,然后噗!它神奇地开始工作:)祝你好运。飞起来很神奇。当然,让我们