displayed items on detail view
This commit is contained in:
@@ -7,6 +7,5 @@ List<Checklist> checklists = [
|
|||||||
'Test1',
|
'Test1',
|
||||||
'tiersntiersntsrien',
|
'tiersntiersntsrien',
|
||||||
DateTime.now(),
|
DateTime.now(),
|
||||||
List.empty(),
|
|
||||||
)
|
)
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -1,14 +1,10 @@
|
|||||||
import 'listitem.dart';
|
|
||||||
|
|
||||||
class Checklist {
|
class Checklist {
|
||||||
final int id;
|
final int id;
|
||||||
final String ownerId;
|
final String ownerId;
|
||||||
String title;
|
String title;
|
||||||
String description;
|
String description;
|
||||||
final DateTime createdTime;
|
final DateTime createdTime;
|
||||||
List<Item> items;
|
|
||||||
|
|
||||||
Checklist(this.id, this.ownerId, this.title, this.description,
|
Checklist(
|
||||||
this.createdTime, List<Item>? items)
|
this.id, this.ownerId, this.title, this.description, this.createdTime);
|
||||||
: items = items ?? [];
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import 'dart:js_interop';
|
||||||
|
|
||||||
import 'package:briessenchecker/models/checklist.dart';
|
import 'package:briessenchecker/models/checklist.dart';
|
||||||
import 'package:briessenchecker/models/listitem.dart';
|
import 'package:briessenchecker/models/listitem.dart';
|
||||||
import 'package:briessenchecker/services/checklist_provider.dart';
|
import 'package:briessenchecker/services/checklist_provider.dart';
|
||||||
@@ -15,10 +17,12 @@ class DetailChecklistPage extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _DetailChecklistPageState extends State<DetailChecklistPage> {
|
class _DetailChecklistPageState extends State<DetailChecklistPage> {
|
||||||
late Future<Checklist> _checklistFuture;
|
late Future<List<Object>> _checklistFutures;
|
||||||
late final ChecklistProvider _checklistProvider;
|
late final ChecklistProvider _checklistProvider;
|
||||||
late Checklist _currentChecklist;
|
Checklist? _checklist;
|
||||||
|
List<Item> _items = [];
|
||||||
int? _selectedItemId;
|
int? _selectedItemId;
|
||||||
|
String? pageTitle;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
@@ -30,19 +34,31 @@ class _DetailChecklistPageState extends State<DetailChecklistPage> {
|
|||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_checklistProvider = Provider.of<ChecklistProvider>(context, listen: false);
|
_checklistProvider = Provider.of<ChecklistProvider>(context, listen: false);
|
||||||
_checklistFuture =
|
_checklistFutures = initFutures(_checklistProvider.selectedChecklistId!);
|
||||||
DbHelper.getChecklistById(_checklistProvider.selectedChecklistId!);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _futureBuilder(
|
Widget _futureBuilder(
|
||||||
BuildContext context, AsyncSnapshot<Checklist> snapshot) {
|
BuildContext context, AsyncSnapshot<List<Object>> snapshot) {
|
||||||
if (snapshot.hasData) {
|
if (snapshot.hasData) {
|
||||||
_currentChecklist = snapshot.data!;
|
_checklist = snapshot.data!.first as Checklist;
|
||||||
String title = _currentChecklist.title;
|
_items = snapshot.data!.last as List<Item>;
|
||||||
|
String title = _checklist!.title;
|
||||||
|
if (pageTitle == null) {
|
||||||
|
WidgetsBinding.instance
|
||||||
|
.addPostFrameCallback((_) => setState(() => pageTitle = title));
|
||||||
|
}
|
||||||
return Column(
|
return Column(
|
||||||
children: [
|
children: [
|
||||||
Text(title == '' ? 'Unnamed ${_currentChecklist.id}' : title),
|
Text(title == '' ? 'Unnamed ${_checklist!.id}' : title),
|
||||||
Text(_currentChecklist.description),
|
Text(_checklist!.description),
|
||||||
|
SizedBox(
|
||||||
|
width: 500,
|
||||||
|
height: 500,
|
||||||
|
child: ListView.builder(
|
||||||
|
itemCount: _items.length,
|
||||||
|
itemBuilder: _itemList,
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
} else if (snapshot.hasError) {
|
} else if (snapshot.hasError) {
|
||||||
@@ -60,7 +76,7 @@ class _DetailChecklistPageState extends State<DetailChecklistPage> {
|
|||||||
TextEditingController titleCon = TextEditingController();
|
TextEditingController titleCon = TextEditingController();
|
||||||
TextEditingController descCon = TextEditingController();
|
TextEditingController descCon = TextEditingController();
|
||||||
if (_selectedItemId != null) {
|
if (_selectedItemId != null) {
|
||||||
final item = _currentChecklist.items.elementAt(_selectedItemId!);
|
final item = _items.elementAt(_selectedItemId!);
|
||||||
titleCon.text = item.title;
|
titleCon.text = item.title;
|
||||||
descCon.text = item.description;
|
descCon.text = item.description;
|
||||||
}
|
}
|
||||||
@@ -103,8 +119,11 @@ class _DetailChecklistPageState extends State<DetailChecklistPage> {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
title: Text(pageTitle ?? ''),
|
||||||
|
),
|
||||||
body: FutureBuilder(
|
body: FutureBuilder(
|
||||||
future: _checklistFuture,
|
future: _checklistFutures,
|
||||||
builder: _futureBuilder,
|
builder: _futureBuilder,
|
||||||
),
|
),
|
||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: FloatingActionButton(
|
||||||
@@ -113,4 +132,18 @@ class _DetailChecklistPageState extends State<DetailChecklistPage> {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget? _itemList(BuildContext context, int index) {
|
||||||
|
return ListTile(
|
||||||
|
title: Text(_items.elementAt(index).title),
|
||||||
|
subtitle: Text(_items.elementAt(index).description),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<Object>> initFutures(int checklistId) async {
|
||||||
|
return Future.wait([
|
||||||
|
DbHelper.getChecklistById(checklistId),
|
||||||
|
DbHelper.getItemsByChecklistId(checklistId),
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||||
import '../assets/example_data.dart' as ed;
|
|
||||||
import '../models/checklist.dart';
|
import '../models/checklist.dart';
|
||||||
import '../models/listitem.dart';
|
import '../models/listitem.dart';
|
||||||
|
|
||||||
class DbHelper {
|
class DbHelper {
|
||||||
static late final SupabaseClient _client;
|
static const checkedItemsTableName = 'checkedItems';
|
||||||
static const checklistsTableName = 'checklists';
|
static const checklistsTableName = 'checklists';
|
||||||
static const itemsTableName = 'items';
|
static const itemsTableName = 'items';
|
||||||
static const checkedItemsTableName = 'checkedItems';
|
|
||||||
|
static late final SupabaseClient _client;
|
||||||
|
|
||||||
static Future<void> init() async {
|
static Future<void> init() async {
|
||||||
await Supabase.initialize(
|
await Supabase.initialize(
|
||||||
@@ -42,7 +42,6 @@ class DbHelper {
|
|||||||
element['title'],
|
element['title'],
|
||||||
element['description'],
|
element['description'],
|
||||||
DateTime.parse(element['createdTime']),
|
DateTime.parse(element['createdTime']),
|
||||||
[],
|
|
||||||
);
|
);
|
||||||
checklists.add(cl);
|
checklists.add(cl);
|
||||||
}
|
}
|
||||||
@@ -72,19 +71,43 @@ class DbHelper {
|
|||||||
return res.last['id'] as int;
|
return res.last['id'] as int;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Future<List<Item>> getItemsByChecklistId(int checklist_id) async {
|
||||||
|
final List<Item> items = [];
|
||||||
|
|
||||||
|
final itemRes = await _client
|
||||||
|
.from(itemsTableName)
|
||||||
|
.select<List<Map<String, dynamic>>>()
|
||||||
|
.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<Checklist> getChecklistById(int id) async {
|
static Future<Checklist> getChecklistById(int id) async {
|
||||||
final res = await _client
|
final checklistRes = await _client
|
||||||
.from(checklistsTableName)
|
.from(checklistsTableName)
|
||||||
.select<Map<String, dynamic>>()
|
.select<Map<String, dynamic>>()
|
||||||
.eq('id', id)
|
.eq('id', id)
|
||||||
.single();
|
.single();
|
||||||
|
|
||||||
return Checklist(
|
return Checklist(
|
||||||
res['id'],
|
checklistRes['id'],
|
||||||
res['ownerId'],
|
checklistRes['ownerId'],
|
||||||
res['title'],
|
checklistRes['title'],
|
||||||
res['description'],
|
checklistRes['description'],
|
||||||
DateTime.parse(res['createdTime']),
|
DateTime.parse(checklistRes['createdTime']),
|
||||||
[],
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user