0

我有一个登录页面,我想在点击按钮CupertinoActivityIndicator时将其更改为 a 。这是我的代码:

class AuthRegister extends StatefulWidget {
  AuthRegister({Key key, this.authenticator}): super(key: key);

  final FirebaseAuth authenticator;

  @override
  _AuthRegisterState createState() => _AuthRegisterState();
}

class _AuthRegisterState extends State<AuthRegister> {

  final _formKey = GlobalKey<FormState>();
  final _emailController = TextEditingController();
  final _passwordController = TextEditingController();

  bool _loading = false;


  void _register(context) async {
    setState(() {
      _loading = true;
    });
    try {
      sleep(const Duration(seconds: 2));
      throw ("Debug");
    } catch (e) {
      print(e);
      setState(() {
        _loading = false;
      });
      Fluttertoast.showToast(msg: e);
    }
  }

  @override
  void dispose() {
    _emailController.dispose();
    _passwordController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {

    // I even tried this and it still fails
    // if (_loading) {
    //   return CupertinoActivityIndicator();
    // }

    return CupertinoPageScaffold(
      child: Form(
        key: _formKey,
        child: ListView(children: [
          Text('CREATE AN ACCOUNT'),
          Padding(
              padding: EdgeInsets.symmetric(horizontal: 20),
              child: MyInput(type: 'email', controller: _emailController)),
          Padding(
              padding: EdgeInsets.symmetric(horizontal: 20),
              child: MyInput(type: 'password', controller: _passwordController)),
          Padding(
            padding: EdgeInsets.only(top: 36, bottom: 14, left: 45, right: 45),

            // HERE ---- \/ \/ ----

            child: (_loading) ?
              CupertinoActivityIndicator() :
              FullButton(
                onPressed: () {
                  // I tried doing setState here but it didn't work
                  // setState(() {
                  //   _loading = true;
                  // });
                  _register(context);
                },
                text: 'CREATE AN ACCOUNT',
                fillColor: true),
            ),

          // /HERE ---- /\ /\ ----

          Center(
            child: Text(
              'FORGOT PASSWORD?',
              style: TextStyle(
                  color: Color.fromRGBO(0xEE, 0x56, 0x42, 1.0),
                  fontFamily: 'Montserrat',
                  fontSize: 12),
            ),
          ),
        ]),
      ),
    );
  }
}

如您所见,我尝试了不同的方法。我一直在网上搜索,无论我在哪里看,似乎我都做对了。在与我有同样问题的人中,我所能找到的只是用于执行条件的布尔变量在某些函数中本地声明或使用 StatelessWidgets 的人等的情况......

我究竟做错了什么 ?

4

1 回答 1

2

代替

sleep(const Duration(seconds: 2));

Future.delayed(Duration(seconds: 2)).then((_) {
  throw ("Debug");
});
于 2020-11-25T15:37:30.290 回答