【问题标题】:Flutter: Progress dialog is not hidingFlutter:进度对话框没有隐藏
【发布时间】:2021-12-27 17:32:09
【问题描述】:

我正在使用progress_dialog 1.2.0 包在我的应用程序中显示一个进度对话框,当我调用pr.show() 时它会显示,但当我调用pr.hide() 时它不会隐藏:

onTap: () async {
    pr.show();

    print('clicked custom category');
    print(categorylist[index].catName);
    print(categorylist[index].catId);

    // await getAllProductsInCategory(categorylist[index].catId);

    setState(() {
        catId = categorylist[index].catId;
        myinitlist.clear();
        myinitlist = List.from(productList);
        pr.hide();
    });
},

当我取消注释 getAllProductsInCategory() 函数时,它会隐藏对话框。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    尝试:

    onTap: () async {
        pr.show();
    
        print('clicked custom category');
        print(categorylist[index].catName);
        print(categorylist[index].catId);
    
        setState(() {
          catId = categorylist[index].catId;
          myinitlist.clear();
          myinitlist = List.from(productList);
    
           Future.delayed(Duration(seconds: 3)).then((value) {
             pr.hide().whenComplete(() {
              print(pr.isShowing());
            });
           });
        });
     },
    

    或:

    onTap: () async {
        pr.show();
    
        print('clicked custom category');
        print(categorylist[index].catName);
        print(categorylist[index].catId);
    
       Future.delayed(Duration(seconds: 3)).then((value) {
        setState(() {
          catId = categorylist[index].catId;
          myinitlist.clear();
          myinitlist = List.from(productList);
    
    
             pr.hide().whenComplete(() {
              print(pr.isShowing());
            });
           });
        });
     },
    

    【讨论】:

    • 非常感谢它有效。你能告诉我为什么没有 Future.delayed 就不能工作。
    • 它仍然无法在我这边工作,所以我不得不使用我自己的自定义小部件来显示进度。
    【解决方案2】:

    当您使用异步调用启动progressDialog & hide时,请使用await关键字:

    await progressDialog.show();
    await progressDialog.hide();
    

    示例:

    添加包:

    dependencies:
      progress_dialog: ^1.2.4
    
    import 'package:progress_dialog/progress_dialog.dart';
    

    build() 方法中创建并初始化一个ProgressDialog 对象,将上下文传递给它。 初始化ProgressDialog对象:

    final ProgressDialog pr = ProgressDialog(context);
    

    默认情况下,它是一个显示一些消息的普通对话框,如果你想用它来显示完成的百分比,指定可选的type参数并指定你是否希望你的对话框在按下后退按钮时关闭isDismissible参数(可选):

    //For normal dialog
    pr = ProgressDialog(context,type: ProgressDialogType.Normal, isDismissible: true/false, showLogs: true/false);
        
    //For showing progress percentage
    pr =  ProgressDialog(context,type: ProgressDialogType.Download, isDismissible: true/false, showLogs: true/false);
    > Note: Please initialize the ```ProgressDialog```, where you have availability of the context
    Style the progress dialog (Optional)
    pr.style(
      message: 'Downloading file...',
      borderRadius: 10.0,
      backgroundColor: Colors.white,
      progressWidget: CircularProgressIndicator(),
      elevation: 10.0,
      insetAnimCurve: Curves.easeInOut,
      progress: 0.0,
      textDirection: TextDirection.rtl,
      maxProgress: 100.0,
      progressTextStyle: TextStyle(
         color: Colors.black, fontSize: 13.0, fontWeight: FontWeight.w400),
      messageTextStyle: TextStyle(
         color: Colors.black, fontSize: 19.0, fontWeight: FontWeight.w600)
      );
    

    注意:您不需要使用所有参数,它们都是可选的。

    显示进度对话框:

    await pr.show();
    Dynamically update the content shown out there
    pr.update(
      progress: 50.0,
      message: "Please wait...",
      progressWidget: Container(
        padding: EdgeInsets.all(8.0), child: CircularProgressIndicator()),
      maxProgress: 100.0,
      progressTextStyle: TextStyle(
        color: Colors.black, fontSize: 13.0, fontWeight: FontWeight.w400),
      messageTextStyle: TextStyle(
        color: Colors.black, fontSize: 19.0, fontWeight: FontWeight.w600),
      );
    

    关闭进度对话框:

    pr.hide().then((isHidden) {
      print(isHidden);
    });
    
    // or
    await pr.hide();
    

    导航到下一个屏幕必须在完成 Future - hide() 之后完成。例如,请参见此处。 检查是否显示进度对话框:

    bool isProgressDialogShowing = pr.isShowing();
    print(isProgressDialogShowing);
    Use custom body
        pr = ProgressDialog(
          context,
          type: ProgressDialogType.Normal,
          isDismissible: true,
          /// your body here
          customBody: LinearProgressIndicator(
            valueColor: AlwaysStoppedAnimation<Color>(Colors.blueAccent),
            backgroundColor: Colors.white,
          ),
        );
    

    更多详情:https://flutterrepos.com/repo/fayaz07-progress_dialog-

    【讨论】:

      猜你喜欢
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-05
      相关资源
      最近更新 更多