flutter - 当我按下下载按钮时,如何显示带有CircularProgressIndicator的弹出窗口和下载状态

标签 flutter dart flutter-layout dart-pub

我正在尝试创建一个可以下载图像的应用程序。我想在按下“下载”按钮时显示一个带有CircularProgressIndicator和“下载状态”的弹出窗口。每当我按下“下载”按钮时,图像就会在后台下载,但它不会在屏幕上显示任何内容。

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

class HomePage extends StatefulWidget {
 @override
 _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
 final imgUrl =
     'https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80';
 bool downloading = false;
 var progressString = " ";

 @override
 void initState() {
   super.initState();
   downloadFile();
 }

 // Downloading image using Dio package
 Future<void> downloadFile() async {
   Dio dio = Dio();
   try {
     var dir = await getApplicationDocumentsDirectory();
     await dio.download(imgUrl, "${dir.path}/DownloadedImage.jpg",
         onReceiveProgress: (rec, total) {
       print("rec: $rec, total: $total");
       setState(() {
         downloading = true;
         progressString = ((rec / total) * 100).toStringAsFixed(0) + "%";
       });
     });
   } catch (e) {
     print(e);
   }

   setState(() {
     progressString = "Download Completed";
     downloading = false;
   });
 }

 @override
 Widget build(BuildContext context) {
   return Scaffold(
     body: Container(
         child: Column(
       mainAxisAlignment: MainAxisAlignment.center,
       children: <Widget>[
         Center(
           child: RaisedButton.icon(
             onPressed: () {
               downloadFile();
             },
             icon: Icon(Icons.file_download),
             label: Text('Download'),
             color: Colors.blueAccent,
           ),
         )
       ],
     )),
   );
 }
}

我想在按下按钮时显示此容器
  Container(
                 height: 120.0,
                 width: 200.0,
                 child: Card(
                   color: Colors.black54,
                   child: Column(
                     mainAxisAlignment: MainAxisAlignment.center,
                     children: <Widget>[
                       CircularProgressIndicator(),
                       SizedBox(
                         height: 10.0,
                       ),
                       Text('Downloading $progressString'),
                     ],
                   ),
                 ),
               );

最佳答案

您可以使用showDialog显示一个弹出对话框。

static Future showDialog(BuildContext context, GlobalKey _key) async   {
  return showLoadingDialog(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext context){
      return SimpleDialog(
          key: _key,
          children: <Widget>[
            Center(
              child: Container(
                child: Row(
                children: <Widget>[
                  CircularProgressIndicator(),
                  SizedBox(
                    height:10,
                    width:10,
                  ),
                  Text("Please Wait!"),
                ]
             ),
          ],
        ),
      );
    }
  );
}

此处GlobalKey用于对创建的对话框进行Showhide

为了简单地调用它,请将类中的GlobalKey初始化为GlobalKey<State> _dialogKey = GlobalKey<State>();

然后,您可以将对话框显示为:
showLoadingDialog(context, _dialogKey);

当您希望隐藏它时,只需调用:
Navigator.of(_dialogKey.currentContext,rootNavigator: true).pop();

希望这可以帮助。

关于flutter - 当我按下下载按钮时,如何显示带有CircularProgressIndicator的弹出窗口和下载状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62003522/

相关文章:

firebase - 我如何在Flutter中将数据从一个小部件传递到另一个小部件?

flutter - 在单个 TextField 中为多行添加下划线 - Flutter/Dart

flutter - 更改onPressed上按钮的背景颜色和文本颜色,并在未按下Flutter时重新设置为默认值

flutter - 如何自定义对话框的位置、大小和背景?

android - Flutter C++ 内存分配导致光栅线程卡顿 - Android NDK Dart FFI

flutter - 如何在 Flutter 中滚动时为多个小部件设置动画

flutter - 如何在另一个页面(收藏夹页面)的另一个列表中显示共享首选项中保存的列表?

flutter - flutter 中的 Stripe 实现

dart - 在 flutter/dart 中获取视频细节

dart - Flutter 应用程序中的远程配置在获取时引发异常