Beginners Tutorial: How To Design UI In Flutter

Flutter, an open-source mobile SDK created by Google, is a tool that makes it easy to build UIs for iOS and Android apps. Flutter’s framework provides all the building blocks necessary to create UI widgets: views, navigation, and actions.

This article will discuss how to design UI in a flutter. It’s good if you are already familiar with Android & IOS UI implementation. It’s a mixture of both. The Login Page is a good example, so let’s begin.

Basically, before we know how to design UI in flutter, we need to analyze the requirements (what widgets we need to design the UI)

  • We need the Edit Text to enter the email id & password field.
  • And the second thing we need is one login button.

So, your code should look like this, using the BLoC pattern.

mobile_signin_screen.dart

import 'package:email_validator/email_validator.dart';

import 'package:flutter/cupertino.dart';

import 'package:flutter/material.dart';

import 'package:flutter/services.dart';

import 'package:flutter_bloc/flutter_bloc.dart';

import 'package:flutter_demo_app/Screen/ForgotPassScreen/view/forgot_pass_page.dart';

import 'package:flutter_demo_app/Screen/LoginScreen/cubit/signin_cubit.dart';

import 'package:flutter_demo_app/Screen/SignupScreen/view/signup_page.dart';

import 'package:flutter_demo_app/utils/form_button.dart';

import 'package:flutter_demo_app/utils/form_fields.dart';

import 'package:flutter_svg/svg.dart';

import 'package:font_awesome_flutter/font_awesome_flutter.dart';

import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart';




class MobileSigninScreen extends StatelessWidget {

 MobileSigninScreen({Key key}) : super(key: key);

 final _scaffoldKey = GlobalKey<ScaffoldState>();

 final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

 @override

 Widget build(BuildContext context) {

   return BlocBuilder<SigninCubit, SigninState>(

     builder: (context, state) {

       return WillPopScope(

           onWillPop: () async {

         SystemNavigator.pop();

       },

       child:

         Scaffold(

           key: _scaffoldKey,

           body: ModalProgressHUD(

             inAsyncCall: state.signUpSignStatus == Status.inprogress,

             child: SingleChildScrollView(

               child: Stack(children: [

                 Container(

                     margin: EdgeInsets.fromLTRB(20, 60, 20, 0),

                     alignment: Alignment.topLeft,

                     decoration: BoxDecoration(

                         // color: Colors.white,

                         ),

                     child: SingleChildScrollView(

                         padding: EdgeInsets.fromLTRB(10, 20, 10, 0),

                         child: Container(

                             // new line

                             child: Column(

                                 mainAxisAlignment: MainAxisAlignment.start,

                                 children: <Widget>[

                               _BuildHeader(),

                               Form(

                                   key: _formKey,

                                   child: Column(

                                     crossAxisAlignment:

                                         CrossAxisAlignment.center,

                                     mainAxisAlignment:

                                         MainAxisAlignment.center,

                                     children: <Widget>[

                                       _BuildEmailInput(),

                                       _BuildPasswordInput(),

                                     ],

                                   )),

                               Row(

                                   crossAxisAlignment: CrossAxisAlignment.end,

                                   mainAxisAlignment: MainAxisAlignment.end,

                                   children: <Widget>[

                                     _BuildForgotPasswordButton(),

                                   ]),

                               _BuildButton(_formKey),

                               _GoogleLoginButton(),

                               _BuildORText(),

                               _BuildSignUpButton(),

                             ]))))

               ]),

             ),

           )));

     },

   );

 }

}




class _BuildHeader extends StatelessWidget {

 const _BuildHeader({Key key}) : super(key: key);




 @override

 Widget build(BuildContext context) {

   return BlocBuilder<SigninCubit, SigninState>(builder: (context, state) {

     return Column(

       children: [

         SvgPicture.asset(

           "assets/icons/sign_in_account_label.svg",

         )

       ],

     );

   });

 }

}




class _BuildEmailInput extends StatelessWidget {

 const _BuildEmailInput({Key key}) : super(key: key);




