Skip to content

Instantly share code, notes, and snippets.

@nemethmik
Created January 19, 2019 07:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nemethmik/8df3743c8eea3c6518b036a38db430e4 to your computer and use it in GitHub Desktop.
Save nemethmik/8df3743c8eea3c6518b036a38db430e4 to your computer and use it in GitHub Desktop.
A self-contained runnable Flutter example of using StatefulBuilder to make a wrapper context for Form.of (line 21) to return the parent form (line 11)
import "package:flutter/material.dart";
//https://github.com/flutter/flutter/issues/26772#issuecomment-455674193
main() {runApp(MaterialApp(home:HomeScreen()));}
class HomeScreen extends StatefulWidget {@override createState() {return _HomeScreen();}}
class _HomeScreen extends State<StatefulWidget> {
String data;
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
@override Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Home")),
body:Form(
key: this.formKey,
child: StatefulBuilder(
builder: (context,setState){return
ListView(
children: <Widget>[
TextFormField(onSaved: (v){this.data = v;}),
RaisedButton(child: Text('Logout'),
onPressed: () {
//formKey.currentState.save();
Form.of(context).save();
print("********** Data=" + this.data);
},),
]
);
},
)
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment