186 lines
7.1 KiB
Dart
186 lines
7.1 KiB
Dart
import 'package:diameter/components/dialogs.dart';
|
|
import 'package:diameter/components/progress_indicator.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> {
|
|
late Future<List<Accuracy>?> _accuracies;
|
|
|
|
void refresh({String? message}) {
|
|
setState(() {
|
|
_accuracies = Accuracy.fetchAll();
|
|
});
|
|
|
|
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.delete().then((_) => refresh(message: 'Accuracy deleted'));
|
|
}
|
|
|
|
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 {
|
|
await Accuracy.update(
|
|
accuracy.objectId!,
|
|
forPortionSize: !accuracy.forPortionSize,
|
|
);
|
|
refresh();
|
|
}
|
|
|
|
void handleToggleForCarbsRatioAction(Accuracy accuracy) async {
|
|
await Accuracy.update(
|
|
accuracy.objectId!,
|
|
forCarbsRatio: !accuracy.forCarbsRatio,
|
|
);
|
|
refresh();
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
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: FutureBuilder<List<Accuracy>?>(
|
|
future: _accuracies,
|
|
builder: (context, snapshot) {
|
|
return ViewWithProgressIndicator(
|
|
snapshot: snapshot,
|
|
child: snapshot.data == null || snapshot.data!.isEmpty
|
|
? Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: const [
|
|
Padding(
|
|
padding: EdgeInsets.all(10.0),
|
|
child: Text('No Accuracies'),
|
|
)
|
|
],
|
|
)
|
|
: ListView.builder(
|
|
padding: const EdgeInsets.only(top: 10.0),
|
|
itemCount: snapshot.data != null
|
|
? snapshot.data!.length
|
|
: 0,
|
|
itemBuilder: (context, index) {
|
|
final accuracy = snapshot.data![index];
|
|
return ListTile(
|
|
onTap: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) =>
|
|
AccuracyDetailScreen(
|
|
accuracy: accuracy),
|
|
),
|
|
).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
|
|
? Colors.blue
|
|
: Colors.grey.shade300),
|
|
onPressed: () =>
|
|
handleToggleForPortionSizeAction(
|
|
accuracy)),
|
|
IconButton(
|
|
icon: Icon(Icons.pie_chart,
|
|
color: accuracy.forCarbsRatio
|
|
? Colors.blue
|
|
: Colors.grey.shade300),
|
|
onPressed: () =>
|
|
handleToggleForCarbsRatioAction(
|
|
accuracy),
|
|
),
|
|
const SizedBox(width: 24),
|
|
IconButton(
|
|
icon: const Icon(
|
|
Icons.delete,
|
|
color: Colors.blue,
|
|
),
|
|
onPressed: () =>
|
|
handleDeleteAction(accuracy),
|
|
)
|
|
],
|
|
),
|
|
);
|
|
}));
|
|
}),
|
|
),
|
|
],
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => const AccuracyDetailScreen(),
|
|
)).then((message) => refresh(message: message));
|
|
},
|
|
child: const Icon(Icons.add),
|
|
),
|
|
);
|
|
}
|
|
}
|