From 3f288f6d4c998448c6b81d08387b2526aedeb953 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Mon, 5 Aug 2024 22:48:59 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Improve=20`LocallyAvailabl?= =?UTF-8?q?eBuilder`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../widgets/locally_available_builder.dart | 66 ++++++++++--------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/lib/src/widgets/locally_available_builder.dart b/lib/src/widgets/locally_available_builder.dart index 9af961e..09fe10a 100644 --- a/lib/src/widgets/locally_available_builder.dart +++ b/lib/src/widgets/locally_available_builder.dart @@ -14,13 +14,19 @@ final class LocallyAvailableBuilder extends StatefulWidget { const LocallyAvailableBuilder({ super.key, required this.asset, - required this.builder, this.isOriginal = true, + required this.builder, + this.progressBuilder, }); final AssetEntity asset; - final Widget Function(BuildContext context, AssetEntity asset) builder; final bool isOriginal; + final Widget Function(BuildContext context, AssetEntity asset) builder; + final Widget Function( + BuildContext context, + PMRequestState? state, + double? progress, + )? progressBuilder; @override State createState() => @@ -86,36 +92,35 @@ class _LocallyAvailableBuilderState extends State { }); } - Widget _indicator(BuildContext context) { + Widget _buildIndicator(BuildContext context) { return StreamBuilder( - stream: _progressHandler!.stream, + stream: _progressHandler?.stream, initialData: const PMProgressState(0, PMRequestState.prepare), builder: (BuildContext c, AsyncSnapshot s) { - if (s.hasData) { - final double progress = s.data!.progress; - final PMRequestState state = s.data!.state; - return Row( - mainAxisSize: MainAxisSize.min, - children: [ - Icon( - state == PMRequestState.failed - ? Icons.cloud_off - : Icons.cloud_queue, - color: context.iconTheme.color?.withOpacity(.4), - size: 28, - ), - if (state != PMRequestState.success && - state != PMRequestState.failed) - ScaleText( - ' iCloud ${(progress * 100).toInt()}%', - style: TextStyle( - color: context.textTheme.bodyMedium?.color?.withOpacity(.4), - ), - ), - ], - ); + final PMRequestState? state = s.data?.state; + final double? progress = s.data?.progress; + if (widget.progressBuilder case final builder?) { + return builder(context, state, progress); } - return const SizedBox.shrink(); + return Row( + children: [ + Icon( + state == PMRequestState.failed + ? Icons.cloud_off + : Icons.cloud_queue, + color: context.iconTheme.color?.withOpacity(.4), + size: 28, + ), + if (state != PMRequestState.success && + state != PMRequestState.failed) + ScaleText( + ' iCloud ${((progress ?? 0) * 100).toInt()}%', + style: TextStyle( + color: context.textTheme.bodyMedium?.color?.withOpacity(.4), + ), + ), + ], + ); }, ); } @@ -125,9 +130,6 @@ class _LocallyAvailableBuilderState extends State { if (_isLocallyAvailable) { return widget.builder(context, widget.asset); } - if (_progressHandler != null) { - return Center(child: _indicator(context)); - } - return const SizedBox.shrink(); + return Center(child: FittedBox(child: _buildIndicator(context))); } }