Flutter 仅当复选框为“时才启用颤振按钮”;勾选“;

Flutter 仅当复选框为“时才启用颤振按钮”;勾选“;,flutter,Flutter,大家好,为了允许注册,我启用了带有复选框的注册表。 我需要选中“我的用户要执行”复选框以启用按钮,否则将显示工具提示。。喜欢“您需要接受注册的条款和条件…” 这是复选框的一部分: Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Theme(

大家好,为了允许注册,我启用了带有复选框的注册表。 我需要选中“我的用户要执行”复选框以启用按钮,否则将显示工具提示。。喜欢“您需要接受注册的条款和条件…” 这是复选框的一部分:

Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Theme(
                      data: ThemeData(unselectedWidgetColor: Colors.white,),
                      child: Checkbox(
                        focusColor: Colors.lightBlue,
                        activeColor: Colors.orange,
                        value: rememberMe,
                        onChanged: (newValue) {
                          setState(() {
                            rememberMe = (newValue);
                          });
                        },
                      ),
                    ),
                    RichText(
                      text: TextSpan(children: [
                        TextSpan(
                          text: 'Accetto le condizioni e ',
                          style: TextStyle(fontSize: 10),
                        ),
                        TextSpan(
                          text: 'il trattamento dei dati personali ',
                          style: TextStyle(fontSize: 10,decoration: TextDecoration.underline,),
                        ),
                      ]),
                    )
                  ],
                ),


您需要将
null
传递给
按钮的
on按钮
,使其禁用。因此,当
rememberMe
false
时,您需要将
null
传递给
raised按钮的
on按下


钮扣(
最小宽度:100,
身高:50.0,
孩子:升起按钮(
形状:圆形矩形边框(
边界半径:边界半径。圆形(5.0),
),
标高:10,
onPressed:rememberMe?()异步{
设置状态(){
showSpinner=true;
});
试一试{
最终新用户=
等待_auth.createUserWithEmailAndPassword(
电子邮件:电子邮件,密码:密码);
if(newUser!=null){
导航器。推(
上下文
MaterialPackageRoute(生成器:(上下文)=>HomePage()),
);
}
设置状态(){
showSpinner=false;
});
}捕获(e){
印刷品(e);
}
}:null,//如果为false,则将其设为null
颜色:颜色(0xFF1f2032),
子:文本(
“注册”,
样式:TextStyle(字体大小:18,颜色:Colors.white),
),
),
),

您需要将
null
传递给按下
按钮的
on
,使其禁用。因此,当
rememberMe
false
时,您需要将
null
传递给
raised按钮的
on按下


钮扣(
最小宽度:100,
身高:50.0,
孩子:升起按钮(
形状:圆形矩形边框(
边界半径:边界半径。圆形(5.0),
),
标高:10,
onPressed:rememberMe?()异步{
设置状态(){
showSpinner=true;
});
试一试{
最终新用户=
等待_auth.createUserWithEmailAndPassword(
电子邮件:电子邮件,密码:密码);
if(newUser!=null){
导航器。推(
上下文
MaterialPackageRoute(生成器:(上下文)=>HomePage()),
);
}
设置状态(){
showSpinner=false;
});
}捕获(e){
印刷品(e);
}
}:null,//如果为false,则将其设为null
颜色:颜色(0xFF1f2032),
子:文本(
“注册”,
样式:TextStyle(字体大小:18,颜色:Colors.white),
),
),
),

我想让您了解一些事情。这肯定会对你有帮助,所以跟着做吧

指针

  • 请继续阅读,它回答了您关于在表单下显示错误消息以进行验证的问题
  • 实现所需功能的一个非常有用的小部件是,不幸的是,您无法以编程方式显示所需的工具提示
解决方法:一定要使用其中任何一种来显示您的信息

  • 只需在复选框下显示一个文本,比如表单的验证器,就像我将在代码中为您演示的那样
现在,我已经在这段代码中演示了它们,但代码并不相似。在工具提示消息showcase中告诉您可以执行的最佳实践就足够了

请注意:要使用
Container()
创建类似工具提示的结构,您可以按照答案进行操作,这将在很大程度上帮助您

class _MyHomePageState extends State<MyHomePage> {
  bool rememberMe = false;
  
