Skip to content

An open-source cross-platform alternative to AirDrop

feat: show more thumbnails

Tien Do Nam623493ce0b80
diff --git a/lib/model/cross_file.dart b/lib/model/cross_file.dart
index bdea9731..0ed1f20f 100644
--- a/lib/model/cross_file.dart
+++ b/lib/model/cross_file.dart
@@ -1,4 +1,3 @@
-import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:localsend_app/model/file_type.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
@@ -14,17 +13,6 @@ class CrossFile with _$CrossFile {
required int size,
required AssetEntity? asset, // for thumbnails
required String? path,
- required Future<List<int>> Function()? bytes, // always UTF-8 encoded for messages
+ required List<int>? bytes, // if type message, then UTF-8 encoded
}) = _CrossFile;
}
-
-extension AssetEntityExt on AssetEntity {
- Widget get thumbnailWidget {
- return AssetEntityImage(
- this,
- isOriginal: false,
- thumbnailSize: const ThumbnailSize.square(32),
- thumbnailFormat: ThumbnailFormat.jpeg,
- );
- }
-}
diff --git a/lib/pages/selected_files_page.dart b/lib/pages/selected_files_page.dart
index d6a2ecc0..c7bd32c0 100644
--- a/lib/pages/selected_files_page.dart
+++ b/lib/pages/selected_files_page.dart
@@ -1,10 +1,13 @@
+import 'dart:convert';
+
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:localsend_app/gen/strings.g.dart';
-import 'package:localsend_app/model/cross_file.dart';
+import 'package:localsend_app/model/file_type.dart';
import 'package:localsend_app/provider/selected_files_provider.dart';
import 'package:localsend_app/util/file_size_helper.dart';
+import 'package:localsend_app/widget/file_thumbnail.dart';
import 'package:routerino/routerino.dart';
class SelectedFilesPage extends ConsumerWidget {
@@ -52,13 +55,19 @@ class SelectedFilesPage extends ConsumerWidget {
padding: const EdgeInsets.all(10),
child: Row(
children: [
- file.asset != null ? file.asset!.thumbnailWidget : Icon(file.fileType.icon, size: 32),
+ FileThumbnail(file),
const SizedBox(width: 10),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
- Text(file.name),
+ Text(
+ file.fileType == FileType.text && file.bytes != null
+ ? '"${utf8.decode(file.bytes!).replaceAll('\n', ' ')}"'
+ : file.name,
+ maxLines: 1,
+ overflow: TextOverflow.ellipsis,
+ ),
Text(file.size.asReadableFileSize, style: Theme.of(context).textTheme.caption),
],
),
diff --git a/lib/pages/tabs/send_tab.dart b/lib/pages/tabs/send_tab.dart
index cc9e375a..b1d5157a 100644
--- a/lib/pages/tabs/send_tab.dart
+++ b/lib/pages/tabs/send_tab.dart
@@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:localsend_app/gen/strings.g.dart';
-import 'package:localsend_app/model/cross_file.dart';
import 'package:localsend_app/model/device.dart';
import 'package:localsend_app/pages/selected_files_page.dart';
import 'package:localsend_app/provider/device_info_provider.dart';
@@ -17,6 +16,7 @@ import 'package:localsend_app/widget/big_button.dart';
import 'package:localsend_app/widget/dialogs/add_file_dialog.dart';
import 'package:localsend_app/widget/dialogs/address_input_dialog.dart';
import 'package:localsend_app/widget/dialogs/no_files_dialog.dart';
+import 'package:localsend_app/widget/file_thumbnail.dart';
import 'package:localsend_app/widget/list_tile/device_list_tile.dart';
import 'package:localsend_app/widget/rotating_widget.dart';
import 'package:routerino/routerino.dart';
@@ -132,7 +132,7 @@ class _SendTabState extends ConsumerState<SendTab> {
...selectedFiles.map((file) {
return Padding(
padding: const EdgeInsets.only(right: 10),
- child: file.asset != null ? file.asset!.thumbnailWidget : Icon(file.fileType.icon, size: 32),
+ child: FileThumbnail(file),
);
}),
],
@@ -143,18 +143,17 @@ class _SendTabState extends ConsumerState<SendTab> {
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
- TextButton.icon(
+ TextButton(
style: TextButton.styleFrom(
foregroundColor: Theme.of(context).colorScheme.onSurface,
),
onPressed: () {
context.push(() => const SelectedFilesPage());
},
- icon: const Icon(Icons.edit),
- label: Text(t.general.edit),
+ child: Text(t.general.edit),
),
const SizedBox(width: 15),
- ElevatedButton.icon(
+ ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context).colorScheme.primary,
foregroundColor: Theme.of(context).colorScheme.onPrimary,
@@ -169,8 +168,7 @@ class _SendTabState extends ConsumerState<SendTab> {
options: addOptions,
));
},
- icon: const Icon(Icons.add),
- label: Text(t.general.add),
+ child: Text(t.general.add),
),
const SizedBox(width: 15),
],
diff --git a/lib/provider/network/send_provider.dart b/lib/provider/network/send_provider.dart
index 67bd9d7b..d2bb701e 100644
--- a/lib/provider/network/send_provider.dart
+++ b/lib/provider/network/send_provider.dart
@@ -56,13 +56,13 @@ class SendNotifier extends StateNotifier<SendState?> {
size: file.size,
fileType: file.fileType,
preview: files.length == 1 && files.first.fileType == FileType.text && files.first.bytes != null
- ? utf8.decode(await files.first.bytes!()) // send simple message by embedding it into the preview
+ ? utf8.decode(files.first.bytes!) // send simple message by embedding it into the preview
: null,
),
status: FileStatus.queue,
token: null,
path: file.path,
- bytes: file.bytes != null ? await file.bytes!() : null,
+ bytes: file.bytes,
),
);
}))),
diff --git a/lib/provider/selected_files_provider.dart b/lib/provider/selected_files_provider.dart
index 360eeaf9..f1e7a6d8 100644
--- a/lib/provider/selected_files_provider.dart
+++ b/lib/provider/selected_files_provider.dart
@@ -33,7 +33,7 @@ class SelectedFilesNotifier extends StateNotifier<List<CrossFile>> {
size: bytes.length,
asset: null,
path: null,
- bytes: () async => bytes,
+ bytes: bytes,
),
]);
}
@@ -69,7 +69,7 @@ class CrossFileConverters {
size: file.size,
asset: null,
path: kIsWeb ? null : file.path,
- bytes: kIsWeb ? () async => file.bytes! : null,
+ bytes: kIsWeb ? file.bytes! : null,
);
}
@@ -92,7 +92,7 @@ class CrossFileConverters {
size: await file.length(),
asset: null,
path: kIsWeb ? null : file.path,
- bytes: kIsWeb ? () => file.readAsBytes() : null,
+ bytes: kIsWeb ? await file.readAsBytes() : null, // we can fetch it now because in Web it is already there
);
}
diff --git a/lib/theme.dart b/lib/theme.dart
index 7431c20d..22aa5356 100644
--- a/lib/theme.dart
+++ b/lib/theme.dart
@@ -37,7 +37,7 @@ ThemeData getTheme(Brightness brightness) {
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
foregroundColor: brightness == Brightness.dark ? Colors.white : null,
- padding: defaultTargetPlatform == TargetPlatform.windows ? const EdgeInsets.symmetric(horizontal: 8, vertical: 16) : const EdgeInsets.all(8),
+ padding: defaultTargetPlatform == TargetPlatform.windows ? const EdgeInsets.symmetric(horizontal: 8, vertical: 16) : const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
),
),
textButtonTheme: TextButtonThemeData(
diff --git a/lib/widget/file_thumbnail.dart b/lib/widget/file_thumbnail.dart
new file mode 100644
index 00000000..3d02d08a
--- /dev/null
+++ b/lib/widget/file_thumbnail.dart
@@ -0,0 +1,50 @@
+import 'dart:io';
+
+import 'package:flutter/material.dart';
+import 'package:localsend_app/model/cross_file.dart';
+import 'package:localsend_app/model/file_type.dart';
+import 'package:wechat_assets_picker/wechat_assets_picker.dart';
+
+class FileThumbnail extends StatelessWidget {
+ final CrossFile file;
+
+ const FileThumbnail(this.file);
+
+ @override
+ Widget build(BuildContext context) {
+ Widget? thumbnail;
+ if (file.asset != null) {
+ thumbnail = AssetEntityImage(
+ file.asset!,
+ isOriginal: false,
+ thumbnailSize: const ThumbnailSize.square(64),
+ thumbnailFormat: ThumbnailFormat.jpeg,
+ );
+ } else if (file.path != null && file.fileType == FileType.image) {
+ thumbnail = Image.file(
+ File(file.path!),
+ fit: BoxFit.fill,
+ cacheWidth: 64, // reduce memory with low cached size
+ cacheHeight: 64,
+ );
+ }
+
+ return SizedBox(
+ width: 50,
+ height: 50,
+ child: ClipRRect(
+ borderRadius: BorderRadius.circular(10),
+ child: ColoredBox(
+ color: Theme.of(context).inputDecorationTheme.fillColor!,
+ child: thumbnail == null
+ ? Icon(file.fileType.icon, size: 32)
+ : FittedBox(
+ fit: BoxFit.cover,
+ clipBehavior: Clip.hardEdge,
+ child: thumbnail,
+ ),
+ ),
+ ),
+ );
+ }
+}