From 88fb731a07f6255cdee3f6052d80146cd85b7403 Mon Sep 17 00:00:00 2001 From: andrei Date: Tue, 23 Nov 2021 18:33:41 -0500 Subject: [PATCH] refactored list items into seperate class --- lib/box_checker.dart | 4 +- lib/check_list.dart | 68 +++++----------------------- lib/check_list_item.dart | 96 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 58 deletions(-) create mode 100644 lib/check_list_item.dart diff --git a/lib/box_checker.dart b/lib/box_checker.dart index 2252f6b..7075058 100644 --- a/lib/box_checker.dart +++ b/lib/box_checker.dart @@ -15,14 +15,16 @@ class BoxChecker extends StatelessWidget { Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', + // starting with dark theme but changing the primary colors manually theme: ThemeData.from(colorScheme: const ColorScheme.dark()).copyWith( canvasColor: Colors.blueGrey[900], - cardColor: Colors.grey[800], scaffoldBackgroundColor: Colors.black12, shadowColor: Colors.black38, toggleableActiveColor: Colors.blue, colorScheme: ColorScheme.fromSwatch( primarySwatch: Colors.blue, + cardColor: Colors.grey[800], + backgroundColor: Colors.black12, ), ), themeMode: ThemeMode.dark, diff --git a/lib/check_list.dart b/lib/check_list.dart index b6e9199..395e989 100644 --- a/lib/check_list.dart +++ b/lib/check_list.dart @@ -1,3 +1,4 @@ +import 'package:boxchecker/check_list_item.dart'; import 'package:flutter/material.dart'; import 'data_util.dart' as data; import 'db_helper.dart'; @@ -11,7 +12,7 @@ class CheckList extends StatefulWidget { } class _CheckList extends State { - bool _editable = false; + late bool _editable; data.List? listData; List list = []; @@ -29,6 +30,7 @@ class _CheckList extends State { void _loadListData() async { var rows = await DBHelper.dbHelper.getListData(widget.id); listData = (rows.isNotEmpty) ? data.List.fromMap(rows[0]) : null; + _editable = (listData != null && !listData!.isTemplate!); } void _addItem() async { @@ -40,14 +42,6 @@ class _CheckList extends State { }); } - void _removeItem(data.Check item) async { - DBHelper.dbHelper.deleteItem(item); - } - - void _updateItem(data.Check item) async { - DBHelper.dbHelper.updateItem(item); - } - void _toggleEditable() { setState(() { _editable = !_editable; @@ -87,65 +81,25 @@ class _CheckList extends State { child: ListView.builder( itemCount: list.length, itemBuilder: (context, index) { - return Dismissible( - key: Key(list[index].id.toString()), + return CheckListItem( confirmDismiss: (direction) async { - var item = list[index]; + if (!_editable) return false; + if (direction == DismissDirection.endToStart) { setState(() { - _removeItem(item); + DBHelper.dbHelper.deleteItem(list[index]); list.removeAt(index); }); return true; } - _updateItem(item); + DBHelper.dbHelper.updateItem(list[index]); setState(() { - item.value = !item.value; + list[index].value = !list[index].value; }); return false; }, - background: Container( - color: Colors.blue, - child: Row(children: const [ - Padding( - padding: EdgeInsets.all(10), - child: Icon(Icons.check), - ), - Spacer(), - ]), - ), - secondaryBackground: Container( - color: Colors.red, - child: Row(children: const [ - Spacer(), - Padding( - padding: EdgeInsets.all(10), - child: Icon(Icons.delete_forever)), - ]), - ), - child: CheckboxListTile( - title: TextFormField( - enabled: (listData != null && - (!listData!.isTemplate! || _editable)), - decoration: const InputDecoration(border: InputBorder.none), - initialValue: list[index].text, - onChanged: (value) { - list[index].text = value; - _updateItem(list[index]); - }, - ), - controlAffinity: ListTileControlAffinity.leading, - value: list[index].value, - onChanged: (listData != null && - (!listData!.isTemplate! || _editable)) - ? ((value) { - _updateItem(list[index]); - setState(() { - list[index].value = value!; - }); - }) - : null, - ), + item: list[index], + locked: !_editable, ); }, ), diff --git a/lib/check_list_item.dart b/lib/check_list_item.dart new file mode 100644 index 0000000..1a4da11 --- /dev/null +++ b/lib/check_list_item.dart @@ -0,0 +1,96 @@ +import 'package:boxchecker/check_list.dart'; +import 'package:flutter/material.dart'; +import 'data_util.dart' as data; +import 'db_helper.dart'; + +typedef FutureCallback = Future Function(DismissDirection); + +class CheckListItem extends StatefulWidget { + const CheckListItem( + {Key? key, + required this.item, + required this.confirmDismiss, + this.locked = false}) + : super(key: key); + final data.Check item; + final FutureCallback confirmDismiss; + final bool locked; + + @override + State createState() => _CheckListItem(); +} + +class _CheckListItem extends State { + Widget dismissIconPadding(Widget icon) { + return Padding( + padding: const EdgeInsets.all(10), + child: icon, + ); + } + + Widget dismissBackground() { + return Container( + color: Colors.blue, + child: Row(children: const [ + Padding( + padding: EdgeInsets.all(10), + child: Icon(Icons.check), + ), + Spacer(), + ])); + } + + Widget dismissSecondaryBackground() { + return Container( + color: Colors.red, + child: Row(children: [ + const Spacer(), + dismissIconPadding(const Icon(Icons.delete_forever)), + ]), + ); + } + + Widget dismissLockedBackground() { + return Container( + color: Colors.grey[900], + child: Row( + children: [ + dismissIconPadding(const Icon(Icons.lock)), + const Spacer(), + dismissIconPadding(const Icon(Icons.lock)), + ], + )); + } + + @override + Widget build(BuildContext context) { + return Dismissible( + key: Key(widget.item.id.toString()), + background: + !widget.locked ? dismissBackground() : dismissLockedBackground(), + secondaryBackground: !widget.locked ? dismissSecondaryBackground() : null, + confirmDismiss: widget.confirmDismiss, + child: CheckboxListTile( + title: TextFormField( + enabled: !widget.locked, + decoration: const InputDecoration(border: InputBorder.none), + initialValue: widget.item.text, + onChanged: (value) { + widget.item.text = value; + DBHelper.dbHelper.updateItem(widget.item); + }, + ), + controlAffinity: ListTileControlAffinity.leading, + value: widget.item.value, + onChanged: !widget.locked + ? ((value) { + DBHelper.dbHelper.updateItem(widget.item); + setState(() { + widget.item.value = value!; + }); + }) + : null, + ), + ); + } +}