From 6b8cf10e3c7679f1ab56a0e8783a29ecaaec8632 Mon Sep 17 00:00:00 2001 From: gitea Date: Sat, 31 Jan 2026 14:55:20 +0100 Subject: [PATCH] butto to import file --- lib/screens/deck_import_screen.dart | 31 ++++++++++++++++++++++++++++ pubspec.lock | 32 +++++++++++++++++++++++++++++ pubspec.yaml | 1 + 3 files changed, 64 insertions(+) diff --git a/lib/screens/deck_import_screen.dart b/lib/screens/deck_import_screen.dart index ffcf8bc..3492213 100644 --- a/lib/screens/deck_import_screen.dart +++ b/lib/screens/deck_import_screen.dart @@ -1,4 +1,6 @@ import 'dart:convert'; + +import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:practice_engine/practice_engine.dart'; @@ -271,6 +273,30 @@ class _DeckImportScreenState extends State { showTopSnackBar(context, message: 'JSON copied to clipboard'); } + Future _pickJsonFile() async { + final result = await FilePicker.platform.pickFiles( + type: FileType.custom, + allowedExtensions: ['json'], + withData: true, + ); + if (result == null || result.files.isEmpty || !mounted) return; + final file = result.files.single; + if (file.bytes == null) { + if (mounted) { + showTopSnackBar(context, message: 'Could not read file content'); + } + return; + } + final text = utf8.decode(file.bytes!); + setState(() { + _errorMessage = null; + _jsonController.text = text; + }); + if (mounted) { + showTopSnackBar(context, message: 'File loaded'); + } + } + @override Widget build(BuildContext context) { return Scaffold( @@ -393,6 +419,11 @@ class _DeckImportScreenState extends State { Row( mainAxisSize: MainAxisSize.min, children: [ + TextButton.icon( + onPressed: _pickJsonFile, + icon: const Icon(Icons.folder_open), + label: const Text('Select file'), + ), TextButton.icon( onPressed: _loadSampleDeck, icon: const Icon(Icons.description), diff --git a/pubspec.lock b/pubspec.lock index 351d550..01a36c2 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -41,6 +41,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.19.1" + cross_file: + dependency: transitive + description: + name: cross_file + sha256: "28bb3ae56f117b5aec029d702a90f57d285cd975c3c5c281eaca38dbc47c5937" + url: "https://pub.dev" + source: hosted + version: "0.3.5+2" cupertino_icons: dependency: "direct main" description: @@ -73,6 +81,14 @@ packages: url: "https://pub.dev" source: hosted version: "7.0.1" + file_picker: + dependency: "direct main" + description: + name: file_picker + sha256: ab13ae8ef5580a411c458d6207b6774a6c237d77ac37011b13994879f68a8810 + url: "https://pub.dev" + source: hosted + version: "8.3.7" flutter: dependency: "direct main" description: flutter @@ -86,6 +102,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.0.2" + flutter_plugin_android_lifecycle: + dependency: transitive + description: + name: flutter_plugin_android_lifecycle + sha256: ee8068e0e1cd16c4a82714119918efdeed33b3ba7772c54b5d094ab53f9b7fd1 + url: "https://pub.dev" + source: hosted + version: "2.0.33" flutter_test: dependency: "direct dev" description: flutter @@ -340,6 +364,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.1.1" + win32: + dependency: transitive + description: + name: win32 + sha256: d7cb55e04cd34096cd3a79b3330245f54cb96a370a1c27adb3c84b917de8b08e + url: "https://pub.dev" + source: hosted + version: "5.15.0" xdg_directories: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index c90990b..c9ca489 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -13,6 +13,7 @@ dependencies: path: packages/practice_engine cupertino_icons: ^1.0.6 shared_preferences: ^2.2.2 + file_picker: ^8.1.6 dev_dependencies: flutter_test: