Created
February 15, 2021 02:35
-
-
Save teerasej/0fd547cb486871b4a4e5e16f0544ef49 to your computer and use it in GitHub Desktop.
Form example with saved and validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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