  // this bool will check rememberMe is checked
  bool showErrorMessage = false;
  
  //for form Validation
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Form(
        key: _formKey,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 20.0),
              child: TextFormField(
                validator: (value) {
                  // retiurning the validator message here
                  return value.isEmpty ? "Please enter the message" : null;
                }
              )
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('Accept Terms & Conditions'),
                SizedBox(width: 20.0),
                Checkbox(
                  focusColor: Colors.lightBlue,
                  activeColor: Colors.orange,
                  value: rememberMe,
                  onChanged: (newValue) {
                    setState(() => rememberMe = newValue);
                  }
                )
              ]
            ),
            // based up on this bool value
            showErrorMessage ? 
            Container(
              decoration: BoxDecoration(
                color: Colors.red,
                borderRadius: BorderRadius.circular(80.0)
              ),
              child: Padding(
                padding: EdgeInsets.all(10.0),
                child: Text('Please accept the terms and conditions to proceed...')
              )
            )
            : Container(),
            SizedBox(height: 20.0),
            RaisedButton(
              child: Text('Submit'),
              onPressed: (){
                // for your form validation
                if(_formKey.currentState.validate()){
                  // do your success operation here!
                  // checking for the rememberValue
                  // and setting the message bool data
                  if(rememberMe != true)
                    setState(() => showErrorMessage = true);
                  else
                    setState(() => showErrorMessage = false);
                }
              }
            )
          ]
        )
      )
    );
  }
}
class\u MyHomePageState扩展状态{
bool rememberMe=false;
//此布尔值将检查Memberme是否已检查
bool-horrormessage=false;
//用于表单验证
final _formKey=GlobalKey();
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:表格(
键:_formKey,
子:列(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
填充物(
填充:边缘组。对称(水平:20.0),
子项:TextFormField(
验证器:(值){
//在此处重新启动验证程序消息
return value.isEmpty?“请输入消息”:null;
}
)
),
划船(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
文本(“接受条款和条件”),
尺寸箱(宽度:20.0),
复选框(
聚焦颜色:颜色。浅蓝色,
activeColor:Colors.orange,
价值观:记住,
一旦更改:(newValue){
设置状态(()=>rememberMe=newValue);
}
)
]
),
//基于这个布尔值
有什么消息吗?
容器(
装饰:盒子装饰(
颜色:颜色,红色,
边界半径:边界半径。圆形(80.0)
),
孩子:填充(
填充:所有边缘设置(10.0),
子项:文本('请接受继续的条款和条件…')
)
)
:Container(),
尺寸箱(高度:20.0),
升起的按钮(
子项:文本('Submit'),
已按下:(){
//用于表单验证
if(_formKey.currentState.validate()){
//你成功了吗
class _MyHomePageState extends State<MyHomePage> {
  bool rememberMe = false;
  
  // this bool will check rememberMe is checked
  bool showErrorMessage = false;
  
  //for form Validation
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Form(
        key: _formKey,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 20.0),
              child: TextFormField(
                validator: (value) {
                  // retiurning the validator message here
                  return value.isEmpty ? "Please enter the message" : null;
                }
              )
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('Accept Terms & Conditions'),
                SizedBox(width: 20.0),
                Checkbox(
                  focusColor: Colors.lightBlue,
                  activeColor: Colors.orange,
                  value: rememberMe,
                  onChanged: (newValue) {
                    setState(() => rememberMe = newValue);
                  }
                )
              ]
            ),
            // based up on this bool value
            showErrorMessage ? 
            Container(
              decoration: BoxDecoration(
                color: Colors.red,
                borderRadius: BorderRadius.circular(80.0)
              ),
              child: Padding(
                padding: EdgeInsets.all(10.0),
                child: Text('Please accept the terms and conditions to proceed...')
              )
            )
            : Container(),
            SizedBox(height: 20.0),
            RaisedButton(
              child: Text('Submit'),
              onPressed: (){
                // for your form validation
                if(_formKey.currentState.validate()){
                  // do your success operation here!
                  // checking for the rememberValue
                  // and setting the message bool data
                  if(rememberMe != true)
                    setState(() => showErrorMessage = true);
                  else
                    setState(() => showErrorMessage = false);
                }
              }
            )
          ]
        )
      )
    );
  }
}