Skip to content

Instantly share code, notes, and snippets.

@teerasej
Created February 15, 2021 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teerasej/0fd547cb486871b4a4e5e16f0544ef49 to your computer and use it in GitHub Desktop.
Save teerasej/0fd547cb486871b4a4e5e16f0544ef49 to your computer and use it in GitHub Desktop.
Form example with saved and validation
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Nextflow Flutter Note App',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Nextflow Flutter Note App'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _formKey = GlobalKey<FormState>();
String _inputMessage = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
padding: EdgeInsets.all(10),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('ข้อความ'),
SizedBox(
height: 10,
),
TextFormField(
onSaved: (value) {
_inputMessage = value;
},
validator: (value) {
if (value == null) {
return "กรุณากรอกข้อความ";
} else {
return "";
}
},
),
SizedBox(
height: 10,
),
SizedBox(
width: double.infinity,
child: RaisedButton(
onPressed: () {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
print(_inputMessage);
}
},
child: Text('Submit'),
),
)
],
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment