import 'package:diameter/components/dialogs.dart'; import 'package:diameter/config.dart'; import 'package:diameter/navigation.dart'; import 'package:flutter/material.dart'; import 'package:diameter/models/bolus_profile.dart'; import 'package:diameter/screens/bolus/bolus_profile_detail.dart'; class BolusProfileListScreen extends StatefulWidget { static const String routeName = '/bolus-profiles'; const BolusProfileListScreen({Key? key}) : super(key: key); @override _BolusProfileListScreenState createState() => _BolusProfileListScreenState(); } class _BolusProfileListScreenState extends State { List _bolusProfiles = []; Widget banner = Container(); bool pickActiveProfileMode = false; void reload({String? message}) { setState(() { pickActiveProfileMode = false; _bolusProfiles = BolusProfile.getAll(); }); updateBanner(); setState(() { if (message != null) { var snackBar = SnackBar( content: Text(message), duration: const Duration(seconds: 2), ); ScaffoldMessenger.of(context) ..removeCurrentSnackBar() ..showSnackBar(snackBar); } }); } void updateBanner() { int activeProfileCount = BolusProfile.activeCount(); setState(() { banner = activeProfileCount != 1 ? MaterialBanner( content: Text(activeProfileCount == 0 ? 'You currently do not have an active Bolus Profile.' : 'More than one active Bolus Profile has been found.'), leading: const CircleAvatar(child: Icon(Icons.warning)), forceActionsBelow: true, actions: activeProfileCount == 0 ? [ _bolusProfiles.isNotEmpty ? TextButton( child: const Text('ACTIVATE A PROFILE'), onPressed: handlePickActiveProfileAction, ) : Container(), TextButton( child: const Text('CREATE A NEW PROFILE'), onPressed: () => onNew(true), ), ] : [ TextButton( child: const Text('PICK A PROFILE'), onPressed: handlePickActiveProfileAction, ), ], ) : Container(); }); } void onDelete(BolusProfile bolusProfile) { BolusProfile.remove(bolusProfile.id); reload(message: 'Bolus Profile deleted'); } void handleDeleteAction(BolusProfile bolusProfile) async { if (showConfirmationDialogOnDelete) { Dialogs.showConfirmationDialog( context: context, onConfirm: () => onDelete(bolusProfile), message: 'Are you sure you want to delete this Bolus Profile?', ); } else { onDelete(bolusProfile); } } void onPickActive(BolusProfile bolusProfile) { BolusProfile.setAllInactive; bolusProfile.active = true; BolusProfile.put(bolusProfile); reload( message: '${bolusProfile.name} has been set as your active Profile'); } void handlePickActiveProfileAction() { setState(() { banner = MaterialBanner( content: const Text('Click one of the profiles to active it.'), leading: const CircleAvatar(child: Icon(Icons.info)), forceActionsBelow: true, actions: [ TextButton( child: const Text('CREATE A NEW PROFILE INSTEAD'), onPressed: () => onNew(true), ), ], ); pickActiveProfileMode = true; }); } void showDetailScreen({BolusProfile? bolusProfile, bool active = false}) { Navigator.push( context, MaterialPageRoute( builder: (context) => BolusProfileDetailScreen( id: bolusProfile?.id ?? 0, active: active), ), ).then((message) => reload(message: message)); } void onNew(bool active) { showDetailScreen(active: active); } void onEdit(BolusProfile bolusProfile) { showDetailScreen(bolusProfile: bolusProfile); } @override void initState() { super.initState(); reload(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Bolus Profiles'), actions: [ IconButton(onPressed: reload, icon: const Icon(Icons.refresh)) ], ), drawer: const Navigation(currentLocation: BolusProfileListScreen.routeName), body: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ banner, Expanded( child: _bolusProfiles.isNotEmpty ? ListView.builder( itemCount: _bolusProfiles.length, itemBuilder: (context, index) { final bolusProfile = _bolusProfiles[index]; return ListTile( tileColor: bolusProfile.active ? Colors.green.shade100 : null, onTap: () { pickActiveProfileMode ? onPickActive(bolusProfile) : onEdit(bolusProfile); }, title: Text( bolusProfile.name, ), subtitle: Text(bolusProfile.notes!), trailing: Row( mainAxisSize: MainAxisSize.min, children: [ IconButton( icon: const Icon( Icons.delete, color: Colors.blue, ), onPressed: () => handleDeleteAction(bolusProfile), ), ], ), ); }, ) : const Center( child: Text('You have not created any Bolus Profiles yet!'), ), ), ], ), floatingActionButton: FloatingActionButton( onPressed: () => onNew(false), child: const Icon(Icons.add), ), ); } }