Ui 1

Simple Login Screen




# UI




# Login Screen Code

import 'package:academy/view/widget/auth_text_button_widget.dart';
import 'package:academy/view/widget/auth_text_field_widget.dart';
import 'package:academy/view/widget/auth_text_widget.dart';
import 'package:flutter/material.dart';

class LoginScreen extends StatefulWidget {
  const LoginScreen({super.key});

  @override
  State createState() => _LoginScreenState();
}

class _LoginScreenState extends State {
  GlobalKey formState = GlobalKey();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Image.asset(
            'assets/images/1.jpg',
            height: MediaQuery.of(context).size.height,
            fit: BoxFit.cover,
          ),
          Align(
            alignment: Alignment.bottomCenter,
            child: ListView(
              shrinkWrap: true,
              children: [
                Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Container(
                      padding: const EdgeInsets.all(15),
                      decoration: const BoxDecoration(
                        color: Color.fromARGB(183, 42, 46, 50),
                        borderRadius: BorderRadius.only(
                          topRight: Radius.circular(50),
                          topLeft: Radius.circular(50),
                        ),
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        children: [
                          const AuthTextWidget(
                            text: 'Welcome!',
                            size: 20,
                            fontWeight: FontWeight.bold,
                          ),
                          const SizedBox(
                            height: 10,
                          ),
                          const AuthTextWidget(
                            text: 'Join us and type your notes',
                            size: 15,
                            fontWeight: FontWeight.w500,
                          ),
                          const SizedBox(
                            height: 10,
                          ),
                          Form(
                            key: formState,
                            child: const Column(children: [
                              AuthTextFieldWidget(
                                type: 'Email',
                                text: 'Email address',
                                icon: Icons.email_rounded,
                              ),
                              SizedBox(
                                height: 20,
                              ),
                              AuthTextFieldWidget(
                                type: 'password',
                                text: 'Password',
                                icon: Icons.key,
                              ),
                            ]),
                          ),
                          const SizedBox(
                            height: 18,
                          ),
                          const AuthTextButtonWidget(
                            text: 'Forget Password?',
                          ),
                          ElevatedButton(
                            onPressed: () {
                              if (formState.currentState!.validate()) {
                                ScaffoldMessenger.of(context).showSnackBar(
                                  const SnackBar(
                                    content: Text('Success'),
                                  ),
                                );
                              }
                            },
                            style: ElevatedButton.styleFrom(
                              minimumSize: const Size(50, 50),
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(20),
                              ),
                            ),
                            child: const Text('Login'),
                          ),
                          const SizedBox(
                            height: 15,
                          ),
                          const Row(
                            children: [
                              Expanded(child: Divider()),
                              Padding(
                                padding: EdgeInsets.symmetric(horizontal: 10),
                                child: AuthTextWidget(
                                  text: 'or',
                                  size: 15,
                                  fontWeight: FontWeight.normal,
                                ),
                              ),
                              Expanded(child: Divider()),
                            ],
                          ),
                          const Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              AuthTextWidget(
                                text: 'Dont’t have an account?',
                                size: 10,
                                fontWeight: FontWeight.normal,
                                color: Colors.grey,
                              ),
                              AuthTextButtonWidget(
                                text: 'Registration',
                              )
                            ],
                          )
                        ],
                      ),
                    ),
                  ],
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}

# Widgets Code


import 'package:academy/view/widget/auth_text_widget.dart';
import 'package:flutter/material.dart';

class AuthTextButtonWidget extends StatelessWidget {
  final String text;
  const AuthTextButtonWidget({super.key, required this.text});

  @override
  Widget build(BuildContext context) {
    return TextButton(
      onPressed: () {},
      style: TextButton.styleFrom(
        alignment: Alignment.topLeft,
      ),
      child: AuthTextWidget(
        text: text,
        size: 15,
        fontWeight: FontWeight.bold,
      ),
    );
  }
}
---------------------------------------------------------------------
import 'package:flutter/material.dart';

class AuthTextFieldWidget extends StatelessWidget {
  final String text, type;
  final IconData icon;
  const AuthTextFieldWidget(
      {super.key, required this.text, required this.icon, required this.type});

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      validator: (val) {
        if (val!.isEmpty) {
          return 'Not Valid $type';
        }
        return null;
      },
      decoration: InputDecoration(
        prefixIcon: Icon(
          icon,
        ),
        hintText: text,
        filled: true,
        fillColor: Color.fromARGB(127, 191, 200, 209),
        contentPadding: const EdgeInsets.all(13),
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(20),
          borderSide: BorderSide.none,
        ),
      ),
    );
  }
}
------------------------------------------------------------------
import 'package:flutter/material.dart';

class AuthTextWidget extends StatelessWidget {
  final String text;
  final Color? color;
  final double size;
  final FontWeight fontWeight;
  const AuthTextWidget({
    super.key,
    required this.text,
    this.color,
    required this.size,
    required this.fontWeight,
  });

  @override
  Widget build(BuildContext context) {
    return Text(
      text,
      textAlign: TextAlign.center,
      style: TextStyle(
        fontFamily: 'Rubik',
        color: color ?? Colors.white,
        fontSize: size,
        fontWeight: fontWeight,
      ),
    );
  }
}

# Links

Stack Widget

Expanded Widget

Form Widget

GlobalKey

# Questions

Q: What is the benefit of using a `ListView` with `shrinkWrap: true` inside the `LoginScreen` widget?

A: Using a `ListView` with `shrinkWrap: true` allows the content of the `ListView` to take up only the necessary space within the parent widget. It means that the `ListView` will shrink its height to fit its children, rather than taking up the entire available height. This can be beneficial in scenarios where you want to avoid unnecessary scrolling and ensure that the content is displayed within a limited area. In the `LoginScreen` widget, it ensures that the `ListView` containing the login form and related widgets takes up only the required space at the bottom of the screen, allowing the background image to occupy the remaining space above it.

Q: Why we cannot use color outside boxDecoration in container?

A: The reason for this limitation is that the decoration property overrides the color property when both are specified. The decoration property allows you to apply more complex visual effects to the Container, such as gradients, borders, shadows, and images. When you set a decoration, the color property is ignored because the decoration itself can define its own background color.

Q: What is the difference between ListView and singleChildScrollView?

A: ListView is suitable for displaying a large number of children efficiently, while SingleChildScrollView is useful when you have a single child widget that exceeds the available space and needs to be scrolled.
ListView: Efficiently handles large lists of children by recycling and reusing widgets as they scroll in and out of view. It conserves memory by rendering only the visible portion of the list.
SingleChildScrollView: Keeps all its child widgets in memory, even if they are not currently visible on the screen. This can lead to increased memory usage, especially if the child widget contains a large amount of content.

Q: Why we use const before some Widget?

A: Using const before a widget constructor in Flutter provides performance optimizations, widget reusability, and helps catch potential issues during development. It is particularly useful when working with simple, lightweight widgets that don't require dynamic property changes.

Q: What is the formState.currentState!.validate();?

A: By calling formState.currentState.validate(), you can initiate the validation process for all the form fields within the Form. If any of the fields fail validation, such as if they have empty or invalid input, the validate() method will return false. If all the fields pass validation, it will return true.
Typically, this method is used in conjunction with a form submission action. For example, you might use it within an onPressed callback of a button to validate the form fields before performing a specific action, such as submitting the form.

Post a Comment

0 Comments