main.dart
void main() => runApp(DemoApp()); class DemoApp extends StatelessWidget { const DemoApp(); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( resizeToAvoidBottomInset: false, appBar: AppBar( title: const Text("Login form"), ), body: LayoutBuilder( builder: (context, dimensions) { final width = dimensions.maxWidth / 1.5; final height = dimensions.maxHeight / 2; return Center( child: SingleChildScrollView( child: SizedBox( width: width, height: height, child: LoginForm(width), ), ), ); }, ), ), ); } }
login_form.dart
class LoginForm extends StatelessWidget { final _key = GlobalKey<FormState>(); String? _validateEmail(String value) { if (value.isEmpty) { return "Field cannot be empty"; } else { return null; } } String? _validatePassword(String value) { if (value.length < 8) { return "At least 8 chars!"; } else { return null; } } void _login() { if (_key.currentState?.validate() ?? false) { debugPrint("Yay! :)"); } else { debugPrint("Error :("); } } @override Widget build(BuildContext context) { return Form( key: _key, child: Column( mainAxisAlignment: MainAxisAlignment.spaceAround, children: <Widget>[ // Email TextFormField( decoration: const InputDecoration( icon: Icon(Icons.mail), hintText: "Email" ), validator: _validateEmail, ), // Password TextFormField( decoration: const InputDecoration( icon: Icon(Icons.vpn_key), hintText: "Password" ), obscureText: true, validator: _validatePassword, ), // Confirm Button RaisedButton( child: const Text("Login"), onPressed: _login, ), ], ), ); } }
This website and the book are not official Google products. No affiliations are involved. Built with Java 14 and Vert.X
"Flutter and the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC"