import 'package:boxchecker/list_card.dart'; import 'package:flutter/material.dart'; import 'add_form.dart'; import 'check_list.dart'; import 'db_helper.dart'; import 'data_util.dart' as data; typedef FutureCallback = Future Function(DismissDirection); 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', // starting with dark theme but changing the primary colors manually theme: ThemeData.from(colorScheme: const ColorScheme.dark()).copyWith( canvasColor: Colors.blueGrey[900], 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, home: const MainListPage(title: 'BoxChecker'), ); } } class MainListPage extends StatefulWidget { const MainListPage({Key? key, required this.title}) : super(key: key); final String title; @override State createState() => _MainListPageState(); } class _MainListPageState extends State { int _selectedPage = data.Page.lists.index; List lists = []; final double _marginHorizontal = 10; Future _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)); } }); } @override void initState() { _loadData(data.Page.lists); super.initState(); } FutureCallback genCofirmDismiss(index) { return (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(() { DBHelper.dbHelper.deleteList(lists[index]); lists.removeAt(index); }); } return value; }); }; } @override Widget build(BuildContext context) { 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 ListCard( checkList: lists[index], confirmDismiss: genCofirmDismiss(index), ); }))), 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", ), ])); } }