dimanche 14 février 2021

checkbox is not getting checked in Flutter

I am creating a sign up form with certain fields.

  1. My checkbox is not working. once the checkbox is enabled then the Button should enable for click.
  2. TextFormField validation is not working. a)FirstName and Lastname should not be empty.- when data is not entered it should so error messages b)Email and Mobile Number Validation. c) Password validation should be done.

i am creating this as widget and need to call in Main.dart

Below is the code.

Signup.dart

import 'dart:developer';
import 'dart:ui';
import 'package:flutter/gestures.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
import 'package:email_validator/email_validator.dart';

class SignUp extends StatefulWidget {
  HomePage createState() => HomePage();
}

class HomePage extends State<SignUp> {
  String _firstname;
  String _lastname;
  String _username;
  String _email;
  String _mobileno;
  String _password;


  @override
  Widget build(BuildContext context) {
    var screenSize = MediaQuery.of(context).size;
    bool agree = false;

    void _doSomething() {
      // Do something
    }

    void _onChanged(bool value){
      setState(() {
        agree = value;
      });
    }
    return Container(
      width: screenSize.width,
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage("assets/img/bg_signup.png"),
          fit: BoxFit.cover,
        ),
      ),


      child: SingleChildScrollView(
        child: Form(
          child: Column(children: [
        Padding(

          padding: EdgeInsets.fromLTRB(30, 10, 30, 0),
          child: new TextFormField(
            keyboardType: TextInputType.text,
            decoration: new InputDecoration(
              labelText: "First Name",
              labelStyle: new TextStyle(
                fontFamily: 'Poppins',
                fontWeight: FontWeight.w300,
              ),
              fillColor: Colors.white,
              border: new OutlineInputBorder(
                borderRadius: new BorderRadius.circular(10.0),
                borderSide: BorderSide(color: Colors.blue),
              ),
            ),
            style: new TextStyle(
              fontFamily: 'Poppins',
              fontWeight: FontWeight.w300,
            ),
          ),
        ),

        Padding(
          padding: EdgeInsets.fromLTRB(30, 25, 30, 0),
          child: new TextFormField(
            keyboardType: TextInputType.text,
            decoration: new InputDecoration(
              labelText: "Last Name",
              labelStyle: new TextStyle(
                fontFamily: 'Poppins',
                fontWeight: FontWeight.w300,
              ),
              fillColor: Colors.white,
              border: new OutlineInputBorder(
                borderRadius: new BorderRadius.circular(10.0),
                borderSide: BorderSide(color: Colors.blue),
              ),
            ),
            style: new TextStyle(
              fontFamily: 'Poppins',
              fontWeight: FontWeight.w300,
            ),
          ),
        ),

        Padding(
          padding: EdgeInsets.fromLTRB(30, 25, 30, 0),
          child: new TextFormField(
            decoration: new InputDecoration(
              labelText: "Username",
              labelStyle: new TextStyle(
                fontFamily: 'Poppins',
                fontWeight: FontWeight.w300,
              ),
              fillColor: Colors.white,
              border: new OutlineInputBorder(
                borderRadius: new BorderRadius.circular(10.0),
                borderSide: BorderSide(color: Colors.blue),
              ),
            ),
            style: new TextStyle(
              fontFamily: 'Poppins',
              fontWeight: FontWeight.w300,
            ),
          ),
        ),

        Padding(
          padding: EdgeInsets.fromLTRB(30, 25, 30, 0),
          child: new TextFormField(
            decoration: new InputDecoration(
              labelText: "Email",
              labelStyle: new TextStyle(
                fontFamily: 'Poppins',
                fontWeight: FontWeight.w300,
              ),
              fillColor: Colors.white,
              border: new OutlineInputBorder(
                borderRadius: new BorderRadius.circular(10.0),
                borderSide: BorderSide(color: Colors.blue),
              ),
            ),
            validator: (value) => EmailValidator.validate(value) ? null : "Please enter a valid email",
            style: new TextStyle(
              fontFamily: 'Poppins',
              fontWeight: FontWeight.w300,
            ),
          ),
        ),

        Padding(
          padding: EdgeInsets.fromLTRB(30, 25, 30, 0),
          child: new TextFormField(
            keyboardType: TextInputType.number,
            decoration: new InputDecoration(
              labelText: "Mobile Number",
              labelStyle: new TextStyle(
                fontFamily: 'Poppins',
                fontWeight: FontWeight.w300,
              ),
              fillColor: Colors.white,
              border: new OutlineInputBorder(
                borderRadius: new BorderRadius.circular(10.0),
                borderSide: BorderSide(color: Colors.blue),
              ),
            ),
            style: new TextStyle(
              fontFamily: 'Poppins',
              fontWeight: FontWeight.w300,
            ),
            
          ),
        ),

        Padding(
          padding: EdgeInsets.fromLTRB(30, 25, 30, 0),
          child: new TextFormField(
            decoration: new InputDecoration(
              labelText: "Password",
              labelStyle: new TextStyle(
                fontFamily: 'Poppins',
                fontWeight: FontWeight.w300,
              ),
              fillColor: Colors.white,
              border: new OutlineInputBorder(
                borderRadius: new BorderRadius.circular(10.0),
                borderSide: BorderSide(color: Colors.blue),
              ),
            ),
            style: new TextStyle(
              fontFamily: 'Poppins',
              fontWeight: FontWeight.w300,
            ),
          ),
        ),

        Padding(
          padding: EdgeInsets.fromLTRB(30, 25, 30, 0),
          child: new TextFormField(
            decoration: new InputDecoration(
              labelText: "Confirm Password",
              labelStyle: new TextStyle(
                fontFamily: 'Poppins',
                fontWeight: FontWeight.w300,
              ),
              fillColor: Colors.white,
              border: new OutlineInputBorder(
                borderRadius: new BorderRadius.circular(10.0),
                borderSide: BorderSide(color: Colors.blue),
              ),
            ),
            style: new TextStyle(
              fontFamily: 'Poppins',
              fontWeight: FontWeight.w300,
            ),
          ),
        ),

        Row(
          children: [
            Material(
              child: Checkbox(
                checkColor: Colors.red,
                focusColor: Colors.black,
                value: agree,
                onChanged: (bool value) {
               _onChanged(value);
                },
              ),
            ),

            new Center(
           child: new RichText(
            text : new TextSpan(
              children: [
                new TextSpan(
                  text: 'Accept the ',
                  style: new TextStyle(color: Colors.black),
                ),
                new TextSpan(
                  text: 'terms and conditions',
                  style: new TextStyle(color: Colors.blue, fontWeight : FontWeight.bold ),
                  recognizer: new TapGestureRecognizer()
                    ..onTap = () {
                    Container(
                    child: AlertDialog(
                      title: Text('Reset settings?'),
                      content: Text('This will reset your device to its default factory settings.'),
                    ),
                    );   }
                ),
              ],
            ),
            ),
            ),
          ],
        ),

        ElevatedButton(onPressed: agree ? _doSomething : null, child: Text('Continue'))
        
      ])),
      ),
    );
  }
}



Aucun commentaire:

Enregistrer un commentaire