How to work with progress indicator in flutter?

后端 未结 12 804
面向向阳花
面向向阳花 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:09

    1. Without plugin

        class IndiSampleState extends State {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
            appBar: new AppBar(
              title: new Text('Demo'),
            ),
            body: Center(
              child: RaisedButton(
                color: Colors.blueAccent,
                child: Text('Login'),
                onPressed: () async {
                  showDialog(
                      context: context,
                      builder: (BuildContext context) {
                        return Center(child: CircularProgressIndicator(),);
                      });
                  await loginAction();
                  Navigator.pop(context);
                },
              ),
            ));
      }
    
      Future loginAction() async {
        //replace the below line of code with your login request
        await new Future.delayed(const Duration(seconds: 2));
        return true;
      }
    }
    

    2. With plugin

    check this plugin progress_hud

    add the dependency in the pubspec.yaml file

    dev_dependencies:
      progress_hud: 
    

    import the package

    import 'package:progress_hud/progress_hud.dart';
    

    Sample code is given below to show and hide the indicator

    class ProgHudPage extends StatefulWidget {
      @override
      _ProgHudPageState createState() => _ProgHudPageState();
    }
    
    class _ProgHudPageState extends State {
      ProgressHUD _progressHUD;
      @override
      void initState() {
        _progressHUD = new ProgressHUD(
          backgroundColor: Colors.black12,
          color: Colors.white,
          containerColor: Colors.blue,
          borderRadius: 5.0,
          loading: false,
          text: 'Loading...',
        );
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
            appBar: new AppBar(
              title: new Text('ProgressHUD Demo'),
            ),
            body: new Stack(
              children: [
                _progressHUD,
                new Positioned(
                    child: RaisedButton(
                      color: Colors.blueAccent,
                      child: Text('Login'),
                      onPressed: () async{
                        _progressHUD.state.show();
                        await loginAction();
                        _progressHUD.state.dismiss();
                      },
                    ),
                    bottom: 30.0,
                    right: 10.0)
              ],
            ));
      }
    
      Future loginAction()async{
        //replace the below line of code with your login request
        await new Future.delayed(const Duration(seconds: 2));
        return true;
      }
    }
    

提交回复
热议问题