Class 如何将数据从TextFormField传递到Flatter中的列表?
我有以下工作代码:Class 如何将数据从TextFormField传递到Flatter中的列表?,class,dart,flutter,Class,Dart,Flutter,我有以下工作代码: class Submit extends StatefulWidget { @override _SubmitState createState() => new _SubmitState(); } class _SubmitState extends State<Submit> { final scaffoldKey = new GlobalKey<ScaffoldState>(); final formKey = new G
class Submit extends StatefulWidget {
@override
_SubmitState createState() => new _SubmitState();
}
class _SubmitState extends State<Submit> {
final scaffoldKey = new GlobalKey<ScaffoldState>();
final formKey = new GlobalKey<FormState>();
final myController = new TextEditingController();
double _w;
@override
void dispose() {
// Clean up the controller when the Widget is removed from the Widget tree
myController.dispose();
super.dispose();
}
void _submit() {
final form = formKey.currentState;
if (form.validate()) {
form.save();
_performSubmit();
}
}
void _performSubmit() {
final double data = double.parse(myController.text)*(2);
var route = new MaterialPageRoute(builder: (BuildContext context) =>
new NextPage(w1: data.toStringAsFixed(3)),
);
Navigator.of(context).push(route);
}
@override
Widget build(BuildContext context) {
void _restart() {
Navigator.of(context).push(new MaterialPageRoute(
builder: (BuildContext context) => new HomePage()));
}
return new Scaffold(
key: scaffoldKey,
appBar: new AppBar(
actions: <Widget>[
],
title: new Text('Next Page'),
),
body: new Form(
key: formKey,
child: new Column(
children: [
new TextFormField(
controller: myController,
keyboardType: TextInputType.numberWithOptions(decimal: true),
onSaved: (val) => _w = double.parse(val),
),
new RaisedButton(
onPressed: _submit,
child: new Text(
'Next Page',
),
)
],
),
),
);
}
}
class NextPage extends StatefulWidget {
final String w1;
NextPage({Key key, this.w1}) : super (key: key);
@override
_NextPageState createState() => _NextPageState();
}
class _NextPageState extends State<NextPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: new Text("Second page")
),
body: new Text("${widget.w1}")
);
}
}
试试这个
class Submit extends StatefulWidget {
@override
_SubmitState createState() => new _SubmitState();
}
class _SubmitState extends State<Submit> {
final scaffoldKey = new GlobalKey<ScaffoldState>();
final formKey = new GlobalKey<FormState>();
final myController = new TextEditingController();
double _w;
@override
void dispose() {
// Clean up the controller when the Widget is removed from the Widget tree
myController.dispose();
super.dispose();
}
void _submit() {
final form = formKey.currentState;
if (form.validate()) {
form.save();
_performSubmit();
}
}
void _performSubmit() {
final double data = double.parse(myController.text) * (2);
var route = new MaterialPageRoute(
builder: (BuildContext context) => new NextPage(
w1: new Entry(
'First',
<Entry>[new Entry('$data')],
),
),
);
Navigator.of(context).push(route);
}
@override
Widget build(BuildContext context) {
void _restart() {
// Navigator.of(context).push(new MaterialPageRoute(
// builder: (BuildContext context) => new HomePage()));
}
return new Scaffold(
key: scaffoldKey,
appBar: new AppBar(
actions: <Widget>[],
title: new Text('Next Page'),
),
body: new Form(
key: formKey,
child: new Column(
children: [
new TextFormField(
controller: myController,
keyboardType: TextInputType.numberWithOptions(decimal: true),
onSaved: (val) => _w = double.parse(val),
),
new RaisedButton(
onPressed: _submit,
child: new Text(
'Next Page',
),
)
],
),
),
);
}
}
class NextPage extends StatefulWidget {
final Entry w1;
NextPage({Key key, this.w1}) : super(key: key);
@override
_NextPageState createState() => _NextPageState();
}
class _NextPageState extends State<NextPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(title: new Text("Second page")),
body: new EntryItem(widget.w1),
);
}
}
class Entry {
Entry(this.title, [this.children = const <Entry>[]]);
final String title;
final List<Entry> children;
}
class EntryItem extends StatelessWidget {
const EntryItem(this.entry);
final Entry entry;
Widget _buildTiles(Entry root) {
if (root.children.isEmpty)
return new ListTile(
title: new Text(root.title));
return new ExpansionTile(
key: new PageStorageKey<Entry>(root),
title: new Text(root.title,),
children: root.children.map(_buildTiles).toList(),
);
}
@override
Widget build(BuildContext context) {
return _buildTiles(entry);
}
}
类提交扩展StatefulWidget{
@凌驾
_SubmitState createState()=>new_SubmitState();
}
类_SubmitState扩展状态{
最终脚手架键=新的GlobalKey();
final formKey=new GlobalKey();
final myController=新文本编辑控制器();
双w;
@凌驾
无效处置(){
//当小部件从小部件树中移除时,清理控制器
myController.dispose();
super.dispose();
}
作废(提交){
最终形式=formKey.currentState;
if(form.validate()){
form.save();
_performSubmit();
}
}
void _performSubmit(){
最终双精度数据=double.parse(myController.text)*(2);
var路线=新材料路线(
生成器:(BuildContext上下文)=>新建下一页(
w1:新条目(
“第一”,
[新条目(“$data”)],
),
),
);
导航器.of(上下文).push(路由);
}
@凌驾
小部件构建(构建上下文){
void_restart(){
//导航器.of(上下文).push(新材料路线)(
//生成器:(BuildContext context)=>new HomePage());
}
归还新脚手架(
钥匙:脚手架钥匙,
appBar:新的appBar(
行动:[],
标题:新文本(“下一页”),
),
正文:新表格(
key:formKey,
子:新列(
儿童:[
新TextFormField(
控制器:myController,
键盘类型:TextInputType.numberWithOptions(十进制:true),
onSaved:(val)=>\u w=double.parse(val),
),
新升起的按钮(
按下按钮:_提交,
儿童:新文本(
“下一页”,
),
)
],
),
),
);
}
}
类NextPage扩展StatefulWidget{
最终条目w1;
下一页({Key-Key,this.w1}):超级(Key:Key);
@凌驾
_NextPageState createState()=>NextPageState();
}
类_下一个妊娠期扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:new appBar(标题:新文本(“第二页”),
正文:新条目项(widget.w1),
);
}
}
班级报名{
条目(this.title[this.children=const[]);
最后的字符串标题;
最后儿童名单;
}
类EntryItem扩展了无状态小部件{
const EntryItem(此项);
最终入围;
Widget\u buildTiles(条目根){
if(root.children.isEmpty)
返回新的ListTile(
标题:新文本(root.title));
返回新的扩展文件(
密钥:新页面存储密钥(根),
标题:新文本(root.title,),
children:root.children.map(_buildTiles.toList(),
);
}
@凌驾
小部件构建(构建上下文){
返回(条目);;
}
}
这里的代码太多了。你能提供一个最小的函数示例吗?@RémiRousselet我已经编辑了这篇文章,并在不破坏它的情况下尽可能地最小化了代码。请注意,第二个代码是具有可展开列表的第一个代码
class Submit extends StatefulWidget {
@override
_SubmitState createState() => new _SubmitState();
}
class _SubmitState extends State<Submit> {
final scaffoldKey = new GlobalKey<ScaffoldState>();
final formKey = new GlobalKey<FormState>();
final myController = new TextEditingController();
double _w;
@override
void dispose() {
// Clean up the controller when the Widget is removed from the Widget tree
myController.dispose();
super.dispose();
}
void _submit() {
final form = formKey.currentState;
if (form.validate()) {
form.save();
_performSubmit();
}
}
void _performSubmit() {
final double data = double.parse(myController.text) * (2);
var route = new MaterialPageRoute(
builder: (BuildContext context) => new NextPage(
w1: new Entry(
'First',
<Entry>[new Entry('$data')],
),
),
);
Navigator.of(context).push(route);
}
@override
Widget build(BuildContext context) {
void _restart() {
// Navigator.of(context).push(new MaterialPageRoute(
// builder: (BuildContext context) => new HomePage()));
}
return new Scaffold(
key: scaffoldKey,
appBar: new AppBar(
actions: <Widget>[],
title: new Text('Next Page'),
),
body: new Form(
key: formKey,
child: new Column(
children: [
new TextFormField(
controller: myController,
keyboardType: TextInputType.numberWithOptions(decimal: true),
onSaved: (val) => _w = double.parse(val),
),
new RaisedButton(
onPressed: _submit,
child: new Text(
'Next Page',
),
)
],
),
),
);
}
}
class NextPage extends StatefulWidget {
final Entry w1;
NextPage({Key key, this.w1}) : super(key: key);
@override
_NextPageState createState() => _NextPageState();
}
class _NextPageState extends State<NextPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(title: new Text("Second page")),
body: new EntryItem(widget.w1),
);
}
}
class Entry {
Entry(this.title, [this.children = const <Entry>[]]);
final String title;
final List<Entry> children;
}
class EntryItem extends StatelessWidget {
const EntryItem(this.entry);
final Entry entry;
Widget _buildTiles(Entry root) {
if (root.children.isEmpty)
return new ListTile(
title: new Text(root.title));
return new ExpansionTile(
key: new PageStorageKey<Entry>(root),
title: new Text(root.title,),
children: root.children.map(_buildTiles).toList(),
);
}
@override
Widget build(BuildContext context) {
return _buildTiles(entry);
}
}