From 940f6e33ceaf559fadb02141bc32785056efe0bb Mon Sep 17 00:00:00 2001 From: marcoabat Date: Sun, 6 Aug 2023 15:50:21 +0200 Subject: [PATCH] displayed items on detail view --- lib/assets/example_data.dart | 1 - lib/models/checklist.dart | 8 +--- lib/pages/detail_checklist_page.dart | 55 ++++++++++++++++++++++------ lib/services/dbhelper.dart | 45 +++++++++++++++++------ 4 files changed, 80 insertions(+), 29 deletions(-) diff --git a/lib/assets/example_data.dart b/lib/assets/example_data.dart index 2c5d154..3cea904 100644 --- a/lib/assets/example_data.dart +++ b/lib/assets/example_data.dart @@ -7,6 +7,5 @@ List checklists = [ 'Test1', 'tiersntiersntsrien', DateTime.now(), - List.empty(), ) ]; diff --git a/lib/models/checklist.dart b/lib/models/checklist.dart index 2d33654..6e41e64 100644 --- a/lib/models/checklist.dart +++ b/lib/models/checklist.dart @@ -1,14 +1,10 @@ -import 'listitem.dart'; - class Checklist { final int id; final String ownerId; String title; String description; final DateTime createdTime; - List items; - Checklist(this.id, this.ownerId, this.title, this.description, - this.createdTime, List? items) - : items = items ?? []; + Checklist( + this.id, this.ownerId, this.title, this.description, this.createdTime); } diff --git a/lib/pages/detail_checklist_page.dart b/lib/pages/detail_checklist_page.dart index 96ed754..268b353 100644 --- a/lib/pages/detail_checklist_page.dart +++ b/lib/pages/detail_checklist_page.dart @@ -1,3 +1,5 @@ +import 'dart:js_interop'; + import 'package:briessenchecker/models/checklist.dart'; import 'package:briessenchecker/models/listitem.dart'; import 'package:briessenchecker/services/checklist_provider.dart'; @@ -15,10 +17,12 @@ class DetailChecklistPage extends StatefulWidget { } class _DetailChecklistPageState extends State { - late Future _checklistFuture; + late Future> _checklistFutures; late final ChecklistProvider _checklistProvider; - late Checklist _currentChecklist; + Checklist? _checklist; + List _items = []; int? _selectedItemId; + String? pageTitle; @override void dispose() { @@ -30,19 +34,31 @@ class _DetailChecklistPageState extends State { void initState() { super.initState(); _checklistProvider = Provider.of(context, listen: false); - _checklistFuture = - DbHelper.getChecklistById(_checklistProvider.selectedChecklistId!); + _checklistFutures = initFutures(_checklistProvider.selectedChecklistId!); } Widget _futureBuilder( - BuildContext context, AsyncSnapshot snapshot) { + BuildContext context, AsyncSnapshot> snapshot) { if (snapshot.hasData) { - _currentChecklist = snapshot.data!; - String title = _currentChecklist.title; + _checklist = snapshot.data!.first as Checklist; + _items = snapshot.data!.last as List; + String title = _checklist!.title; + if (pageTitle == null) { + WidgetsBinding.instance + .addPostFrameCallback((_) => setState(() => pageTitle = title)); + } return Column( children: [ - Text(title == '' ? 'Unnamed ${_currentChecklist.id}' : title), - Text(_currentChecklist.description), + Text(title == '' ? 'Unnamed ${_checklist!.id}' : title), + Text(_checklist!.description), + SizedBox( + width: 500, + height: 500, + child: ListView.builder( + itemCount: _items.length, + itemBuilder: _itemList, + ), + ), ], ); } else if (snapshot.hasError) { @@ -60,7 +76,7 @@ class _DetailChecklistPageState extends State { TextEditingController titleCon = TextEditingController(); TextEditingController descCon = TextEditingController(); if (_selectedItemId != null) { - final item = _currentChecklist.items.elementAt(_selectedItemId!); + final item = _items.elementAt(_selectedItemId!); titleCon.text = item.title; descCon.text = item.description; } @@ -103,8 +119,11 @@ class _DetailChecklistPageState extends State { @override Widget build(BuildContext context) { return Scaffold( + appBar: AppBar( + title: Text(pageTitle ?? ''), + ), body: FutureBuilder( - future: _checklistFuture, + future: _checklistFutures, builder: _futureBuilder, ), floatingActionButton: FloatingActionButton( @@ -113,4 +132,18 @@ class _DetailChecklistPageState extends State { ), ); } + + Widget? _itemList(BuildContext context, int index) { + return ListTile( + title: Text(_items.elementAt(index).title), + subtitle: Text(_items.elementAt(index).description), + ); + } + + Future> initFutures(int checklistId) async { + return Future.wait([ + DbHelper.getChecklistById(checklistId), + DbHelper.getItemsByChecklistId(checklistId), + ]); + } } diff --git a/lib/services/dbhelper.dart b/lib/services/dbhelper.dart index eda3d96..7e5e8ef 100644 --- a/lib/services/dbhelper.dart +++ b/lib/services/dbhelper.dart @@ -1,14 +1,14 @@ import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; -import '../assets/example_data.dart' as ed; import '../models/checklist.dart'; import '../models/listitem.dart'; class DbHelper { - static late final SupabaseClient _client; + static const checkedItemsTableName = 'checkedItems'; static const checklistsTableName = 'checklists'; static const itemsTableName = 'items'; - static const checkedItemsTableName = 'checkedItems'; + + static late final SupabaseClient _client; static Future init() async { await Supabase.initialize( @@ -42,7 +42,6 @@ class DbHelper { element['title'], element['description'], DateTime.parse(element['createdTime']), - [], ); checklists.add(cl); } @@ -72,19 +71,43 @@ class DbHelper { return res.last['id'] as int; } + static Future> getItemsByChecklistId(int checklist_id) async { + final List items = []; + + final itemRes = await _client + .from(itemsTableName) + .select>>() + .eq('checklist_id', checklist_id); + + for (final item in itemRes) { + items.add( + Item( + item['id'], + item['owner_id'], + item['title'], + item['description'], + DateTime.parse(item['createdTime']), + null, + ), + ); + } + + return items; + } + static Future getChecklistById(int id) async { - final res = await _client + final checklistRes = await _client .from(checklistsTableName) .select>() .eq('id', id) .single(); + return Checklist( - res['id'], - res['ownerId'], - res['title'], - res['description'], - DateTime.parse(res['createdTime']), - [], + checklistRes['id'], + checklistRes['ownerId'], + checklistRes['title'], + checklistRes['description'], + DateTime.parse(checklistRes['createdTime']), ); }