单击按钮后使文本小部件可见

Making a Text Widget visible after button click

我有一个基本的登录页面,由 'email'、'password' 和 'SwitchListTile'(切换)组成,用户将 select 确认阅读法律文件.在 'Login' 上,我希望显示 'Text'。默认是不可见的。

我使用 'Visibility' class 实现了文本的可见性。现在我想使用 'if-else' 检查使其可见。

        SwitchListTile(
          value: _acceptTerms,
          onChanged: (bool value) {
            setState(() {
              _acceptTerms = value;
            });
          },
          title: Text('Accept T&Cs'),
        ),
        SizedBox(height: 5.0),
        Visibility(
            visible: false,
            child: Text(
              'Please accept Terms & Conditions!',
              style: TextStyle(color: Colors.red),
            )),
        SizedBox(
          height: 10.0,
        ),
        RaisedButton(
          color: Theme.of(context).primaryColor,
          textColor: Colors.white,
          child: Text('LOGIN'),
          onPressed: () {
            print(_emailValue);
            print(_passwordValue);
            if(_acceptTerms) {
              Navigator.pushReplacementNamed(context, '/products');
            } else {
              //code to make invisible text visible
            }
          },
        ),

我对 Flutter 和 Dart 开发还是个新手。谢谢

在 flutter 中做到这一点的唯一方法是创建一个变量

var tcVisibility = false

将文本框的可见性设置为变量

Visibility(
     visible: tcVisibility,
     child: Text(
     'Please accept Terms & Conditions!',
     style: TextStyle(color: Colors.red),
)),

然后在代码中更新变量

if(_acceptTerms) {
     Navigator.pushReplacementNamed(context, '/products');
} else {
    setState(() {
         tcVisibility = true;
    });
}

答案已更新