Skip to content

An open-source cross-platform alternative to AirDrop

feat: update selection UI

Tien Do Nam6f7739b30e38
diff --git a/lib/pages/tabs/send_tab.dart b/lib/pages/tabs/send_tab.dart
index 16a5ed0f..0833ff3f 100644
--- a/lib/pages/tabs/send_tab.dart
+++ b/lib/pages/tabs/send_tab.dart
@@ -1,10 +1,7 @@
-import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
-import 'package:image_picker/image_picker.dart';
import 'package:localsend_app/gen/strings.g.dart';
-import 'package:localsend_app/model/cross_file.dart';
import 'package:localsend_app/pages/selected_files_page.dart';
import 'package:localsend_app/provider/device_info_provider.dart';
import 'package:localsend_app/provider/network/nearby_devices_provider.dart';
@@ -12,9 +9,9 @@ import 'package:localsend_app/provider/network/send_provider.dart';
import 'package:localsend_app/provider/network_info_provider.dart';
import 'package:localsend_app/provider/selected_files_provider.dart';
import 'package:localsend_app/provider/settings_provider.dart';
-import 'package:localsend_app/theme.dart';
-import 'package:localsend_app/util/file_path_helper.dart';
import 'package:localsend_app/util/file_size_helper.dart';
+import 'package:localsend_app/widget/big_button.dart';
+import 'package:localsend_app/widget/dialogs/add_file_dialog.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';
@@ -53,208 +50,132 @@ class _SendTabState extends ConsumerState<SendTab> {
final selectedFiles = ref.watch(selectedFilesProvider);
final myDevice = ref.watch(deviceInfoProvider);
final nearbyDevicesState = ref.watch(nearbyDevicesProvider);
+ final addOptions = [
+ AddOption.file,
+ if (defaultTargetPlatform == TargetPlatform.iOS) ...[
+ AddOption.image,
+ AddOption.video,
+ ],
+ ];
+
return ListView(
padding: const EdgeInsets.symmetric(horizontal: 15),
children: [
const SizedBox(height: 20),
- if (selectedFiles.isEmpty)
- Padding(
- padding: const EdgeInsets.only(bottom: 10),
- child: Text(
- t.sendTab.selection.title,
- style: Theme.of(context).textTheme.subtitle1,
+ if (selectedFiles.isEmpty) ...[
+ Padding(
+ padding: const EdgeInsets.only(bottom: 10),
+ child: Text(
+ t.sendTab.selection.title,
+ style: Theme.of(context).textTheme.subtitle1,
+ ),
),
- )
- else
- ...[
- Card(
- margin: EdgeInsets.zero,
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Padding(
- padding: const EdgeInsets.only(left: 15, top: 15),
- child: Text(
- t.sendTab.selection.title,
- style: Theme.of(context).textTheme.subtitle1,
+ IntrinsicHeight(
+ child: Row(
+ crossAxisAlignment: CrossAxisAlignment.stretch,
+ children: [
+ ...List.generate(3, (index) {
+ final option = index < addOptions.length ? addOptions[index] : null;
+ return [
+ Expanded(
+ child: option == null
+ ? Container()
+ : BigButton(
+ icon: option.icon,
+ label: option.label,
+ onTap: () => option.select(ref),
+ ),
),
+ const SizedBox(width: 15),
+ ];
+ }).expand((e) => e).toList()
+ ..removeLast(),
+ ],
+ ),
+ ),
+ ] else ...[
+ Card(
+ margin: EdgeInsets.zero,
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Padding(
+ padding: const EdgeInsets.only(left: 15, top: 15),
+ child: Text(
+ t.sendTab.selection.title,
+ style: Theme.of(context).textTheme.subtitle1,
),
- const SizedBox(height: 5),
- Padding(
- padding: const EdgeInsets.only(left: 15),
- child: Text(t.sendTab.selection.files(files: selectedFiles.length)),
- ),
- Padding(
- padding: const EdgeInsets.only(left: 15),
- child: Text(t.sendTab.selection.size(size: selectedFiles.fold(0, (prev, curr) => prev + curr.size).asReadableFileSize)),
- ),
- const SizedBox(height: 10),
- Padding(
- padding: const EdgeInsets.symmetric(horizontal: 15),
+ ),
+ const SizedBox(height: 5),
+ Padding(
+ padding: const EdgeInsets.only(left: 15),
+ child: Text(t.sendTab.selection.files(files: selectedFiles.length)),
+ ),
+ Padding(
+ padding: const EdgeInsets.only(left: 15),
+ child: Text(t.sendTab.selection.size(size: selectedFiles.fold(0, (prev, curr) => prev + curr.size).asReadableFileSize)),
+ ),
+ const SizedBox(height: 10),
+ Padding(
+ padding: const EdgeInsets.symmetric(horizontal: 15),
+ child: SingleChildScrollView(
+ scrollDirection: Axis.horizontal,
child: Row(
children: [
- Expanded(
- child: Stack(
- children: [
- SingleChildScrollView(
- scrollDirection: Axis.horizontal,
- child: Row(
- children: [
- ...selectedFiles.map((file) {
- return Padding(
- padding: const EdgeInsets.only(right: 10),
- child: Opacity(
- opacity: 1,
- child: Icon(file.fileType.icon, size: 32),
- ),
- );
- }),
- ],
- ),
- ),
- Positioned.fill(
- child: IgnorePointer(
- child: DecoratedBox(
- decoration: BoxDecoration(
- gradient: LinearGradient(
- begin: FractionalOffset.centerLeft,
- end: FractionalOffset.centerRight,
- colors: [
- Colors.transparent,
- Colors.transparent,
- Theme.of(context).cardColorWithElevation.withOpacity(0),
- Theme.of(context).cardColorWithElevation,
- ],
- stops: const [0, 0, 0.7, 1],
- ),
- ),
- ),
- ),
- ),
- ],
- ),
- ),
- ElevatedButton.icon(
- style: IconButton.styleFrom(
- backgroundColor: Theme.of(context).colorScheme.primary,
- foregroundColor: Theme.of(context).colorScheme.onPrimary,
- ),
- onPressed: () {
- context.push(() => const SelectedFilesPage());
- },
- icon: const Icon(Icons.edit),
- label: Text(t.general.edit),
- ),
+ ...selectedFiles.map((file) {
+ return Padding(
+ padding: const EdgeInsets.only(right: 10),
+ child: Opacity(
+ opacity: 1,
+ child: Icon(file.fileType.icon, size: 32),
+ ),
+ );
+ }),
],
),
),
- const SizedBox(height: 15),
- ],
- ),
- ),
- const SizedBox(height: 10),
- Padding(
- padding: const EdgeInsets.only(bottom: 10),
- child: Text(
- t.general.add,
- style: Theme.of(context).textTheme.subtitle1,
- ),
- ),
- ],
- IntrinsicHeight(
- child: Row(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: [
- Expanded(
- child: _BigButton(
- icon: Icons.description,
- label: t.sendTab.picker.file,
- onTap: () async {
- final result = await FilePicker.platform.pickFiles(allowMultiple: true);
- if (result != null) {
- ref.read(selectedFilesProvider.notifier).state = [
- ...selectedFiles,
- ...result.files.map((f) {
- return CrossFile(
- name: f.name,
- fileType: f.name.guessFileType(),
- size: f.size,
- path: kIsWeb ? null : f.path,
- bytes: kIsWeb ? f.bytes : null,
- );
- }),
- ];
- }
- },
),
- ),
- const SizedBox(width: 15),
- if (defaultTargetPlatform == TargetPlatform.iOS)
- Expanded(
- child: _BigButton(
- icon: Icons.image,
- label: t.sendTab.picker.image,
- onTap: () async {
- final images = await ImagePicker().pickMultiImage();
- ref.read(selectedFilesProvider.notifier).state = [
- ...selectedFiles,
- ...await Future.wait(images.map((file) async {
- return CrossFile(
- name: file.name,
- fileType: file.name.guessFileType(),
- size: await file.length(),
- path: kIsWeb ? null : file.path,
- bytes: kIsWeb ? await file.readAsBytes() : null,
- );
- })
- ),
- ];
- },
- ),
- )
- else
- Expanded(child: Container()),
- const SizedBox(width: 15),
- if (defaultTargetPlatform == TargetPlatform.iOS)
- Expanded(
- child: _BigButton(
- icon: Icons.movie,
- label: t.sendTab.picker.video,
- onTap: () async {
- final file = await ImagePicker().pickVideo(source: ImageSource.gallery);
- if (file != null) {
- ref.read(selectedFilesProvider.notifier).state = [
- ...selectedFiles,
- CrossFile(
- name: file.name,
- fileType: file.name.guessFileType(),
- size: await file.length(),
- path: kIsWeb ? null : file.path,
- bytes: kIsWeb ? await file.readAsBytes() : null,
- ),
- ];
- }
- },
- ),
- )
- else
- Expanded(child: Container()),
- // const SizedBox(width: 15),
- // Expanded(
- // child: _BigButton(
- // icon: Icons.subject,
- // label: t.send.picker.text,
- // onTap: () async {
- // final result = await FilePicker.platform.pickFiles(allowMultiple: true);
- // if (result != null) {
- // ref.read(selectedFilesProvider.notifier).state = [...selectedFiles, ...result.files];
- // }
- // },
- // ),
- // ),
- ],
+ const SizedBox(height: 10),
+ Row(
+ mainAxisAlignment: MainAxisAlignment.end,
+ children: [
+ TextButton.icon(
+ style: IconButton.styleFrom(
+ foregroundColor: Theme.of(context).colorScheme.onSurface,
+ ),
+ onPressed: () {
+ context.push(() => const SelectedFilesPage());
+ },
+ icon: const Icon(Icons.edit),
+ label: Text(t.general.edit),
+ ),
+ const SizedBox(width: 15),
+ ElevatedButton.icon(
+ style: IconButton.styleFrom(
+ backgroundColor: Theme.of(context).colorScheme.primary,
+ foregroundColor: Theme.of(context).colorScheme.onPrimary,
+ ),
+ onPressed: () {
+ if (addOptions.length == 1) {
+ addOptions.first.select(ref); // open directly
+ return;
+ }
+ context.pushBottomSheet(() => AddFileDialog(
+ parentRef: ref,
+ options: addOptions,
+ ));
+ },
+ icon: const Icon(Icons.add),
+ label: Text(t.general.add),
+ ),
+ const SizedBox(width: 15),
+ ],
+ ),
+ const SizedBox(height: 15),
+ ],
+ ),
),
- ),
+ ],
const SizedBox(height: 20),
Row(
children: [
@@ -306,40 +227,3 @@ class _SendTabState extends ConsumerState<SendTab> {
);
}
}
-
-class _BigButton extends StatelessWidget {
- final IconData icon;
- final String label;
- final VoidCallback onTap;
-
- const _BigButton({
- required this.icon,
- required this.label,
- required this.onTap,
- });
-
- @override
- Widget build(BuildContext context) {
- return ElevatedButton(
- style: ElevatedButton.styleFrom(
- padding: EdgeInsets.symmetric(vertical: defaultTargetPlatform == TargetPlatform.windows ? 20 : 10),
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(10),
- ),
- ),
- onPressed: onTap,
- child: Column(
- mainAxisAlignment: MainAxisAlignment.start,
- children: [
- Icon(icon),
- const SizedBox(height: 5),
- Expanded(
- child: FittedBox(
- child: Text(label, maxLines: 1),
- ),
- ),
- ],
- ),
- );
- }
-}
diff --git a/lib/widget/big_button.dart b/lib/widget/big_button.dart
new file mode 100644
index 00000000..8327e89c
--- /dev/null
+++ b/lib/widget/big_button.dart
@@ -0,0 +1,39 @@
+import 'package:flutter/foundation.dart';
+import 'package:flutter/material.dart';
+
+class BigButton extends StatelessWidget {
+ final IconData icon;
+ final String label;
+ final VoidCallback onTap;
+
+ const BigButton({
+ required this.icon,
+ required this.label,
+ required this.onTap,
+ });
+
+ @override
+ Widget build(BuildContext context) {
+ return ElevatedButton(
+ style: ElevatedButton.styleFrom(
+ padding: EdgeInsets.symmetric(vertical: defaultTargetPlatform == TargetPlatform.windows ? 20 : 10),
+ shape: RoundedRectangleBorder(
+ borderRadius: BorderRadius.circular(10),
+ ),
+ ),
+ onPressed: onTap,
+ child: Column(
+ mainAxisAlignment: MainAxisAlignment.start,
+ children: [
+ Icon(icon),
+ const SizedBox(height: 5),
+ Expanded(
+ child: FittedBox(
+ child: Text(label, maxLines: 1),
+ ),
+ ),
+ ],
+ ),
+ );
+ }
+}
diff --git a/lib/widget/dialogs/add_file_dialog.dart b/lib/widget/dialogs/add_file_dialog.dart
new file mode 100644
index 00000000..15e54bd5
--- /dev/null
+++ b/lib/widget/dialogs/add_file_dialog.dart
@@ -0,0 +1,134 @@
+import 'package:file_picker/file_picker.dart';
+import 'package:flutter/foundation.dart';
+import 'package:flutter/material.dart';
+import 'package:flutter_riverpod/flutter_riverpod.dart';
+import 'package:image_picker/image_picker.dart';
+import 'package:localsend_app/gen/strings.g.dart';
+import 'package:localsend_app/model/cross_file.dart';
+import 'package:localsend_app/provider/selected_files_provider.dart';
+import 'package:localsend_app/util/file_path_helper.dart';
+import 'package:localsend_app/widget/big_button.dart';
+import 'package:localsend_app/widget/dialogs/custom_bottom_sheet.dart';
+import 'package:routerino/routerino.dart';
+
+enum AddOption {
+ file(Icons.description),
+ image(Icons.image),
+ video(Icons.movie);
+
+ const AddOption(this.icon);
+
+ final IconData icon;
+
+ String get label {
+ switch (this) {
+ case AddOption.file:
+ return t.sendTab.picker.file;
+ case AddOption.image:
+ return t.sendTab.picker.image;
+ case AddOption.video:
+ return t.sendTab.picker.video;
+ }
+ }
+
+ Future<void> select(WidgetRef ref) async {
+ switch (this) {
+ case AddOption.file:
+ final selectedFiles = ref.read(selectedFilesProvider);
+ final result = await FilePicker.platform.pickFiles(allowMultiple: true);
+ if (result != null) {
+ ref.read(selectedFilesProvider.notifier).state = [
+ ...selectedFiles,
+ ...result.files.map((f) {
+ return CrossFile(
+ name: f.name,
+ fileType: f.name.guessFileType(),
+ size: f.size,
+ path: kIsWeb ? null : f.path,
+ bytes: kIsWeb ? f.bytes : null,
+ );
+ }),
+ ];
+ }
+ break;
+ case AddOption.image:
+ final selectedFiles = ref.read(selectedFilesProvider);
+ final images = await ImagePicker().pickMultiImage();
+ ref.read(selectedFilesProvider.notifier).state = [
+ ...selectedFiles,
+ ...await Future.wait(images.map((file) async {
+ return CrossFile(
+ name: file.name,
+ fileType: file.name.guessFileType(),
+ size: await file.length(),
+ path: kIsWeb ? null : file.path,
+ bytes: kIsWeb ? await file.readAsBytes() : null,
+ );
+ })),
+ ];
+ break;
+ case AddOption.video:
+ final selectedFiles = ref.read(selectedFilesProvider);
+ final file = await ImagePicker().pickVideo(source: ImageSource.gallery);
+ if (file != null) {
+ ref.read(selectedFilesProvider.notifier).state = [
+ ...selectedFiles,
+ CrossFile(
+ name: file.name,
+ fileType: file.name.guessFileType(),
+ size: await file.length(),
+ path: kIsWeb ? null : file.path,
+ bytes: kIsWeb ? await file.readAsBytes() : null,
+ ),
+ ];
+ }
+ break;
+ }
+ }
+}
+
+class AddFileDialog extends StatelessWidget {
+ final WidgetRef parentRef;
+ final List<AddOption> options;
+
+ const AddFileDialog({required this.parentRef, required this.options});
+
+ @override
+ Widget build(BuildContext context) {
+ return CustomBottomSheet(
+ title: t.general.add,
+ description: null,
+ child: Column(
+ children: [
+ const SizedBox(height: 20),
+ IntrinsicHeight(
+ child: Row(
+ crossAxisAlignment: CrossAxisAlignment.stretch,
+ children: [
+ ...List.generate(3, (index) {
+ final option = index < options.length ? options[index] : null;
+ return [
+ Expanded(
+ child: option == null
+ ? Container()
+ : BigButton(
+ icon: option.icon,
+ label: option.label,
+ onTap: () {
+ context.popUntilRoot();
+ option.select(parentRef);
+ },
+ ),
+ ),
+ const SizedBox(width: 15),
+ ];
+ }).expand((e) => e).toList()
+ ..removeLast(),
+ ],
+ ),
+ ),
+ ],
+ ),
+ );
+ }
+}
diff --git a/lib/widget/list_tile/custom_list_tile.dart b/lib/widget/list_tile/custom_list_tile.dart
index 26439b15..4c0ffe25 100644
--- a/lib/widget/list_tile/custom_list_tile.dart
+++ b/lib/widget/list_tile/custom_list_tile.dart
@@ -20,6 +20,7 @@ class CustomListTile extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Card(
+ margin: EdgeInsets.zero,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),