How to work with progress indicator in flutter?

后端 未结 12 808
面向向阳花
面向向阳花 2020-12-02 05:29

I\'m newbie in flutter and wanted to know what is better way to add CircularProgressIndicator in my layout. For example, my login view. This view have username,

12条回答
  •  无人及你
    2020-12-02 06:03

    For me, one neat way to do this is to show a SnackBar at the bottom while the Signing-In process is taken place, this is a an example of what I mean:

    Here is how to setup the SnackBar.

    Define a global key for your Scaffold

    final GlobalKey _scaffoldKey = new GlobalKey();
    

    Add it to your Scaffold key attribute

    return new Scaffold(
          key: _scaffoldKey,
    .......
    

    My SignIn button onPressed callback:

    onPressed: () {
                      _scaffoldKey.currentState.showSnackBar(
                          new SnackBar(duration: new Duration(seconds: 4), content:
                          new Row(
                            children: [
                              new CircularProgressIndicator(),
                              new Text("  Signing-In...")
                            ],
                          ),
                          ));
                      _handleSignIn()
                          .whenComplete(() =>
                          Navigator.of(context).pushNamed("/Home")
                      );
                    }
    

    It really depends on how you want to build your layout, and I am not sure what you have in mind.

    Edit

    You probably want it this way, I have used a Stack to achieve this result and just show or hide my indicator based on onPressed

    class TestSignInView extends StatefulWidget {
      @override
      _TestSignInViewState createState() => new _TestSignInViewState();
    }
    
    
    class _TestSignInViewState extends State {
      bool _load = false;
      @override
      Widget build(BuildContext context) {
        Widget loadingIndicator =_load? new Container(
          color: Colors.grey[300],
          width: 70.0,
          height: 70.0,
          child: new Padding(padding: const EdgeInsets.all(5.0),child: new Center(child: new CircularProgressIndicator())),
        ):new Container();
        return new Scaffold(
          backgroundColor: Colors.white,
          body:  new Stack(children: [new Padding(
            padding: const EdgeInsets.symmetric(vertical: 50.0, horizontal: 20.0),
            child: new ListView(
    
              children: [
                new Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center
                  ,children: [
                new TextField(),
                new TextField(),
    
                new FlatButton(color:Colors.blue,child: new Text('Sign In'),
                    onPressed: () {
                  setState((){
                    _load=true;
                  });
    
                      //Navigator.of(context).push(new MaterialPageRoute(builder: (_)=>new HomeTest()));
                    }
                ),
    
                ],),],
            ),),
            new Align(child: loadingIndicator,alignment: FractionalOffset.center,),
    
          ],));
      }
    
    }
    

提交回复
热议问题