Untitled

 avatar
unknown
dart
2 years ago
5.1 kB
4
Indexable
import 'package:budget_image_links/blocs/sign_up/sign_up.screen.dart';
import 'package:budget_image_links/services/sign_in.service.dart';
import 'package:budget_image_links/widgets/switch_form_field.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';

import 'bloc/sign_in_bloc.dart';

class SignInScreen extends StatefulWidget {
  SignInScreen({super.key});
  static const routeName = "/sign-in";
  final _formKey = GlobalKey<FormBuilderState>();

  @override
  State<SignInScreen> createState() => _SignInScreenState();
}

class _SignInScreenState extends State<SignInScreen> {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => SignInBloc(
        RepositoryProvider.of<SignInService>(context),
      )..add(LoadInitialFormEvent()),
      child: Scaffold(
        body: BlocBuilder<SignInBloc, SignInState>(
          builder: (context, state) {
            if (state is SignInLoadingState) {
              return const Center(
                child: CircularProgressIndicator(),
              );
            } else if (state is SignInSuccessfullState) {
              return Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    child: Center(
                      child: Text(state.message),
                    ),
                  ),
                  ElevatedButton(
                    onPressed: () {},
                    child: const Text("Access App"),
                  ),
                  ElevatedButton(
                    onPressed: () => BlocProvider.of<SignInBloc>(context)
                        .add(LoadInitialFormEvent()),
                    child: const Text("Go Back"),
                  )
                ],
              );
            } else if (state is SignInInitialState) {
              return FormBuilder(
                onChanged: () => print("FORM CHANGED"),
                autovalidateMode: AutovalidateMode.onUserInteraction,
                initialValue: const {
                  "username": "",
                  "password": "",
                },
                key: widget._formKey,
                child: SingleChildScrollView(
                  child: Padding(
                    padding: const EdgeInsets.all(40),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Image.network(
                            "SOME LINK"),
                        FormBuilderTextField(
                          name: "username",
                          decoration: const InputDecoration(
                            labelText: "Username",
                          ),
                        ),
                        FormBuilderTextField(
                          name: "password",
                          obscureText: true,
                          decoration: const InputDecoration(
                            labelText: "Password",
                          ),
                        ),

                        //FormBuilderTextField(name: "password"),
                        ElevatedButton(
                            onPressed: () => signInClicked(context),
                            child: const Text("Sign In")),
                        SwitchForm(
                          formName: "Sign up",
                          onTap: () => Navigator.of(context)
                              .pushReplacementNamed(SignUpScreen.routeName),
                        ),
                        ElevatedButton(
                            onPressed: resetFields, child: const Text("Reset")),
                        ElevatedButton(
                            onPressed: readFields, child: const Text("Read")),
                      ],
                    ),
                  ),
                ),
              );
            }
            return Container(
              child: const Center(
                child: Text("STATE ERROR"),
              ),
            );
          },
        ),
      ),
    );
  }

  // ! Functions
  void resetFields() {
    widget._formKey.currentState?.reset();
  }

  void readFields() {
    // ! to get the whole form value, we need to save it first to display it as map
    widget._formKey.currentState?.save();

    var x = widget._formKey.currentState?.value;

    print(x);
  }

  void signInClicked(BuildContext context) {
    widget._formKey.currentState?.save();

    var username =
        widget._formKey.currentState?.fields["username"]?.value.toString();
    var password =
        widget._formKey.currentState?.fields["password"]?.value.toString();

    print("$username $password");

    BlocProvider.of<SignInBloc>(context).add(
      SignInClickEvent(
        username!,
        password!,
      ),
    );
  }
}
Editor is loading...