复选框未在 Flutter 中被选中

checkbox is not getting checked in Flutter

我正在创建一个包含特定字段的注册表单。

  1. 我的复选框不起作用。启用复选框后,按钮应启用点击。
  2. TextFormField 验证无效。 a) FirstName 和 Lastname 不应为空。- 当未输入数据时,应显示错误消息 b) 电子邮件和手机号码验证。 c) 应该完成密码验证。

我正在将其创建为小部件,需要调用 Main.dart

下面是代码。

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'))
        
      ])),
      ),
    );
  }
}

试试这个例子

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  bool agree = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.all(10.0),
        child: Form(
          key: _formKey,
          child: Column(
            children: [
              TextFormField(
                decoration: new InputDecoration(labelText: "First Name"),
                validator: (val) {
                  if (val.isEmpty) {
                    return "Must not be empty";
                  }
                },
              ),
              SizedBox(height: 10),
              Checkbox(
                  value: agree,
                  onChanged: (check) {
                    setState(() {
                      agree = check;
                    });
                  }),
              SizedBox(height: 10),
              ElevatedButton(
                  onPressed: agree
                      ? () {
                          if (_formKey.currentState.validate()) {
                            print("DO Something");
                          }
                        }
                      : null,
                  child: Text('Continue'))
            ],
          ),
        ),
      ),
    );
  }
}