HomeMobile DevelopmentFlutter onPress button - Form validator and get value from TextField Flutter onPress button - Form validator and get value from TextField Makarablue June 26, 2022 0 Code :import 'package:flutter/material.dart'; void main() { runApp(MaterialApp( home: RegisterForm(), )); } class RegisterForm extends StatefulWidget { RegisterForm({Key? key}) : super(key: key); @override State createState() => _RegisterFormState(); } class _RegisterFormState extends State { List gender = ["Male", "Female", "Gay"]; String genderSelected = "Male"; GlobalKey _formkey = new GlobalKey(); TextEditingController _emailText = new TextEditingController(); TextEditingController _passwordText = new TextEditingController(); TextEditingController _cPasswordText = new TextEditingController(); String result = ""; @override Widget build(BuildContext context) { return Scaffold( backgroundColor: const Color.fromARGB(255, 202, 235, 247), body: Center( child: buildForm(), )); } Widget buildForm() { return Form( key: _formkey, child: Container( width: MediaQuery.of(context).size.width * 0.9, height: MediaQuery.of(context).size.height * 0.5, decoration: BoxDecoration( color: const Color.fromARGB(255, 235, 248, 255), borderRadius: BorderRadius.circular(15)), child: Padding( padding: const EdgeInsets.all(40), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Center( child: Text( "Register Form", style: TextStyle( color: Colors.lightBlue, fontWeight: FontWeight.bold, fontSize: 20), ), ), TextFormField( controller: _emailText, keyboardType: TextInputType.emailAddress, decoration: const InputDecoration(hintText: "Email"), validator: (val) { if (val!.isEmpty) { return "Email is required"; } }, ), TextFormField( controller: _passwordText, keyboardType: TextInputType.text, obscureText: true, decoration: const InputDecoration(hintText: "Password"), validator: (val) { if (val!.isEmpty) { return "Password is required"; } }, ), TextFormField( controller: _cPasswordText, keyboardType: TextInputType.text, obscureText: true, decoration: const InputDecoration(hintText: "Confirm Password"), validator: (val) { if (val!.isEmpty) { return "Confirm password is required"; } }, ), const SizedBox( height: 10, ), buildGender(), const SizedBox( height: 10, ), buildButton(), Text(result) ], ), ), )); } Row buildButton() { return Row( mainAxisAlignment: MainAxisAlignment.center, children: [ ElevatedButton( onPressed: () => onRegister(), child: const Text( "Register", style: TextStyle(color: Colors.white), )), const SizedBox( width: 10, ), ElevatedButton( style: ElevatedButton.styleFrom(primary: Colors.red), onPressed: () => {}, child: const Text( "Cancel", style: TextStyle(color: Colors.white), )), ], ); } Row buildGender() { return Row( children: [ const Text( "Gender", style: TextStyle(), ), const SizedBox( width: 10, ), DropdownButton( value: genderSelected, items: gender.map((item) { return DropdownMenuItem( child: Text(item), value: item, ); }).toList(), onChanged: (getItem) { setState(() { genderSelected = getItem.toString(); }); }) ], ); } onRegister() { //Example to validate from if (_formkey.currentState!.validate()) { // } else { // } //Example to get value from email field setState(() { result = _emailText.text; }); } } Tags Coding Flutter Mobile Development Newer Older