在父窗口小部件中访问子窗口小部件的变量(带有Dart的Flutter)

时间:2019-09-29 22:21:28

标签: flutter dart global-variables scoped-model

我有一个按钮,当按下该按钮时,将打开一个模态底页。该工作表具有一个表单小部件,该部件需要几个文本字段和一个图像(来自画廊/相机)。对于此图像输入,我创建了另一个有状态的小部件,该小部件在上一个视图(模式表)中称为。 现在,通过用户接收的图像文件被设置在子状态小部件中的变量中。我的问题是,如何在父窗口小部件中访问此变量(子窗口小部件中的File对象)?

请参考以下代码:

底页:(请参阅在其中调用子小部件的注释。)

        context: _scaffoldKey.currentContext,
        builder: (BuildContext context) {
          return Scaffold(
              backgroundColor: Colors.white,
              appBar: AppBar(
                elevation: 0.0,
                automaticallyImplyLeading: false,
                backgroundColor: Colors.white,
                title: Center(
                  child: _formTitleWidget(),
                ),
              ),
              body: Container(
              height: MediaQuery.of(context).size.height* 0.5,
              margin: EdgeInsets.all(MediaQuery
                  .of(context)
                  .copyWith()
                  .size
                  .width * 0.05),
              child: Form(
                key: _addChildFormKey,
                child: SingleChildScrollView(
                  child: Container(
                    height: MediaQuery.of(context).size.height* 0.4,
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        // Calling the child widget here - where 'image' variable is set
                        AddChildView(),
                        Container(
                          height: MediaQuery.of(context).size.height* 0.4,
                          width:  MediaQuery.of(context).size.width* 0.65,
                          child: Column(
                            children: [
                              _childNameInput(),
                              _childBirthDateInput(),
                              _childHeightInput(),
                              _childWeightInput(),
                              _addChildWithInfo()
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            )
          );
        }```

2 个答案:

答案 0 :(得分:0)

如果您不使用状态管理解决方案,则必须使用回调。

在父级中创建一个变量。 创建一个使用值的方法,并将其分配给刚创建的变量。

创建一个最终的Function并将其添加到您孩子的构造函数中。 现在,当您在父级中实例化“子级”小部件时,它将接受您刚刚创建的方法。

在适当的时候在孩子中运行该功能。

collapse

答案 1 :(得分:0)

class someChildWidget extends StatefulWidget {
  
  String someValue ;  // this is the value that should be accesible from a parent widget
  
  @override
  _someChildWidgetState createState() => _someChildWidgetState();
}

class _someChildWidgetState extends State<someChildWidget> {
  .
  .
  .
  
    metodInTheChild(String something) {
    setState(() {
       widget.someValue = something;
    
      });
   }
  }
  
  
  
  
class parentWidget extends StatefulWidget {
  @override
  _someChildWidgetState createState() => _someChildWidgetState(esRequerido);
}

class _parentWidgetState extends State<parentWidget> {
someChildWidget scw= someChildWidget();

    .
    .
    .
     metodInTheParent() {
      String value=swc.someValue; // now here is where you can access the value of the child widget
      
  }