Tutorial Create a login user interface with Flutter SDK Framework

in #utopian-io8 years ago (edited)

What Will I Learn?

I will learn about how to make attractive user interface with Flutter SDK Framework.

  • Create a project

  • Create a new file in the login page

  • Create a new file for the main page

Requirements

Difficulty

  • Intermediate

Tutorial Contents

  • Create a project

Run editor VS Code and then activate the feature dart preview 2
from the menu
File > Preferences > User Settings
then search for the dart.previewDart2 and set the value to true.

Next create a new project by pressing the key combination CTRL+SHIFT+P and write the name of the project for example login_app. For the application name should be all lowercase and should not contain spaces.

Next, create an assets folder in the project directory and copy all file preparation (jpg,alucard,logo,png, nunito.ttf) into the folder assets. This file must be explained in advance on the file pubspec.yaml in order to be used , add the following line right below the configuration of the flutter like this:

flutter:
  uses-material-design: true
  assets:
    - assets/logo.png
    - assets/alucard.jpg
  fonts:
      - family: Nunito
        fonts:
          - asset: assets/Nunito.ttf

Then in the main file main.dart remove all the code up to the only remaining class MyApp like this:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Kodeversitas',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.lightBlue,
        fontFamily: 'Nunito',
      ),
      home: null,
    );
  }
}

  • Create a new file in the login page

Create a new file with a right click on a folder lib next give the name of the login_page.darts. On the login page we need few widgets that the widget image for the logo, the field for email, field for password, login button, and last button is flat to forget the password. More code for the login_page.dart like this :

import 'package:flutter/material.dart';
import 'package:login/home_page.dart';

class LoginPage extends StatefulWidget {
  static String tag = 'login-page';
  @override
  _LoginPageState createState() => new _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  @override
  Widget build(BuildContext context) {
    final logo = Hero(
      tag: 'hero',
      child: CircleAvatar(
        backgroundColor: Colors.transparent,
        radius: 48.0,
        child: Image.asset('assets/logo.png'),
      ),
    );

    final email = TextFormField(
      keyboardType: TextInputType.emailAddress,
      autofocus: false,
      initialValue: '[email protected]',
      decoration: InputDecoration(
        hintText: 'Email',
        contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
        border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
      ),
    );

    final password = TextFormField(
      autofocus: false,
      initialValue: 'some password',
      obscureText: true,
      decoration: InputDecoration(
        hintText: 'Password',
        contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
        border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
      ),
    );

    final loginButton = Padding(
      padding: EdgeInsets.symmetric(vertical: 16.0),
      child: Material(
        borderRadius: BorderRadius.circular(30.0),
        shadowColor: Colors.lightBlueAccent.shade100,
        elevation: 5.0,
        child: MaterialButton(
          minWidth: 200.0,
          height: 42.0,
          onPressed: () {
            Navigator.of(context).pushNamed(HomePage.tag);
          },
          color: Colors.lightBlueAccent,
          child: Text('Log In', style: TextStyle(color: Colors.white)),
        ),
      ),
    );

    final forgotLabel = FlatButton(
      child: Text(
        'Forgot password?',
        style: TextStyle(color: Colors.black54),
      ),
      onPressed: () {},
    );

    return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
        child: ListView(
          shrinkWrap: true,
          padding: EdgeInsets.only(left: 24.0, right: 24.0),
          children: <Widget>[
            logo,
            SizedBox(height: 48.0),
            email,
            SizedBox(height: 8.0),
            password,
            SizedBox(height: 24.0),
            loginButton,
            forgotLabel
          ],
        ),
      ),
    );
  }
}

  • Create a new file for the main page

Next create a new file for the main page with the name of home_page.darts. On this page we will only display the widget on the profile picture of the assets alucard.jpg and a text widget for additional information. Widgets profile photo that will be wrapped by the widget hero with tag names same as the tag hero logo on the login_page.darts. Write the code like this :

import 'package:flutter/material.dart';

class HomePage extends StatelessWidget {
  static String tag = 'home-page';

  @override
  Widget build(BuildContext context) {
    final alucard = Hero(
      tag: 'hero',
      child: Padding(
        padding: EdgeInsets.all(16.0),
        child: CircleAvatar(
          radius: 72.0,
          backgroundColor: Colors.transparent,
          backgroundImage: AssetImage('assets/alucard.jpg'),
        ),
      ),
    );

    final welcome = Padding(
      padding: EdgeInsets.all(8.0),
      child: Text(
        'Welcome Alucard',
        style: TextStyle(fontSize: 28.0, color: Colors.white),
      ),
    );

    final lorem = Padding(
      padding: EdgeInsets.all(8.0),
      child: Text(
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec hendrerit condimentum mauris id tempor. Praesent eu commodo lacus. Praesent eget mi sed libero eleifend tempor. Sed at fringilla ipsum. Duis malesuada feugiat urna vitae convallis. Aliquam eu libero arcu.',
        style: TextStyle(fontSize: 16.0, color: Colors.white),
      ),
    );

    final body = Container(
      width: MediaQuery.of(context).size.width,
      padding: EdgeInsets.all(28.0),
      decoration: BoxDecoration(
        gradient: LinearGradient(colors: [
          Colors.blue,
          Colors.lightBlueAccent,
        ]),
      ),
      child: Column(
        children: <Widget>[alucard, welcome, lorem],
      ),
    );

    return Scaffold(
      body: body,
    );
  }
}

Next we return to the main.dart and add the code to the navigation of the router like this :

final routes = <String, WidgetBuilder>{
    LoginPage.tag: (context) => LoginPage(),
    HomePage.tag: (context) => HomePage(),
  };

Now set the attributes of the routes widget MaterialApp with the object of the routes above. Also set for the property the home becomes the object of login_page like this LoginPage() So the complete code to main as the following :

import 'package:flutter/material.dart';
import 'login_page.dart';
import 'home_page.dart';

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

class MyApp extends StatelessWidget {
  
  final routes = <String, WidgetBuilder>{
    LoginPage.tag: (context) => LoginPage(),
    HomePage.tag: (context) => HomePage(),
  };

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Kodeversitas',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.lightBlue,
        fontFamily: 'Nunito',
      ),
      home: LoginPage(),
      routes: routes,
    );
  }
}

Now run the application Flutter by pressing the F5 but don't forget to start the Android Emulator or IOS Simulator.

Thank You



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

mantap bosquh

Pane na, droe keuh inan yang leubeh lancar

Your contribution cannot be approved because it does not follow the Utopian Rules.

The tutorial is not original. You took it from https://www.codepolitan.com/mudahnya-membuat-antarmuka-login-cantik-dengan-flutter-5aa46d29e03d1

You can contact us on Discord.
[utopian-moderator]