Skip to content

An open-source cross-platform alternative to AirDrop

feat: add file picker and add alias input

Tien Do Name1ab04dda757
diff --git a/assets/i18n/strings.i18n.json b/assets/i18n/strings.i18n.json
index e9af5ce4..a076d754 100644
--- a/assets/i18n/strings.i18n.json
+++ b/assets/i18n/strings.i18n.json
@@ -15,7 +15,11 @@
}
},
"send": {
- "title": "Send"
+ "title": "Send",
+ "files": "Files",
+ "filesWithCount": "Files ({count})",
+ "selectFiles": "Select files",
+ "nearbyDevices": "Nearby devices"
},
"settings": {
"title": "Settings",
diff --git a/build.yaml b/build.yaml
index 8a281dac..c92e97ca 100644
--- a/build.yaml
+++ b/build.yaml
@@ -7,4 +7,5 @@ targets:
fallback_strategy: base_locale
input_directory: assets/i18n
input_file_pattern: .i18n.json
- output_directory: lib/gen
\ No newline at end of file
+ output_directory: lib/gen
+ string_interpolation: braces
\ No newline at end of file
diff --git a/lib/pages/tabs/send_page.dart b/lib/pages/tabs/send_page.dart
index 7317835c..1316e3e1 100644
--- a/lib/pages/tabs/send_page.dart
+++ b/lib/pages/tabs/send_page.dart
@@ -1,6 +1,11 @@
+import 'package:collection/collection.dart';
+import 'package:file_picker/file_picker.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/provider/nearby_devices_provider.dart';
+import 'package:localsend_app/provider/selected_files_provider.dart';
+import 'package:localsend_app/util/file_size_helper.dart';
import 'package:localsend_app/widget/device_widget.dart';
class SendPage extends ConsumerStatefulWidget {
@@ -11,7 +16,6 @@ class SendPage extends ConsumerStatefulWidget {
}
class _SendPageState extends ConsumerState<SendPage> {
-
@override
void initState() {
super.initState();
@@ -19,10 +23,66 @@ class _SendPageState extends ConsumerState<SendPage> {
@override
Widget build(BuildContext context) {
+ final selectedFiles = ref.watch(selectedFilesProvider);
final devices = ref.watch(nearbyDevicesProvider);
return ListView(
- padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 30),
+ padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 20),
children: [
+ Text(
+ selectedFiles.isEmpty ? t.send.files : t.send.filesWithCount(count: selectedFiles.length),
+ style: Theme.of(context).textTheme.headline6,
+ ),
+ const SizedBox(height: 10),
+ ...selectedFiles.mapIndexed((index, file) {
+ return Padding(
+ padding: const EdgeInsets.only(bottom: 10),
+ child: Card(
+ child: Padding(
+ padding: const EdgeInsets.all(10),
+ child: Row(
+ children: [
+ Expanded(
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Text(file.name),
+ Text(file.size.asReadableFileSize, style: Theme.of(context).textTheme.caption),
+ ],
+ ),
+ ),
+ IconButton(
+ onPressed: () {
+ final newList = [...selectedFiles];
+ newList.removeAt(index);
+ ref.read(selectedFilesProvider.notifier).state = newList;
+ },
+ icon: const Icon(Icons.delete),
+ ),
+ ],
+ ),
+ ),
+ ),
+ );
+ }),
+ Align(
+ alignment: Alignment.centerLeft,
+ child: Padding(
+ padding: const EdgeInsets.only(left: 5),
+ child: ElevatedButton.icon(
+ onPressed: () async {
+ final result = await FilePicker.platform.pickFiles(allowMultiple: true);
+ if (result != null) {
+ ref.read(selectedFilesProvider.notifier).state = [...selectedFiles, ...result.files];
+ }
+ },
+ icon: const Icon(Icons.add),
+ label: Text(t.send.selectFiles),
+ ),
+ ),
+ ),
+ const SizedBox(height: 20),
+ Text(t.send.nearbyDevices, style: Theme.of(context).textTheme.headline6),
+ const SizedBox(height: 10),
...devices.when(
data: (data) {
return data.map((device) {
diff --git a/lib/pages/tabs/settings_page.dart b/lib/pages/tabs/settings_page.dart
index a20a73db..88e65d2c 100644
--- a/lib/pages/tabs/settings_page.dart
+++ b/lib/pages/tabs/settings_page.dart
@@ -20,9 +20,13 @@ class _SettingsPageState extends ConsumerState<SettingsPage> {
_SettingsEntry(
label: t.settings.alias,
child: SizedBox(
- width: 200,
- child: TextField(
- onChanged: (s) {},
+ width: 150,
+ child: TextFormField(
+ initialValue: settings.alias,
+ textAlign: TextAlign.center,
+ onChanged: (s) async {
+ await ref.read(settingsProvider.notifier).setAlias(s);
+ },
),
),
),
diff --git a/lib/provider/selected_files_provider.dart b/lib/provider/selected_files_provider.dart
new file mode 100644
index 00000000..62b1976f
--- /dev/null
+++ b/lib/provider/selected_files_provider.dart
@@ -0,0 +1,4 @@
+import 'package:file_picker/file_picker.dart';
+import 'package:flutter_riverpod/flutter_riverpod.dart';
+
+final selectedFilesProvider = StateProvider<List<PlatformFile>>((ref) => []);
diff --git a/lib/theme.dart b/lib/theme.dart
index 6c3e5cd8..9e0c99bc 100644
--- a/lib/theme.dart
+++ b/lib/theme.dart
@@ -5,5 +5,10 @@ ThemeData getTheme(Brightness brightness) {
brightness: brightness,
primarySwatch: Colors.teal,
useMaterial3: true,
+ scaffoldBackgroundColor: brightness == Brightness.light ? Colors.white : Colors.grey.shade900,
+ navigationBarTheme: brightness == Brightness.dark ? NavigationBarThemeData(
+ indicatorColor: Colors.teal,
+ iconTheme: MaterialStateProperty.all(const IconThemeData(color: Colors.white)),
+ ) : null,
);
}
diff --git a/lib/util/file_size_helper.dart b/lib/util/file_size_helper.dart
new file mode 100644
index 00000000..def59805
--- /dev/null
+++ b/lib/util/file_size_helper.dart
@@ -0,0 +1,14 @@
+extension IntFileSize on int {
+ /// Converts the integer representing bytes to a readable string
+ String get asReadableFileSize {
+ if (this < 1024) {
+ return '$this B';
+ } else if (this < 1024 * 1024) {
+ return '${(this / 1024).toStringAsFixed(1)} KB';
+ } else if (this < 1024 * 1024 * 1024) {
+ return '${(this / (1024 * 1024)).toStringAsFixed(1)} MB';
+ } else {
+ return '${(this / (1024 * 1024 * 1024)).toStringAsFixed(1)} GB';
+ }
+ }
+}
diff --git a/pubspec.lock b/pubspec.lock
index a6958161..365807ba 100644
--- a/pubspec.lock
+++ b/pubspec.lock
@@ -204,6 +204,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.4"
+ file_picker:
+ dependency: "direct main"
+ description:
+ name: file_picker
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "5.2.4"
fixnum:
dependency: transitive
description:
@@ -237,6 +244,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
+ flutter_plugin_android_lifecycle:
+ dependency: transitive
+ description:
+ name: flutter_plugin_android_lifecycle
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "2.0.7"
flutter_riverpod:
dependency: "direct main"
description:
diff --git a/pubspec.yaml b/pubspec.yaml
index 228c3817..a88d5eeb 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -15,6 +15,7 @@ dependencies:
flutter:
sdk: flutter
network_info_plus: 3.0.1
+ file_picker: 5.2.4
flutter_riverpod: 2.1.1
freezed_annotation: 2.2.0
json_annotation: 4.7.0
diff --git a/windows/runner/main.cpp b/windows/runner/main.cpp
index 25ecbbb4..64b1b6b3 100644
--- a/windows/runner/main.cpp
+++ b/windows/runner/main.cpp
@@ -26,7 +26,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
FlutterWindow window(project);
Win32Window::Point origin(10, 10);
- Win32Window::Size size(1280, 720);
+ Win32Window::Size size(500, 600);
if (!window.CreateAndShow(L"LocalSend", origin, size)) {
return EXIT_FAILURE;
}