build method

  1. @override
Widget build (
  1. BuildContext context
)
override

Describes the part of the user interface represented by this widget.

The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.

Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a widget's build method is to depend on anything else, use a StatefulWidget instead.

See also:

  • StatelessWidget, which contains the discussion on performance considerations.

Implementation

@override
Widget build(BuildContext context) {

  /// Providers to access the authentication and database services
  var _authProvider = Provider.of<FirebaseAuthenticationService>(context);
  var _databaseProvider = Provider.of<FirebaseDatabaseService>(context);

  return SafeArea(
    child: Scaffold(
      backgroundColor: Hexcolor('#fddcd8'),
      appBar: AppBar(
        title: Text(
          'Register',
          style: TextStyle(
            fontSize: 18,
            color: Colors.grey[700],
          ),
        ),
        centerTitle: true,
        elevation: 1.0,
        backgroundColor: Hexcolor('#fddcd8'),
        iconTheme: IconThemeData(
          color: Colors.grey[700],
        ),
      ),
      body: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Padding(
              padding: const EdgeInsets.fromLTRB(0,8,0,0),
              child: Image.asset('assets/milk_matters_logo_login.png'),
            ),
            Padding(
              padding: const EdgeInsets.all(15.0),
              child: Text(
                'Please complete and submit the form to create an account.',
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 18.0,
                  fontWeight: FontWeight.w500,
                  letterSpacing: 2.0,
                  color: Colors.grey[700],
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(20,0,20,5),
              child: Container(
                decoration: BoxDecoration(
                  //color: Hexcolor('#7588fd'),//blue
                  color: Hexcolor('#f2e6bb'), //Light yellow
                  //color: Hexcolor('#f5eccc'), //Lighter yellow
                  boxShadow: <BoxShadow>[
                    BoxShadow(
                      color: Colors.grey[500],
                      offset: Offset(0.3, 0.3),
                      blurRadius: 3.0,
                    ),
                  ],
                  borderRadius: BorderRadius.all(Radius.circular(25.0)),
                ),
                child: Column(
                  children: [
                      FormBuilder(
                        key: _fbKey,
                        child: Padding(
                          padding: const EdgeInsets.fromLTRB(30.0, 5.0, 30.0, 5.0),
                          child: Column(
                            children: <Widget>[
                              FormBuilderTextField(
                                controller: nameController,
                                attribute: "fullName",
                                decoration: InputDecoration(
                                  labelText: "Full Name",
                                  icon: Icon(Icons.person),
                                  //focusedBorder:
                                ),
                                validators: [
                                  FormBuilderValidators.required(
                                      errorText: 'Please enter a full name.'
                                  ),
                                ],
                              ),
                              SizedBox(
                                height: 5.0,
                              ),
                              FormBuilderTextField(
                                controller: phoneNumberController,
                                attribute: "phoneNumber",
                                keyboardType: TextInputType.number,
                                decoration: InputDecoration(
                                  labelText: "Phone Number",
                                  icon: Icon(Icons.phone),
                                  //focusedBorder:
                                ),
                                validators: [
                                  FormBuilderValidators.required(
                                      errorText: 'Please enter a phone number.'
                                  ),
                                  FormBuilderValidators.numeric(
                                      errorText: 'Please enter a valid phone number.'
                                  ),
                                ],
                              ),
                              SizedBox(
                                height: 5.0,
                              ),
                              FormBuilderTextField(
                                controller: emailController,
                                attribute: "email",
                                decoration: InputDecoration(
                                  labelText: "Email",
                                  icon: Icon(Icons.email),
                                ),
                                validators: [
                                  FormBuilderValidators.required(
                                      errorText: 'Please enter an email address.'
                                  ),
                                  FormBuilderValidators.email(
                                      errorText: 'Please enter a valid email address.'
                                  ),
                                ],
                              ),
                              SizedBox(
                                height: 5.0,
                              ),
                              FormBuilderTextField(
                                controller: passwordController,
                                attribute: "password",
                                obscureText: true,
                                decoration: InputDecoration(
                                  labelText: "Password",
                                  icon: Icon(Icons.lock),
                                ),
                                validators: [
                                  FormBuilderValidators.required(
                                      errorText: 'Please enter a password.'
                                  ),
                                ],
                              ),
                            ],
                          ),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(15.0),
                        child: RaisedButton(
                          child: Text(
                            'Register',
                            style: TextStyle(
                              fontSize: 18.0,
                              color: Colors.grey[200],
                            ),
                          ),
                          color: Hexcolor('#dc0963'),
                          /// If the form is validated correctly,
                          /// then attempt to register the account.
                          /// Inform the user of any errors, or log them into their account.
                          onPressed: () async  {
                            if(_fbKey.currentState.validate()) {
                              BotToast.showLoading();
                              AuthResultStatus result = await _authProvider.registerEmailPassword(
                                  emailController.text,
                                  passwordController.text);
                              if(result==AuthResultStatus.successful){
                                await _databaseProvider.pushNewDonorUser(emailController.text.trim(),
                                    nameController.text.trim(), phoneNumberController.text.trim());
                                Navigator.pop(context);
                              }
                              BotToast.closeAllLoading();
                              if(result!=AuthResultStatus.successful){
                                BotToast.showText(
                                  text: AuthExceptionHandler.generateExceptionMessage(result),
                                );
                              }
                            }
                          },
                        ),
                      ),
                    ],
                  ),
                ),
            ),
          ],
        ),
      ),
    ),
  );
}