【发布时间】:2018-03-14 13:02:05
【问题描述】:
我被困在我的项目中,我在颤振中创建了两个有状态的小部件作为两个不同的 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'),),
),
],
),
),
);
}
}
models.dart
class MyModel {
String name;
String emailId;
String password;
}
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'),),
),
],
),
),
);
}
}
【问题讨论】:
-
我不知道这里有什么问题,你可以简单地导入另一个 dart 文件,然后在另一个小部件中创建一个小部件的实例,如果你添加两者的代码会更好dart 文件,以便了解您要实现的目标,否则您的问题非常笼统且含糊。
-
非常感谢 aziza 我已经编辑了问题以准确显示我正在寻找的内容。但科林杰克逊提出了一个答案。所以我会先尝试一下,看看是否能解决我的问题。
-
Collin 建议的第三个选项在这里应该可以正常工作,您可以简单地将保存在第一页的
TextForms 中的数据作为SecondScreen的构造函数参数传递,然后使用它们来创建SecondScreen中的模型对象。