From 359e2386c6dbcb314e984a3e339b1d45b62139e0 Mon Sep 17 00:00:00 2001 From: Hajar RAHMOUNI Date: Thu, 15 Feb 2024 10:03:19 +0100 Subject: [PATCH] =?UTF-8?q?s=C3=A9paration=20des=20pages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/data_page.dart | 188 +++++++++++++++++++++++++++++++++++++++++++++ lib/main.dart | 183 +------------------------------------------ 2 files changed, 189 insertions(+), 182 deletions(-) create mode 100644 lib/data_page.dart diff --git a/lib/data_page.dart b/lib/data_page.dart new file mode 100644 index 0000000..5365fbb --- /dev/null +++ b/lib/data_page.dart @@ -0,0 +1,188 @@ +import 'dart:convert'; +import 'package:flutter/material.dart'; +import 'package:http/http.dart' as http; +import 'package:open_file/open_file.dart'; +import 'package:pdf/widgets.dart' as pw; +import 'config.dart'; +import 'dart:io'; + +class DataPage extends StatefulWidget { + final String googleSheetsLink; + + const DataPage({Key? key, required this.googleSheetsLink}) : super(key: key); + + @override + _DataPageState createState() => _DataPageState(); +} + +class _DataPageState extends State { + List> _sheetData = []; + late bool _isLoading; + + @override + void initState() { + super.initState(); + _isLoading = true; + _loadSheetData(); + } + + Future _loadSheetData() async { + RegExp regex = RegExp( + r'https:\/\/docs\.google\.com\/spreadsheets\/d\/([a-zA-Z0-9_-]+)\/(edit|pubhtml).*'); + Match? match = regex.firstMatch(widget.googleSheetsLink); + + if (match != null) { + String sheetId = match.group(1)!; + String apiUrl = + 'https://sheets.googleapis.com/v4/spreadsheets/$sheetId/values/${AppConfig.sheetName}?key=${AppConfig.apiKey}'; + + try { + final response = await http.get(Uri.parse(apiUrl)); + + if (response.statusCode == 200) { + final Map data = json.decode(response.body); + List> values = []; + + if (data['values'] == null) { + setState(() { + _isLoading = false; + }); + return; + } + + for (final entry in data['values']) { + values.add(List.from(entry)); + } + + setState(() { + _sheetData = values; + }); + } else { + print( + 'Erreur lors de la récupération des données du Google Sheets. Statut : ${response.statusCode}'); + } + } catch (e) { + print( + 'Erreur lors de la récupération des données du Google Sheets : $e'); + } + } + } + + Future generatePdf(List> sheetData) async { + final pdf = pw.Document(); + + final List rows = []; + + for (final row in sheetData) { + final List rowWidgets = row.map((cell) => pw.Text('$cell')).toList(); + rows.add(pw.TableRow(children: rowWidgets)); + } + + pdf.addPage(pw.Page( + build: (pw.Context context) { + return pw.Table( + columnWidths: { + for (int i = 0; i < sheetData[0].length; i++) + i: pw.FixedColumnWidth(100.0), + }, + children: rows, + ); + }, + )); + + final file = File('google_sheets_data.pdf'); + await file.writeAsBytes(await pdf.save()); + + OpenFile.open(file.path); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('Page de données'), + ), + body: Padding( + padding: const EdgeInsets.all(16.0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + if (_sheetData.isNotEmpty) + Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text( + 'Contenu du Google Sheets :', + style: + TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold), + ), + const SizedBox(height: 10.0), + Table( + border: TableBorder.all(), + columnWidths: { + for (int i = 0; i < _sheetData[0].length; i++) + i: FixedColumnWidth(100.0), + }, + children: _sheetData.map((row) { + if (row.length < _sheetData[0].length) { + final filledRow = List.from(row) + ..addAll(List.generate( + _sheetData[0].length - row.length, (_) => '')); + + return TableRow( + children: filledRow.map((cell) => Padding( + padding: const EdgeInsets.all(8.0), + child: Text( + '$cell', + style: TextStyle(fontWeight: FontWeight.bold), + ), + )).toList(), + ); + } else { + return TableRow( + children: row.map((cell) => Padding( + padding: const EdgeInsets.all(8.0), + child: Text( + '$cell', + style: TextStyle(fontWeight: FontWeight.bold), + ), + )).toList(), + ); + } + }).toList(), + ), + const SizedBox(height: 20.0), + Row( + children: [ + ElevatedButton( + onPressed: () async { + await generatePdf(_sheetData); + }, + child: const Text('Générer PDF'), + ), + const SizedBox(width: 20.0), + ElevatedButton( + onPressed: () async { + }, + child: const Text('Lire la leocarte'), + ), + ], + ), + ], + ) + else if (_isLoading) + const Text( + 'Chargement des données..', + style: TextStyle(fontSize: 16.0), + ) + else if (_sheetData.isEmpty) + const Text( + 'Aucune donnée disponible.', + style: TextStyle(fontSize: 16.0), + ), + ], + ), + ), + ); + } +} \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index cdc9a23..0e951b2 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,11 +1,9 @@ import 'dart:convert'; -import 'dart:io'; - import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:open_file/open_file.dart'; import 'package:pdf/widgets.dart' as pw; - +import 'data_page.dart'; import 'config.dart'; void main() { @@ -109,183 +107,4 @@ class _HomePageState extends State { } } -class DataPage extends StatefulWidget { - final String googleSheetsLink; - - const DataPage({Key? key, required this.googleSheetsLink}) : super(key: key); - - @override - _DataPageState createState() => _DataPageState(); -} - -class _DataPageState extends State { - List> _sheetData = []; - late bool _isLoading; - - @override - void initState() { - super.initState(); - _isLoading = true; - _loadSheetData(); - } - - Future _loadSheetData() async { - RegExp regex = RegExp( - r'https:\/\/docs\.google\.com\/spreadsheets\/d\/([a-zA-Z0-9_-]+)\/(edit|pubhtml).*'); - Match? match = regex.firstMatch(widget.googleSheetsLink); - - if (match != null) { - String sheetId = match.group(1)!; - String apiUrl = - 'https://sheets.googleapis.com/v4/spreadsheets/$sheetId/values/${AppConfig.sheetName}?key=${AppConfig.apiKey}'; - - try { - final response = await http.get(Uri.parse(apiUrl)); - - if (response.statusCode == 200) { - final Map data = json.decode(response.body); - List> values = []; - - if (data['values'] == null) { - setState(() { - _isLoading = false; - }); - return; - } - - for (final entry in data['values']) { - values.add(List.from(entry)); - } - - setState(() { - _sheetData = values; - }); - } else { - print( - 'Erreur lors de la récupération des données du Google Sheets. Statut : ${response.statusCode}'); - } - } catch (e) { - print( - 'Erreur lors de la récupération des données du Google Sheets : $e'); - } - } - } - - Future generatePdf(List> sheetData) async { - final pdf = pw.Document(); - - final List rows = []; - - for (final row in sheetData) { - final List rowWidgets = row.map((cell) => pw.Text('$cell')).toList(); - rows.add(pw.TableRow(children: rowWidgets)); - } - pdf.addPage(pw.Page( - build: (pw.Context context) { - return pw.Table( - columnWidths: { - for (int i = 0; i < sheetData[0].length; i++) - i: pw.FixedColumnWidth(100.0), - }, - children: rows, - ); - }, - )); - - final file = File('google_sheets_data.pdf'); - await file.writeAsBytes(await pdf.save()); - - OpenFile.open(file.path); - } - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: const Text('Page de données'), - ), - body: Padding( - padding: const EdgeInsets.all(16.0), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - if (_sheetData.isNotEmpty) - Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - const Text( - 'Contenu du Google Sheets :', - style: - TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold), - ), - const SizedBox(height: 10.0), - Table( - border: TableBorder.all(), - columnWidths: { - for (int i = 0; i < _sheetData[0].length; i++) - i: FixedColumnWidth(100.0), - }, - children: _sheetData.map((row) { - if (row.length < _sheetData[0].length) { - final filledRow = List.from(row) - ..addAll(List.generate( - _sheetData[0].length - row.length, (_) => '')); - - return TableRow( - children: filledRow.map((cell) => Padding( - padding: const EdgeInsets.all(8.0), - child: Text( - '$cell', - style: TextStyle(fontWeight: FontWeight.bold), - ), - )).toList(), - ); - } else { - return TableRow( - children: row.map((cell) => Padding( - padding: const EdgeInsets.all(8.0), - child: Text( - '$cell', - style: TextStyle(fontWeight: FontWeight.bold), - ), - )).toList(), - ); - } - }).toList(), - ), - const SizedBox(height: 20.0), - Row( - children: [ - ElevatedButton( - onPressed: () async { - await generatePdf(_sheetData); - }, - child: const Text('Générer PDF'), - ), - const SizedBox(width: 20.0), - ElevatedButton( - onPressed: () async { - }, - child: const Text('Lire la leocarte'), - ), - ], - ), - ], - ) - else if (_isLoading) - const Text( - 'Chargement des données..', - style: TextStyle(fontSize: 16.0), - ) - else if (_sheetData.isEmpty) - const Text( - 'Aucune donnée disponible.', - style: TextStyle(fontSize: 16.0), - ), - ], - ), - ), - ); - } -} -- GitLab