boxchecker/lib/box_checker.dart

187 lines
6.6 KiB
Dart

import 'package:flutter/material.dart';
import 'add_form.dart';
import 'check_list.dart';
import 'db_helper.dart';
import 'data_util.dart' as data;
void main() {
runApp(const BoxChecker());
}
class BoxChecker extends StatelessWidget {
const BoxChecker({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData.from(colorScheme: 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,
),
),
themeMode: ThemeMode.dark,
home: const MainListPage(title: 'BoxChecker'),
);
}
}
class MainListPage extends StatefulWidget {
const MainListPage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MainListPage> createState() => _MainListPageState();
}
class _MainListPageState extends State<MainListPage> {
int _selectedPage = data.Page.lists.index;
List<data.List> lists = [];
final double _marginHorizontal = 10;
final double _cardSpacing = 4;
Future<void> _loadData(data.Page listType) async {
lists.clear();
var res = await DBHelper.dbHelper.getAllLists(listType);
setState(() {
for (var row in res) {
lists.add(data.List.fromMap(row));
}
});
}
Future<void> _removeList(data.List list) async {
DBHelper.dbHelper.deleteList(list);
}
@override
void initState() {
_loadData(data.Page.lists);
super.initState();
}
@override
Widget build(BuildContext context) {
final EdgeInsets cardMargin =
EdgeInsets.symmetric(vertical: _cardSpacing, horizontal: 0);
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
AddForm(type: data.Page.values[_selectedPage])))
.then((value) => _loadData(data.Page.values[_selectedPage]));
},
tooltip: 'Add List',
child: const Icon(Icons.add),
),
body: Container(
padding: EdgeInsets.symmetric(horizontal: _marginHorizontal),
child: RefreshIndicator(
onRefresh: () => _loadData(data.Page.values[_selectedPage]),
child: ListView.builder(
itemCount: lists.length,
itemBuilder: (context, index) {
return Dismissible(
direction: DismissDirection.startToEnd,
key: Key(lists[index].id.toString()),
background: Card(
margin: cardMargin,
color: Colors.red,
child: Row(
children: const [
Padding(
padding: EdgeInsets.all(10),
child: Icon(Icons.delete),
),
],
),
),
confirmDismiss: (direction) async {
if (direction != DismissDirection.startToEnd) {
return false;
}
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Are you sure?'),
content: const Text(
'You are about to delete this permently'),
actions: [
TextButton(
onPressed: () =>
Navigator.pop(context, false),
child: const Text('Cancel')),
TextButton(
onPressed: () =>
Navigator.pop(context, true),
child: const Text('OK')),
],
);
}).then((value) {
if (value) {
setState(() {
_removeList(lists[index]);
lists.removeAt(index);
});
}
return value;
});
},
child: Card(
margin: cardMargin,
child: ListTile(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
CheckList(id: lists[index].id!)));
},
title: Text(lists[index].name),
subtitle: Text((lists[index].description != null)
? lists[index].description!
: ""),
),
),
);
}))),
bottomNavigationBar: BottomNavigationBar(
elevation: 4,
currentIndex: _selectedPage,
onTap: (index) {
setState(() {
_selectedPage = index;
_loadData(data.Page.values[_selectedPage]);
});
},
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.checklist),
label: "Lists",
),
BottomNavigationBarItem(
icon: Icon(Icons.list_alt),
label: "Templates",
),
]));
}
}