From 6e20d28c85486a36d1e333eea361b73ce5264c04 Mon Sep 17 00:00:00 2001 From: Paolo Pietrelli Date: Wed, 2 Oct 2024 01:38:13 +0200 Subject: [PATCH] web and mobile working --- lib/main.dart | 2 +- lib/view/insert_file.dart | 45 +++++++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 63b21a7..cfcbc62 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -9,7 +9,6 @@ Future main() async { supportedLocales: [ 'en', 'it', - 'zh', ], ); @@ -36,6 +35,7 @@ class MyApp extends StatelessWidget { localizationsDelegates: [ GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, localizationDelegate ], supportedLocales: localizationDelegate.supportedLocales, diff --git a/lib/view/insert_file.dart b/lib/view/insert_file.dart index df05b1c..a0dadb5 100644 --- a/lib/view/insert_file.dart +++ b/lib/view/insert_file.dart @@ -1,9 +1,11 @@ -import 'dart:io'; +import 'dart:io' show File; // Import for mobile file handling +import 'dart:typed_data'; import 'package:excel/excel.dart'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; import 'package:exel_category/model/excel_element.dart'; import 'package:flutter_translate/flutter_translate.dart'; +import 'package:flutter/foundation.dart' show kIsWeb; class InsertFile extends StatefulWidget { final Function(Map>, List) onFileLoaded; @@ -50,11 +52,19 @@ class _InsertFileState extends State { onPressed: () async { FilePickerResult? result = await FilePicker.platform.pickFiles(); - if (result != null) { + if (result != null && result.files.isNotEmpty) { setState(() { fileName = result.files.single.name; }); - await loadExcelFile(result.files.single.path!); + + // Check if we are on the web or mobile + if (kIsWeb) { + // For web, use bytes + await loadExcelFileFromBytes(result.files.single.bytes!); + } else { + // For mobile, use path + await loadExcelFileFromPath(result.files.single.path!); + } } }, child: Text(translate('Select File')), @@ -69,31 +79,38 @@ class _InsertFileState extends State { ); } - Future loadExcelFile(String path) async { + Future loadExcelFileFromBytes(Uint8List bytes) async { + // Decode the Excel file from bytes + var file = Excel.decodeBytes(bytes); + await processExcelFile(file); + } + + Future loadExcelFileFromPath(String path) async { + // Decode the Excel file from the file path var file = Excel.decodeBytes(await File(path).readAsBytes()); + await processExcelFile(file); + } + + Future processExcelFile(Excel file) async { List elements = []; - Map> columnItems = {}; // Per salvare gli oggetti per le colonne + Map> columnItems = {}; // To save column items for (var table in file.tables.keys) { - // Assumiamo che la prima riga contenga i titoli delle colonne var headerRow = file.tables[table]?.rows[0]; - // Salva i titoli delle colonne in una lista + // Save column titles in a list List columnTitles = headerRow?.map((cell) => cell?.value?.toString() ?? '').toList() ?? []; - // Inizia a iterare sulle righe dopo la prima + // Start iterating over rows after the header for (var row in file.tables[table]!.rows.skip(1)) { - // Salta la prima riga if (row.isNotEmpty) { Map rowDetails = {}; - // Popola il Map utilizzando i titoli delle colonne come chiavi + // Populate the Map using column titles as keys for (var i = 0; i < row.length; i++) { if (i < columnTitles.length) { - rowDetails[columnTitles[i]] = - row[i]?.value; // Associa il valore della cella alla chiave - // Aggiungi il valore alla mappa di colonne + rowDetails[columnTitles[i]] = row[i]?.value; // Associate cell value with key columnItems.putIfAbsent(columnTitles[i], () => []).add(row[i]?.value.toString() ?? ''); } } @@ -103,7 +120,7 @@ class _InsertFileState extends State { } } - // Passa i dati al widget genitore + // Pass the data to the parent widget widget.onFileLoaded(columnItems, elements); } }