mirrored_repositories/Flutter-Open-Source-Apps/Flutter Open Source Apps/Advanced/flutter_eCommerce_ui_kit/lib
import 'package:flutter/material.dart';
import 'package:flutter_ecommerce_ui_kit/blocks/auth_block.dart';
import 'package:flutter_ecommerce_ui_kit/models/user.dart';
import 'package:provider/provider.dart';

class SignIn extends StatefulWidget {
  @override
  _SignInState createState() => _SignInState();
}

class _SignInState extends State<SignIn> {
  final _formKey = GlobalKey<FormState>();
  final UserCredential userCredential =
      UserCredential(usernameOrEmail: '', password: '');

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Form(
        key: _formKey,
        child: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.only(left: 15.0, right: 15.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.only(bottom: 12.0),
                  child: TextFormField(
                    keyboardType: TextInputType.emailAddress,
                    validator: (value) {
                      if (value!.isEmpty) {
                        return 'Please Enter Email or Username';
                      }
                      return null;
                    },
                    onSaved: (value) {
                      setState(() {
                        userCredential.usernameOrEmail = value!;
                      });
                    },
                    decoration: InputDecoration(
                      hintText: 'Enter Username Or Email',
                      labelText: 'Email',
                    ),
                  ),
                ),
                TextFormField(
                  validator: (value) {
                    if (value!.isEmpty) {
                      return 'Please Enter Password';
                    }
                    return null;
                  },
                  onSaved: (value) {
                    setState(() {
                      userCredential.password = value!;
                    });
                  },
                  decoration: InputDecoration(
                    hintText: 'Enter Password',
                    labelText: 'Password',
                  ),
                  obscureText: true,
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 25.0),
                  child: SizedBox(
                    width: double.infinity,
                    height: 50,
                    child: Consumer<AuthBlock>(
                      builder: (BuildContext context, AuthBlock auth,
                          Widget? child) {
                        return ElevatedButton(
                          style: ElevatedButton.styleFrom(
                            primary: Theme.of(context).primaryColor,
                          ),
                          child: auth.loading && auth.loadingType == 'login'
                              ? CircularProgressIndicator(
                                  valueColor: AlwaysStoppedAnimation<Color>(
                                      Colors.white),
                                )
                              : Text(
                                  'Sign In',
                                  style: TextStyle(color: Colors.white),
                                ),
                          onPressed: () {
                            // Validate form
                            if (_formKey.currentState!.validate() &&
                                !auth.loading) {
                              // Update values
                              _formKey.currentState!.save();
                              // Hit Api
                              auth.login(userCredential);
                            }
                          },
                        );
                      },
                    ),
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
