How to work with progress indicator in flutter?

后端 未结 12 762
面向向阳花
面向向阳花 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 05:43

    This is my solution with stack

    import 'package:flutter/material.dart';
    import 'package:shared_preferences/shared_preferences.dart';
    import 'dart:async';
    
    final themeColor = new Color(0xfff5a623);
    final primaryColor = new Color(0xff203152);
    final greyColor = new Color(0xffaeaeae);
    final greyColor2 = new Color(0xffE8E8E8);
    
    class LoadindScreen extends StatefulWidget {
      LoadindScreen({Key key, this.title}) : super(key: key);
      final String title;
      @override
      LoginScreenState createState() => new LoginScreenState();
    }
    
    class LoginScreenState extends State<LoadindScreen> {
      SharedPreferences prefs;
    
      bool isLoading = false;
    
      Future<Null> handleSignIn() async {
        setState(() {
          isLoading = true;
        });
        prefs = await SharedPreferences.getInstance();
        var isLoadingFuture = Future.delayed(const Duration(seconds: 3), () {
          return false;
        });
        isLoadingFuture.then((response) {
          setState(() {
            isLoading = response;
          });
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text(
                widget.title,
                style: TextStyle(color: primaryColor, fontWeight: FontWeight.bold),
              ),
              centerTitle: true,
            ),
            body: Stack(
              children: <Widget>[
                Center(
                  child: FlatButton(
                      onPressed: handleSignIn,
                      child: Text(
                        'SIGN IN WITH GOOGLE',
                        style: TextStyle(fontSize: 16.0),
                      ),
                      color: Color(0xffdd4b39),
                      highlightColor: Color(0xffff7f7f),
                      splashColor: Colors.transparent,
                      textColor: Colors.white,
                      padding: EdgeInsets.fromLTRB(30.0, 15.0, 30.0, 15.0)),
                ),
    
                // Loading
                Positioned(
                  child: isLoading
                      ? Container(
                          child: Center(
                            child: CircularProgressIndicator(
                              valueColor: AlwaysStoppedAnimation<Color>(themeColor),
                            ),
                          ),
                          color: Colors.white.withOpacity(0.8),
                        )
                      : Container(),
                ),
              ],
            ));
      }
    }
    
    0 讨论(0)
  • 2020-12-02 05:49

    You can use FutureBuilder widget instead. This takes an argument which must be a Future. Then you can use a snapshot which is the state at the time being of the async call when loging in, once it ends the state of the async function return will be updated and the future builder will rebuild itself so you can then ask for the new state.

    FutureBuilder(
      future:  myFutureFunction(),
      builder: (context, AsyncSnapshot<List<item>> snapshot) {
        if (!snapshot.hasData) {
          return Center(
            child: CircularProgressIndicator(),
          );
        } else {
         //Send the user to the next page.
      },
    );
    

    Here you have an example on how to build a Future

    Future<void> myFutureFunction() async{
     await callToApi();}
    
    0 讨论(0)
  • 2020-12-02 05:50

    You can do it for center transparent progress indicator

    Future<Null> _submitDialog(BuildContext context) async {
      return await showDialog<Null>(
          context: context,
          barrierDismissible: false,
          builder: (BuildContext context) {
            return SimpleDialog(
              elevation: 0.0,
              backgroundColor: Colors.transparent,
              children: <Widget>[
                Center(
                  child: CircularProgressIndicator(),
                )
              ],
            );
          });
    }
    
    0 讨论(0)
  • 2020-12-02 05:53

    I took the following approach, which uses a simple modal progress indicator widget that wraps whatever you want to make modal during an async call.

    The example in the package also addresses how to handle form validation while making async calls to validate the form (see flutter/issues/9688 for details of this problem). For example, without leaving the form, this async form validation method can be used to validate a new user name against existing names in a database while signing up.

    https://pub.dartlang.org/packages/modal_progress_hud

    Here is the demo of the example provided with the package (with source code):

    Example could be adapted to other modal progress indicator behaviour (like different animations, additional text in modal, etc..).

    0 讨论(0)
  • 2020-12-02 05:55

    Create a bool isLoading and set it to false. With the help of ternary operator, When user clicks on login button set state of isLoading to true. You will get circular loading indicator in place of login button

     isLoading ? new PrimaryButton(
                          key: new Key('login'),
                          text: 'Login',
                          height: 44.0,
                          onPressed: setState((){isLoading = true;}))
                      : Center(
                          child: CircularProgressIndicator(),
                        ),
    

    You can see Screenshots how it looks while before login is clicked

    After login is clicked

    In mean time you can run login process and login user. If user credentials are wrong then again you will setState of isLoading to false, such that loading indicator will become invisible and login button visible to user. By the way, primaryButton used in code is my custom button. You can do same with OnPressed in button.

    0 讨论(0)
  • 2020-12-02 06:02
    {
    isloading? progressIos:Container()
    
    progressIos(int i) {
        return Container(
            color: i == 1
                ? AppColors.liteBlack
                : i == 2 ? AppColors.darkBlack : i == 3 ? AppColors.pinkBtn : '',
            child: Center(child: CupertinoActivityIndicator()));
      }
    }
    
    0 讨论(0)
提交回复
热议问题