 @override

 Widget build(BuildContext context) {

   return BlocBuilder<SigninCubit, SigninState>(

       buildWhen: (previous, current) => previous.email != current.email,

       builder: (context, state) {

         return Padding(

             padding: EdgeInsets.only(top: 60),

             child: TextFormField(

               autovalidateMode: AutovalidateMode.onUserInteraction,

               style: TextStyle(fontSize: 15),

               decoration: FormFields.getInputDecoration('Email Id'),

               autocorrect: false,

               validator: (value) {

                 if (value.isEmpty) {

                   return 'Please enter your email';

                 }

                 if (!EmailValidator.validate(value, true, true)) {

                   return 'Please enter a valid email address';

                 }

                 return null;

               },

               //email space allow validation - edited by Kiran Patil

               onChanged: (email) => context

                   .read<SigninCubit>()

                   .emailChanged(email.replaceAll(" ", "")),

             ));

       });

 }

}




class _BuildPasswordInput extends StatelessWidget {

 @override

 Widget build(BuildContext context) {

   return BlocBuilder<SigninCubit, SigninState>(

       buildWhen: (previous, current) => previous.password != current.password,

       builder: (context, state) {

         return Padding(

             padding: EdgeInsets.only(top: 15),

             child: TextFormField(

               autovalidateMode: AutovalidateMode.onUserInteraction,

               // focusNode: myFocusNode,

               obscureText: !state.showPassword,

               // controller: textController,

               style: TextStyle(fontSize: 15),




               decoration: InputDecoration(

                 contentPadding:

                     EdgeInsets.symmetric(vertical: 10, horizontal: 20),

                 labelText: "Password",

                 suffixIcon: GestureDetector(

                   onTap: () {

                     context.read<SigninCubit>().isShowPassword();

                   },

                   child: Padding(

                       padding: EdgeInsets.all(5),

                       child: !state.showPassword

                           ? SvgPicture.asset("assets/icons/btn_show.svg")

                           : Image.asset(

                               "assets/icons/hide_btn.png",

                               width: 55,

                               height: 25,

                             )),

                 ),

                 labelStyle: TextStyle(color: const Color(0xFF828282)),

                 focusedBorder: OutlineInputBorder(

                   // gapPadding: 20,

                   borderRadius: BorderRadius.circular(2),

                   borderSide: const BorderSide(

                     color: Colors.black,

                   ),

                 ),

                 enabledBorder: OutlineInputBorder(

                     borderRadius: BorderRadius.circular(1),

                     borderSide: BorderSide(color: const Color(0xFFE0E0E0))),

                 focusedErrorBorder: OutlineInputBorder(

                     borderRadius: BorderRadius.circular(1),

                     borderSide: BorderSide(color: Colors.red)),

                 border: OutlineInputBorder(

                     borderRadius: BorderRadius.circular(1),

                     borderSide: BorderSide(color: const Color(0xFFE0E0E0))),

                 // border: InputBorder(borderSide: BorderStyle.solid),

               ),

               validator: (value) {

                 if (value.isEmpty) {

                   return 'Please enter a password';

                 }

                 return null;

               },

               onChanged: (password) =>

                   context.read<SigninCubit>().passwordChanged(password),

             ));

       });

 }

}




class _BuildButton extends StatelessWidget {

 GlobalKey<FormState> formKey;




 _BuildButton(GlobalKey<FormState> this.formKey, {Key key}) : super(key: key);

 @override

 Widget build(BuildContext context) {

   return BlocBuilder<SigninCubit, SigninState>(builder: (context, state) {

     return Padding(

         padding: const EdgeInsets.fromLTRB(0, 30, 0, 10),

         child:

             // SizedBox.expand(child:

             SizedBox(

           width: double.maxFinite,

           child: ElevatedButton(

               style: FormButton.getButtonStyle(),

               onPressed: () {

                 if (formKey.currentState.validate()) {

                   context.read<SigninCubit>().logInWithCredentials();

                 }

               },

               child: FormButton.getButtonText("Sign In")),

         )

         // )

         );

   });

 }

}




