163 lines
5.1 KiB
Dart
163 lines
5.1 KiB
Dart
import 'package:diameter/components/dialogs.dart';
|
|
import 'package:diameter/config.dart';
|
|
import 'package:diameter/navigation.dart';
|
|
import 'package:diameter/screens/accuracy_detail.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:diameter/models/accuracy.dart';
|
|
|
|
class AccuracyListScreen extends StatefulWidget {
|
|
static const String routeName = '/accuracies';
|
|
const AccuracyListScreen({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
_AccuracyListScreenState createState() => _AccuracyListScreenState();
|
|
}
|
|
|
|
class _AccuracyListScreenState extends State<AccuracyListScreen> {
|
|
List<Accuracy> _accuracies = Accuracy.getAll();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
refresh();
|
|
}
|
|
|
|
void refresh({String? message}) {
|
|
setState(() {
|
|
_accuracies = Accuracy.getAll();
|
|
});
|
|
|
|
setState(() {
|
|
if (message != null) {
|
|
var snackBar = SnackBar(
|
|
content: Text(message),
|
|
duration: const Duration(seconds: 2),
|
|
);
|
|
ScaffoldMessenger.of(context)
|
|
..removeCurrentSnackBar()
|
|
..showSnackBar(snackBar);
|
|
}
|
|
});
|
|
}
|
|
|
|
void onDelete(Accuracy accuracy) {
|
|
Accuracy.remove(accuracy.id);
|
|
refresh();
|
|
}
|
|
|
|
void handleDeleteAction(Accuracy accuracy) async {
|
|
if (showConfirmationDialogOnDelete) {
|
|
Dialogs.showConfirmationDialog(
|
|
context: context,
|
|
onConfirm: () => onDelete(accuracy),
|
|
message: 'Are you sure you want to delete this Accuracy?',
|
|
);
|
|
} else {
|
|
onDelete(accuracy);
|
|
}
|
|
}
|
|
|
|
void handleToggleForPortionSizeAction(Accuracy accuracy) async {
|
|
accuracy.forPortionSize = !accuracy.forPortionSize;
|
|
Accuracy.put(accuracy);
|
|
refresh();
|
|
}
|
|
|
|
void handleToggleForCarbsRatioAction(Accuracy accuracy) async {
|
|
accuracy.forCarbsRatio = !accuracy.forCarbsRatio;
|
|
Accuracy.put(accuracy);
|
|
refresh();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Accuracies'),
|
|
actions: <Widget>[
|
|
IconButton(onPressed: refresh, icon: const Icon(Icons.refresh))
|
|
],
|
|
),
|
|
drawer: const Navigation(currentLocation: AccuracyListScreen.routeName),
|
|
body: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
Expanded(
|
|
child: _accuracies.isNotEmpty ? ListView.builder(
|
|
padding: const EdgeInsets.only(top: 10.0),
|
|
itemCount: _accuracies.length,
|
|
itemBuilder: (context, index) {
|
|
final accuracy = _accuracies[index];
|
|
return ListTile(
|
|
onTap: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) =>
|
|
AccuracyDetailScreen(id: accuracy.id),
|
|
),
|
|
).then((message) => refresh(message: message));
|
|
},
|
|
title: Text(accuracy.value),
|
|
leading: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
IconButton(
|
|
icon: const Icon(Icons.reorder),
|
|
onPressed: () {
|
|
// TODO: implement reordering
|
|
},
|
|
),
|
|
],
|
|
),
|
|
trailing: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
IconButton(
|
|
icon: Icon(
|
|
Icons.square_foot,
|
|
color: accuracy.forPortionSize
|
|
? Theme.of(context).toggleableActiveColor
|
|
: Theme.of(context).highlightColor,
|
|
),
|
|
onPressed: () => handleToggleForPortionSizeAction(accuracy),
|
|
),
|
|
IconButton(
|
|
icon: Icon(
|
|
Icons.pie_chart,
|
|
color: accuracy.forCarbsRatio
|
|
? Theme.of(context).toggleableActiveColor
|
|
: Theme.of(context).highlightColor,
|
|
),
|
|
onPressed: () => handleToggleForCarbsRatioAction(accuracy),
|
|
),
|
|
const SizedBox(width: 24),
|
|
IconButton(
|
|
icon: const Icon(Icons.delete),
|
|
onPressed: () => handleDeleteAction(accuracy),
|
|
)
|
|
],
|
|
),
|
|
);
|
|
}
|
|
) : const Center(
|
|
child: Text('You have not created any Accuracies yet!'),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => const AccuracyDetailScreen(),
|
|
),
|
|
).then((message) => refresh(message: message));
|
|
},
|
|
child: const Icon(Icons.add),
|
|
),
|
|
);
|
|
}
|
|
}
|