From dafb4bde5377ecd6b53ad34667f0fa5279cedc84 Mon Sep 17 00:00:00 2001 From: Stuart Russell Date: Fri, 15 Oct 2021 02:26:53 +0100 Subject: [PATCH 1/2] multiple SELECT queries shows all results --- lib/pages/home_page.dart | 38 ++++++++++---------- lib/widgets/query_result.dart | 68 +++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 20 deletions(-) create mode 100644 lib/widgets/query_result.dart diff --git a/lib/pages/home_page.dart b/lib/pages/home_page.dart index b06cd1e..c7e1dd3 100644 --- a/lib/pages/home_page.dart +++ b/lib/pages/home_page.dart @@ -1,7 +1,9 @@ +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:sqflite_common/sqlite_api.dart' as sqflite; import 'package:sqflite_web/sqflite_web.dart'; +import '../widgets/query_result.dart'; import '../widgets/radio_button.dart'; class HomePage extends StatefulWidget { @@ -45,6 +47,8 @@ class _HomePageState extends State { @override Widget build(BuildContext context) { + final Color canvasColor = Theme.of(context).canvasColor; + final Color indicatorColor = Theme.of(context).indicatorColor; return Scaffold( appBar: AppBar( centerTitle: true, @@ -164,27 +168,21 @@ class _HomePageState extends State { break; case 'Query': - final List> queryOutput = - await db.rawQuery(_commandController.text); - - if (queryOutput.isEmpty) { - _output = const Text('No output!'); - } else { - _output = DataTable( - columns: queryOutput.first.keys - .map((e) => DataColumn( - label: Text(e), - )) - .toList(), - rows: queryOutput - .map((e) => DataRow( - cells: queryOutput.first.keys - .map((a) => DataCell( - Text(e[a]?.toString() ?? 'null'))) - .toList())) - .toList(), - ); + final List queries = _commandController.text + .replaceAll('\n', '') + .split(';') + .where((s) => s.isNotEmpty) + .toList(); + + final List>> results = []; + for (final query in queries) { + final List> queryOutput = + await db.rawQuery(query); + results.addAll([queryOutput]); } + + _output = buildQueryResult( + results, canvasColor, indicatorColor); setState(() {}); diff --git a/lib/widgets/query_result.dart b/lib/widgets/query_result.dart new file mode 100644 index 0000000..a26723b --- /dev/null +++ b/lib/widgets/query_result.dart @@ -0,0 +1,68 @@ +import 'package:flutter/material.dart'; + +Widget buildQueryResult(List>> results, + Color canvasColor, Color indicatorColor) { + if (results.length < 2) { + final queryOutput = results.first; + return DataTable( + columns: queryOutput.first.keys + .map((e) => DataColumn( + label: Text(e), + )) + .toList(), + rows: queryOutput + .map((e) => DataRow( + cells: queryOutput.first.keys + .map((a) => DataCell(Text(e[a]?.toString() ?? 'null'))) + .toList())) + .toList(), + ); + } + return Center( + child: Container( + height: 300, + child: DefaultTabController( + length: results.length, + child: Scaffold( + appBar: AppBar( + title: const Text(''), + toolbarHeight: 50, + elevation: 0, + automaticallyImplyLeading: false, + backgroundColor: canvasColor, + bottom: TabBar( + indicatorColor: indicatorColor, + labelColor: Colors.black, + tabs: List.generate( + results.length, + (int index) { + return Tab(text: 'Query ${index+1}'); + }, + )), + ), + body: TabBarView( + children: List.generate( + results.length, + (int index) { + final queryOutput = results.elementAt(index); + return DataTable( + columns: queryOutput.first.keys + .map((e) => DataColumn( + label: Text(e), + )) + .toList(), + rows: queryOutput + .map((e) => DataRow( + cells: queryOutput.first.keys + .map((a) => DataCell(Text(e[a]?.toString() ?? 'null'))) + .toList())) + .toList(), + ); + }, + ), + ), + ), + ), + ), + ); +} From 953b40c2da51a790533306c98fcb4d040a649807 Mon Sep 17 00:00:00 2001 From: Stuart Russell Date: Fri, 15 Oct 2021 11:45:20 +0100 Subject: [PATCH 2/2] tab controller now adjusts size to suit the larges child table --- lib/widgets/query_result.dart | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/widgets/query_result.dart b/lib/widgets/query_result.dart index a26723b..a6a9184 100644 --- a/lib/widgets/query_result.dart +++ b/lib/widgets/query_result.dart @@ -1,3 +1,5 @@ +import 'dart:math'; + import 'package:flutter/material.dart'; Widget buildQueryResult(List>> results, @@ -18,9 +20,11 @@ Widget buildQueryResult(List>> results, .toList(), ); } + final int tableLength = results.map((e) => e.length).reduce(max); return Center( + // ignore: sized_box_for_whitespace child: Container( - height: 300, + height: (tableLength + 2) * 50, child: DefaultTabController( length: results.length, child: Scaffold( @@ -36,7 +40,7 @@ Widget buildQueryResult(List>> results, tabs: List.generate( results.length, (int index) { - return Tab(text: 'Query ${index+1}'); + return Tab(text: 'Query ${index + 1}'); }, )), ), @@ -54,7 +58,8 @@ Widget buildQueryResult(List>> results, rows: queryOutput .map((e) => DataRow( cells: queryOutput.first.keys - .map((a) => DataCell(Text(e[a]?.toString() ?? 'null'))) + .map((a) => + DataCell(Text(e[a]?.toString() ?? 'null'))) .toList())) .toList(), );