From 481387e8fbebf2eb0bbc07d22849fdbb4bfb7539 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Tue, 12 Oct 2021 01:06:49 +0800 Subject: [PATCH] Refactoring: extract widget class --- lib/widget/sharing_browser.dart | 136 +++++++++++++++++++------------- 1 file changed, 82 insertions(+), 54 deletions(-) diff --git a/lib/widget/sharing_browser.dart b/lib/widget/sharing_browser.dart index 8222f60f..e31376e1 100644 --- a/lib/widget/sharing_browser.dart +++ b/lib/widget/sharing_browser.dart @@ -155,7 +155,35 @@ class _SharingBrowserState extends State { final dateStr = DateFormat(DateFormat.YEAR_ABBR_MONTH_DAY, Localizations.localeOf(context).languageCode) .format(shares.first.share.stime.toLocal()); - return InkWell( + return _ListTile( + leading: shares.first.share.itemType == ShareItemType.folder + ? const Icon( + Icons.folder_outlined, + size: leadingSize, + ) + : CachedNetworkImage( + width: leadingSize, + height: leadingSize, + cacheManager: ThumbnailCacheManager.inst, + imageUrl: api_util.getFilePreviewUrl( + widget.account, shares.first.file, + width: k.photoThumbSize, height: k.photoThumbSize), + httpHeaders: { + "Authorization": + Api.getAuthorizationHeaderValue(widget.account), + }, + fadeInDuration: const Duration(), + filterQuality: FilterQuality.high, + imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet, + ), + label: shares.first.share.filename, + description: shares.first.share.uidOwner == widget.account.username + ? L10n.global().fileLastSharedDescription(dateStr) + : L10n.global().fileLastSharedByOthersDescription( + shares.first.share.displaynameOwner, dateStr), + trailing: (shares.any((element) => element.share.url?.isNotEmpty == true)) + ? const Icon(Icons.link) + : null, onTap: () { Navigator.of(context).pushNamed(SharedFileViewer.routeName, arguments: SharedFileViewerArguments( @@ -164,59 +192,6 @@ class _SharingBrowserState extends State { shares.map((e) => e.share).toList(), )); }, - child: Padding( - padding: const EdgeInsets.all(16), - child: Row( - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - shares.first.share.itemType == ShareItemType.folder - ? const Icon( - Icons.folder_outlined, - size: leadingSize, - ) - : CachedNetworkImage( - width: leadingSize, - height: leadingSize, - cacheManager: ThumbnailCacheManager.inst, - imageUrl: api_util.getFilePreviewUrl( - widget.account, shares.first.file, - width: k.photoThumbSize, height: k.photoThumbSize), - httpHeaders: { - "Authorization": - Api.getAuthorizationHeaderValue(widget.account), - }, - fadeInDuration: const Duration(), - filterQuality: FilterQuality.high, - imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet, - ), - const SizedBox(width: 16), - Expanded( - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - shares.first.share.filename, - style: Theme.of(context).textTheme.subtitle1, - maxLines: 1, - overflow: TextOverflow.ellipsis, - ), - Text( - shares.first.share.uidOwner == widget.account.username - ? L10n.global().fileLastSharedDescription(dateStr) - : L10n.global().fileLastSharedByOthersDescription( - shares.first.share.displaynameOwner, dateStr), - style: TextStyle( - color: AppTheme.getSecondaryTextColor(context), - ), - ), - ], - ), - ), - if (shares.any((element) => element.share.url?.isNotEmpty == true)) - const Icon(Icons.link) - ], - ), - ), ); } @@ -270,3 +245,56 @@ class _SharingBrowserState extends State { static final _log = Logger("widget.sharing_browser._SharingBrowserState"); } + +class _ListTile extends StatelessWidget { + const _ListTile({ + required this.leading, + required this.label, + required this.description, + this.trailing, + required this.onTap, + }); + + @override + build(BuildContext context) { + return InkWell( + onTap: onTap, + child: Padding( + padding: const EdgeInsets.all(16), + child: Row( + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + leading, + const SizedBox(width: 16), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + label, + style: Theme.of(context).textTheme.subtitle1, + maxLines: 1, + overflow: TextOverflow.ellipsis, + ), + Text( + description, + style: TextStyle( + color: AppTheme.getSecondaryTextColor(context), + ), + ), + ], + ), + ), + if (trailing != null) trailing!, + ], + ), + ), + ); + } + + final Widget leading; + final String label; + final String description; + final Widget? trailing; + final VoidCallback onTap; +}