【问题标题】:Circular progress indicator not showing in flutter圆形进度指示器未在颤动中显示
【发布时间】:2021-09-07 10:21:21
【问题描述】:

每当用户按下注册按钮时,我都会尝试显示CircularProgressIndicator,这是我的代码:

onPressed: () async{
                if (_email.isNotEmpty && _password.isNotEmpty) {
                  startProgressIndicator();
                  FirebaseAuth mAuth = FirebaseAuth.instance;
                  UserCredential credential =  await mAuth.createUserWithEmailAndPassword(email: _email, password: _password);
                  print(credential.user!.email);
                  //stopProgressIndicator();
                } else {
                  ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                    content: Text(
                      "Please enter all information!",
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: 15,
                          fontWeight: FontWeight.bold
                      ),
                    ),
                    backgroundColor: Colors.black,
                    elevation: 5,
                    duration: Duration(
                      seconds: 3
                    ),
                    action: SnackBarAction(
                      label: "close",
                      onPressed: (){
                        ScaffoldMessenger.of(context).hideCurrentSnackBar();
                      },
                    ),
                  ));
                }
              

startProgressIndicator():

CircularProgressIndicator startProgressIndicator() {
    return CircularProgressIndicator(
      valueColor: AlwaysStoppedAnimation<Color>(Colors.orange),
      backgroundColor: Colors.black,
      strokeWidth: 5,
    );
  }

stopProgressIndicator():

CircularProgressIndicator stopProgressIndicator() {
    return CircularProgressIndicator(
      value: 0,
    );
  }

加载图标根本没有出现。

所有颜色都正确(即背景颜色和进度条颜色不同)

这里有什么问题?

编辑:我添加了以下代码,但仍然无法正常工作:

Column(
  children: [
  <Other widgets>
  Visibility(
            visible: _isProgressVisible,
            child: CircularProgressIndicator(),
          )
  ]
)

我将_isProgressVisible 设置为true 并且设置为false:

if (_email.isNotEmpty && _password.isNotEmpty) {
                  setState(() {
                    _isProgressVisible = true;
                  });
                  FirebaseAuth mAuth = FirebaseAuth.instance;
                  UserCredential credential =  await mAuth.createUserWithEmailAndPassword(email: _email, password: _password);
                  print(credential.user!.email);
                  setState(() {
                    _isProgressVisible = false;
                  });
                }

【问题讨论】:

  • 奇怪的是已经给出了答案,你还没有给他们任何反馈。不太有动力回答您的新问题。

标签: flutter dart


【解决方案1】:

您没有在任何地方显示返回的小部件,startProgressIndicator() 返回了小部件,但您只是在调用而不是渲染它, 要么这样使用

showCupertinoDialog(
      useRootNavigator: true,
      barrierDismissible: false,
      context: context,
      builder: (context) {
        return startProgressIndicator();
      },
    );

如果你想在其他地方展示,你可以这样做

Container(
      margin: const EdgeInsets.all(16),
      child: Column(children: [
        Visibility(
            visible: showIndicator,
            child: startProgressIndicator()),
        const SizedBox(height: 10),
        RaisedButton(
          child: const Text('Here'),
          onPressed: () => setState(() => showIndicator = !showIndicator),
        )
      ]),
    );

【讨论】:

    【解决方案2】:
    return startProgressIndicator();
    

    这只是一个简单的错误 您必须返回小部件;

    【讨论】:

      猜你喜欢
      • 2020-08-10
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      • 2016-10-04
      • 2018-10-19
      • 2015-09-06
      • 2021-07-04
      相关资源
      最近更新 更多