如何在flutter中访问另一个有状态窗口小部件中的一个有状态窗口小部件中创建的对象

Mah*_*ahi 19 dart flutter

我被困在我的项目中,我有两个有状态的小部件在颤动中创建为两个不同的dart文件.现在,我必须访问第二个小部件中第一个小部件中创建的对象的实例,但我不确定在创建小部件时如何在颤动时执行此操作.

我想到的一个可能的解决方案是在一个dart文件中声明两个小部件而不是两个布局的两个dart文件,但我很好奇我们是否可以通过在两个单独的dart文件中声明来完成它.

我发布文件只是为了重新创建问题.

main.dart

 import 'package:flutter/material.dart';
 import 'package:untitled2/models.dart';
 import 'package:untitled2/secondwidget.dart';

void main() {
 runApp(new MaterialApp(
     home: new MyApp(),
 ),);
}

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
  }

      class _MyAppState extends State<MyApp> {
 final TextEditingController _nameController = new TextEditingController();
 final TextEditingController _emailIdController = new 
 TextEditingController();
 final TextEditingController _passwordController = new 
 TextEditingController();
 final TextEditingController _confirmPasswordController = new 
 TextEditingController();

 MyModel myModel = new MyModel();

 @override
 Widget build(BuildContext context) {
  return new MaterialApp(
  home: new Scaffold(
    body: new ListView(

      children: <Widget>[
        new Container(
          child: new TextFormField(
            decoration: new InputDecoration(
              labelText: 'Enter your Name'
            ),
            controller: _nameController,
            onSaved: (String value){myModel.name = value;} ,
          ),
        ),
        new Container(
          child: new TextFormField(
            decoration: new InputDecoration(
                labelText: 'EmailId'
            ),
              controller: _emailIdController,
              onSaved: (String value){myModel.emailId = value;}
          ),
        ),
        new Container(
          child: new TextFormField(
            decoration: new InputDecoration(
                labelText: 'Password'
            ),
              controller: _passwordController,
              onSaved: (String value){myModel.password = value;}
          ),
        ),
        new Container(
          child: new TextFormField(
            decoration: new InputDecoration(
                labelText: 'Confirm Password'
            ),
            controller: _confirmPasswordController,

          ),
        ),
        new Container(
          child: new FlatButton(
            onPressed: ((){

              Navigator.push(context, new MaterialPageRoute(builder: (_) => 
         new SecondScreen(),),);

            }),
            child: new Text('Save'),),
        ),



      ],


      ),
     ),
    );
   }
  }
Run Code Online (Sandbox Code Playgroud)

models.dart

class MyModel {

String name;
String emailId;
String password;

  }
Run Code Online (Sandbox Code Playgroud)

secondwidget.dart

 import 'package:flutter/material.dart';


 class SecondScreen extends StatefulWidget {
 @override
 _SecondScreenState createState() => new _SecondScreenState();
}

class _SecondScreenState extends State<SecondScreen> {
@override
Widget build(BuildContext context) {
return new MaterialApp(
  home: new Scaffold(
    body: new ListView(

      children: <Widget>[
        new Container(
          child: new TextFormField(
            decoration: new InputDecoration(
                labelText: 'Enter address'
            ),
          ),
        ),
        new Container(
          child: new TextFormField(
            decoration: new InputDecoration(
                labelText: 'Address Line 2'
            ),
          ),
        ),
        new Container(
          child: new TextFormField(
            decoration: new InputDecoration(
                labelText: 'Address Line 3'
            ),
          ),
        ),
        new Container(
          child: new TextFormField(
            decoration: new InputDecoration(
                labelText: 'POST CODE'
            ),
          ),
        ),

        new Container(
          child: new TextFormField(
            decoration: new InputDecoration(
                labelText: 'Mobile Number'
            ),
          ),
        ),
        new Container(
          child: new FlatButton(
              onPressed: ((){


                //I want to push the data captured from main.dart and from 
                            //secondwidget.dart into database
     // I want to use the instance of MyModel from main.dart here to save 
      // the data captured from the first screen and this one into database

              }),
              child: new Text('Save'),),
        ),


      ],


    ),
  ),
);
}
}
Run Code Online (Sandbox Code Playgroud)

Col*_*son 41

根据您的使用情况,有很多方法可以做到这一点.以下是一些选项:

  1. 您可以将创建的对象公开为您的公共成员State.然后使用currentStatea GlobalKeyin 的属性State来获取对另一个的引用State.现在,您可以通过公共成员访问创建的对象.(注意:此模式限制了您的可测试性和封装State,因此请谨慎使用.)
  2. 这两个小部件都可以有一个扩展的祖先小部件,InheritedWidget用于查找创建的对象.
  3. 两个小部件都可以在它们的构造函数中传递模型参数,例如a ValueNotifier.他们可以使用此对象来读取和写入值.

如果您详细了解您的用例,我们可以帮助您选择一个有意义的模式.

以下是一些实现选项#1的代码.

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

final key = new GlobalKey<MyStatefulWidget1State>();

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        body: new Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            new MyStatefulWidget1(key: key),
            new MyStatefulWidget2(),
          ],
        ),
      ),
    );
  }
}

class MyStatefulWidget1 extends StatefulWidget {
  MyStatefulWidget1({ Key key }) : super(key: key);
  State createState() => new MyStatefulWidget1State();
}

class MyStatefulWidget1State extends State<MyStatefulWidget1> {
  String _createdObject = "Hello world!";
  String get createdObject => _createdObject;

  @override
  Widget build(BuildContext context) {
    return new Center(
      child: new Text(_createdObject),
    );
  }
}

class MyStatefulWidget2 extends StatefulWidget {
  State createState() => new MyStatefulWidget2State();
}

class MyStatefulWidget2State extends State<MyStatefulWidget2> {
  String _text = 'PRESS ME';

  @override
  Widget build(BuildContext context) {
    return new Center(
      child: new RaisedButton(
        child: new Text(_text),
        onPressed: () {
          setState(() {
            _text = key.currentState.createdObject;
          });
        },
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


apt*_*tik 10

您可以在构造SecondScreen窗口小部件时传递模型.

  1. 将模型添加到SecondScreen构造函数:

    class SecondScreen extends StatefulWidget {
      final MyModel myModel;
    
      SecondScreen(MyModel myModel, {Key key}):
          super(key: key);
    ...
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. SecondScreen在main.dart中构造时传递模型

    Navigator.push(context, new MaterialPageRoute(builder: (_) => 
      new SecondScreen(model)));
    
    Run Code Online (Sandbox Code Playgroud)
  3. 现在您可以访问_SecondScreenStatevia中的模型widget.myModel


Hal*_*ynh 6

这是我将参数传递给子小部件的方式。

第一个小部件飞镖文件

class FirstWidget extends StatefulWidget {
    _FirstWidgetState createState() => _FirstWidgetState() 
}

class _FirstWidgetState extends State<FirstWidget> {
    String param = "My parameter";
    @override
    Widget build(BuildContext context) {
        return Container(
                    child: 
                        SecondWidget(param), //pass parmater here
                );
    }
}
Run Code Online (Sandbox Code Playgroud)

第二个小部件飞镖文件

class SecondWidget extends StatefulWidget {
    final String param; 
    SecondWidget(this.param); //passed paramter
    _SecondWidgetState createState() => _SecondWidgetState() 
}

class _SecondWidgetState extends State<SecondWidget> {
    @override
    Widget build(BuildContext context) {
        return Container(
                    child: 
                        Text(widget.param), //output paramter
                );
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 最简单的解决办法,非常感谢! (3认同)