class _BuildForgotPasswordButton extends StatelessWidget {

 const _BuildForgotPasswordButton({Key key}) : super(key: key);




 @override

 Widget build(BuildContext context) {

   return BlocBuilder<SigninCubit, SigninState>(builder: (context, state) {

     return InkWell(

         onTap: () => {

               Navigator.of(context).push(MaterialPageRoute(

                   builder: (context) => ForgotPass(),

                   settings: RouteSettings(name: "/forgot_password")))

             },

         child: Padding(

             padding: EdgeInsets.symmetric(vertical: 20, horizontal: 0),

             child: RichText(

                 textAlign: TextAlign.center,

                 text: const TextSpan(children: [

                   TextSpan(

                       text: 'Forgot Password?',

                       style:

                           TextStyle(color: Color(0xFF333333), fontSize: 14))

                 ]))));

   });

 }

}




class _BuildSignUpButton extends StatelessWidget {

 const _BuildSignUpButton({Key key}) : super(key: key);




 @override

 Widget build(BuildContext context) {

   return BlocBuilder<SigninCubit, SigninState>(builder: (context, state) {

     return Padding(

         padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),

         child: SizedBox(

           width: double.maxFinite,

           child: ElevatedButton(

               style: FormButton.getButtonStyle(),

               onPressed: () {

                 {

                   Navigator.of(context).push(MaterialPageRoute(

                       builder: (context) => SignUp(),

                       settings: RouteSettings(name: "/signup")));

                 }

               },

               child: FormButton.getButtonText("Register")),

         )

         // )

         );

   });

 }

}




class _GoogleLoginButton extends StatelessWidget {

 @override

 Widget build(BuildContext context) {

   final theme = Theme.of(context);

   return Container(

       width: double.maxFinite,

       child: ElevatedButton.icon(

         label: const Text(

           'SIGN IN WITH GOOGLE',

           style: TextStyle(color: Colors.white),

         ),

         style: ElevatedButton.styleFrom(

           shape: RoundedRectangleBorder(

             borderRadius: BorderRadius.circular(30),

           ),

           primary: theme.colorScheme.secondary,

         ),

         icon: const Icon(FontAwesomeIcons.google, color: Colors.white),

         onPressed: () => context.read<SigninCubit>().logInWithGoogle(),

       ));

 }

}




class _BuildORText extends StatelessWidget {

 const _BuildORText({Key key}) : super(key: key);




 @override

 Widget build(BuildContext context) {

   return BlocBuilder<SigninCubit, SigninState>(builder: (context, state) {

     return Row(

       crossAxisAlignment: CrossAxisAlignment.center,

       mainAxisAlignment: MainAxisAlignment.center,

       children: [

         Container(

           color: Color(0xFF707070),

           height: 1,

           width: 70,

         ),

         Padding(

             padding: EdgeInsets.symmetric(vertical: 20, horizontal: 5),

             child: Text(

               "OR",

               style: TextStyle(fontSize: 12, color: Color(0xFF707070)),

             )),

         Container(

           color: Color(0xFF707070),

           height: 1,

           width: 70,

         ),

       ],

     );

   });

 }

}

What I did is we needed to show the 2 text fields & a login button in the column, so we had to take one container and place all these 3 widgets in the column so it could be shown to us vertically. And as they have properties we can change the style & behavior as per our requirement.

Hire Flutter Developers at your Ease

If someone wants to show the widgets in the horizontal form, they can do that using the Row component. They can use try cross-axis alignment and main-axis alignment as well. As we need to save the state of our widgets we used Flutter BLoC.

So the output will probably be seen like this. For more in detail, you can refer  the guide to building layouts in Flutter.

coma

Conclusion

The Flutter UI design is not the same as android or iOS; you need to add UI manually and align them in your layout. But, it’s quite good as it uses material UI & is flexible with any device. So here we conclude the tutorial on how to design UI in flutter.

Keep Reading

Keep Reading

  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?