Flutter 图片保存页面

narakuyang
2023-02-28 / 0 评论 / 31 阅读 / 正在检测是否收录...
class ImagesView extends StatefulWidget {
  final String pic;
  const ImagesView({Key? key, required this.pic}) : super(key: key);

  @override
  State<ImagesView> createState() => _ImagesViewState();
}

class _ImagesViewState extends State<ImagesView> {
  int downproess = 0;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Get.back();
      },
      child: comBottonBar(
        child: Scaffold(
          appBar: AppBar(
            leading: comLeading(),
            actions: [
              IconButton(
                  onPressed: () {
                    Get.bottomSheet(Container(
                      height: 120,
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(12),
                              topRight: Radius.circular(12)),
                          color: appColor.main),
                      child: SingleChildScrollView(
                        padding: EdgeInsets.all(12),
                        child: Column(
                          children: [
                            Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                toolsWidget(
                                    iconData: CocoIconBold.Arrow_Bottom_3,
                                    msg: '保存图片',
                                    ontap: () async {
                                      Get.back();
                                      try {
                                        var response = await dio.get(
                                          widget.pic,
                                          options: Options(
                                              responseType: ResponseType.bytes),
                                          onReceiveProgress: (count, total) {
                                            setState(() {
                                              downproess =
                                                  ((count / total) * 100)
                                                      .toInt();
                                            });
                                          },
                                        );
                                        final result =
                                            await ImageGallerySaver.saveImage(
                                                Uint8List.fromList(
                                                    response.data),
                                                quality: 80,
                                                name: widget.pic.replaceAll(
                                                    'https://', ''));
                                        comToast(msg: '保存成功');
                                      } on DioError catch (e) {
                                        comToast(msg: '保存失败');
                                        Exception(e);
                                      }
                                      vibrate();
                                    }),
                              ],
                            ),
                            const SizedBox(height: 12),
                            TextButton(
                                onPressed: () {
                                  Get.back();
                                },
                                child: Text(
                                  '关闭',
                                  style: TextStyle(color: appColor.sub),
                                ))
                          ],
                        ),
                      ),
                    ));
                  },
                  icon: Icon(
                    CocoIconBold.Menu_1,
                    size: 20,
                  ))
            ],
          ),
          body: Stack(alignment: Alignment.center, children: [
            Padding(
              padding: const EdgeInsets.all(12.0),
              child: Center(
                  child: PhotoView(
                loadingBuilder: (context, event) => Center(
                  child: comLoading(),
                ),
                imageProvider: CachedNetworkImageProvider(widget.pic),
                backgroundDecoration: BoxDecoration(),
              )),
            ),
            Visibility(
              visible: downproess == 0 || downproess == 100 ? false : true,
              child: CircularProgressIndicator(
                value: downproess / 100,
                backgroundColor: appColor.black.withOpacity(0.02),
                valueColor: AlwaysStoppedAnimation<Color>(
                  (appColor.sub),
                ),
              ),
            )
          ]),
        ),
      ),
    );
  }
}
0

评论

博主关闭了所有页面的评论