butto to import file
This commit is contained in:
@@ -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<DeckImportScreen> {
|
||||
showTopSnackBar(context, message: 'JSON copied to clipboard');
|
||||
}
|
||||
|
||||
Future<void> _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<DeckImportScreen> {
|
||||
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),
|
||||
|
||||
Reference in New Issue
Block a user