diameter/lib/screens/bolus/bolus_list.dart

127 lines
4.2 KiB
Dart
Raw Normal View History

2021-10-22 23:08:09 +00:00
import 'package:diameter/components/dialogs.dart';
import 'package:diameter/config.dart';
import 'package:flutter/material.dart';
import 'package:diameter/components/progress_indicator.dart';
import 'package:diameter/models/bolus.dart';
import 'package:diameter/models/bolus_profile.dart';
import 'package:diameter/screens/bolus/bolus_detail.dart';
class BolusListScreen extends StatefulWidget {
final BolusProfile? bolusProfile;
const BolusListScreen({Key? key, this.bolusProfile}) : super(key: key);
@override
_BolusListScreenState createState() => _BolusListScreenState();
}
class _BolusListScreenState extends State<BolusListScreen> {
void refresh({String? message}) {
setState(() {
if (widget.bolusProfile != null) {
widget.bolusProfile!.bolusRates =
Bolus.fetchAllForBolusProfile(widget.bolusProfile!);
}
});
setState(() {
if (message != null) {
var snackBar = SnackBar(
content: Text(message),
duration: const Duration(seconds: 2),
);
ScaffoldMessenger.of(context)
..removeCurrentSnackBar()
..showSnackBar(snackBar);
}
});
}
void handleEditAction(Bolus bolus) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BolusDetailScreen(
bolusProfile: widget.bolusProfile!,
bolus: bolus,
),
),
).then((message) => refresh(message: message));
}
void onDelete(Bolus bolus) {
bolus.delete().then((_) => refresh(message: 'Bolus Rate deleted'));
}
void handleDeleteAction(Bolus bolus) async {
if (showConfirmationDialogOnDelete) {
Dialogs.showConfirmationDialog(
context: context,
onConfirm: () => onDelete(bolus),
message: 'Are you sure you want to delete this Bolus Rate?',
);
} else {
onDelete(bolus);
}
}
@override
void initState() {
super.initState();
refresh();
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
padding: const EdgeInsets.only(top: 10.0),
child: Column(
children: [
FutureBuilder<List<Bolus>>(
future: widget.bolusProfile!.bolusRates,
builder: (context, snapshot) {
return ViewWithProgressIndicator(
// TODO: add warning if time period is missing or has multiple rates
snapshot: snapshot,
child: snapshot.data == null || snapshot.data!.isEmpty
? const Padding(
padding: EdgeInsets.all(10.0),
child: Text('No Bolus Rates for this Profile'),
)
: ListBody(
children: [
DataTable(
columnSpacing: 10.0,
showCheckboxColumn: false,
rows: snapshot.data != null
? snapshot.data!.map((bolus) {
return DataRow(
cells: bolus.asDataTableCells(
[
IconButton(
icon: const Icon(Icons.edit),
iconSize: 16.0,
onPressed: () =>
handleEditAction(bolus)),
IconButton(
icon: const Icon(Icons.delete),
iconSize: 16.0,
onPressed: () =>
handleDeleteAction(bolus)),
],
),
);
}).toList()
: [],
columns: Bolus.asDataTableColumns(),
),
],
),
);
},
),
],
),
);
}
}