Blog Details

img
Development

Designing a Form Submission Page in Flutter

Spoke Right / 2 Nov, 2023

There are many ways to submit user-input data in Flutter. But the most used way is using TextFields. But there is a drawback of using TextFields it requires controller of the every text field you create in Flutter.  So to Overcome makes the use of Forms. Forms in flutter don’t need any textController to store data. It just needs 1 GlobalKey which is set to FormState.

Note: The below code doesn’t have a submit button as there is no requirement for it here. Readers can add their own functionality to the form and use the below code as a template.

Source Code:

  • Dart




import 'package:flutter/material.dart';

  

void main() => runApp(MyApp());

  

class MyApp extends StatefulWidget {

  @override

  _MyAppState createState() => _MyAppState();

}

  

class _MyAppState extends State {

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      title: "FormValidation",

      home: MyHomePage(),

    );

  }

}

  

class MyHomePage extends StatefulWidget {

  @override

  _MyHomePageState createState() => _MyHomePageState();

}

  

class _MyHomePageState extends State {

  final GlobalKey _formKey = GlobalKey();

  String email = "";

  String password = "";

  

  void _submit() {

    // you can write your

    // own code according to

    // whatever you want to submit;

  }

  

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(

        title: Text("Form Validation"),

      ),

      body: Padding(

        padding: const EdgeInsets.all(16.0),

        child: Column(

          children: [

            Form(

              key: _formKey,

              child: Column(

                children: [

                  TextFormField(

                    decoration: InputDecoration(labelText: 'E-Mail'),

                    keyboardType: TextInputType.emailAddress,

                    onFieldSubmitted: (value) {

                      setState(() {

                        email = value;

                      });

                    },

                    validator: (value) {

                      if (value.isEmpty || !value.contains('@')) {

                        return 'Invalid email!';

                      }

                    },

                  ),

                  // this is where the

                  // input goes

                  TextFormField(

                    decoration: InputDecoration(labelText: 'password'),

                    keyboardType: TextInputType.visiblePassword,

                    obscureText: true,

                    validator: (value) {

                      if (value.isEmpty && value.length < 7) {

                        return 'Invalid password!';

                      }

                    },

                    onFieldSubmitted: (value) {

                      setState(() {

                        password = value;

                      });

                    },

                  ),

                  RaisedButton(

                    onPressed: _submit,

                    child: Text("submit"),

                  ),

                ],

              ),

            ),

            // this is where

            // the form field

            // are defined

            SizedBox(

              height: 20,

            ),

            Column(

              children: [

                email.isEmpty ? Text("No data") : Text(email),

                SizedBox(

                  height: 10,

                ),

                password.isEmpty ? Text("No Data") : Text(password),

              ],

            )

          ],

        ),

      ),

    );

  }

}

Output:

0 comments

Warning: PHP Startup: Unable to load dynamic library 'imagick.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0