137 lines
4.5 KiB
Dart
137 lines
4.5 KiB
Dart
|
import 'package:diameter/components/dialogs.dart';
|
||
|
import 'package:diameter/components/progress_indicator.dart';
|
||
|
import 'package:diameter/config.dart';
|
||
|
import 'package:diameter/models/meal.dart';
|
||
|
import 'package:diameter/navigation.dart';
|
||
|
import 'package:diameter/screens/meal/meal_detail.dart';
|
||
|
import 'package:flutter/material.dart';
|
||
|
|
||
|
class MealListScreen extends StatefulWidget {
|
||
|
static const String routeName = '/meals';
|
||
|
|
||
|
const MealListScreen({Key? key}) : super(key: key);
|
||
|
|
||
|
@override
|
||
|
_MealListScreenState createState() => _MealListScreenState();
|
||
|
}
|
||
|
|
||
|
class _MealListScreenState extends State<MealListScreen> {
|
||
|
late Future<List<Meal>?> _meals;
|
||
|
|
||
|
void refresh({String? message}) {
|
||
|
setState(() {
|
||
|
_meals = Meal.fetchAll();
|
||
|
});
|
||
|
setState(() {
|
||
|
if (message != null) {
|
||
|
var snackBar = SnackBar(
|
||
|
content: Text(message),
|
||
|
duration: const Duration(seconds: 2),
|
||
|
);
|
||
|
ScaffoldMessenger.of(context)
|
||
|
..removeCurrentSnackBar()
|
||
|
..showSnackBar(snackBar);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
void onDelete(Meal meal) {
|
||
|
meal.delete().then((_) => refresh(message: 'Meal deleted'));
|
||
|
}
|
||
|
|
||
|
void handleDeleteAction(Meal meal) async {
|
||
|
if (showConfirmationDialogOnDelete) {
|
||
|
Dialogs.showConfirmationDialog(
|
||
|
context: context,
|
||
|
onConfirm: () => onDelete(meal),
|
||
|
message: 'Are you sure you want to delete this Meal?',
|
||
|
);
|
||
|
} else {
|
||
|
onDelete(meal);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void initState() {
|
||
|
super.initState();
|
||
|
refresh();
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return Scaffold(
|
||
|
appBar: AppBar(title: const Text('Meals'), actions: <Widget>[
|
||
|
IconButton(onPressed: refresh, icon: const Icon(Icons.refresh))
|
||
|
]),
|
||
|
drawer: const Navigation(currentLocation: MealListScreen.routeName),
|
||
|
body: Column(
|
||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||
|
children: <Widget>[
|
||
|
Expanded(
|
||
|
child: FutureBuilder<List<Meal>?>(
|
||
|
future: _meals,
|
||
|
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 Meals'),
|
||
|
),
|
||
|
],
|
||
|
)
|
||
|
: ListView.builder(
|
||
|
padding: const EdgeInsets.all(10.0),
|
||
|
itemCount:
|
||
|
snapshot.data != null ? snapshot.data!.length : 0,
|
||
|
itemBuilder: (context, index) {
|
||
|
final meal = snapshot.data![index];
|
||
|
return ListTile(
|
||
|
onTap: () {
|
||
|
Navigator.push(
|
||
|
context,
|
||
|
MaterialPageRoute(
|
||
|
builder: (context) =>
|
||
|
MealDetailScreen(meal: meal),
|
||
|
),
|
||
|
).then((message) => refresh(message: message));
|
||
|
},
|
||
|
title: Text(meal.value),
|
||
|
subtitle: Text(meal.notes ?? ''),
|
||
|
trailing: Row(
|
||
|
mainAxisSize: MainAxisSize.min,
|
||
|
children: [
|
||
|
IconButton(
|
||
|
onPressed: () => handleDeleteAction(meal),
|
||
|
icon: const Icon(Icons.delete,
|
||
|
color: Colors.blue),
|
||
|
)
|
||
|
],
|
||
|
),
|
||
|
);
|
||
|
},
|
||
|
),
|
||
|
);
|
||
|
},
|
||
|
),
|
||
|
),
|
||
|
],
|
||
|
),
|
||
|
floatingActionButton: FloatingActionButton(
|
||
|
onPressed: () {
|
||
|
Navigator.push(
|
||
|
context,
|
||
|
MaterialPageRoute(
|
||
|
builder: (context) => const MealDetailScreen(),
|
||
|
),
|
||
|
).then((message) => refresh(message: message));
|
||
|
},
|
||
|
child: const Icon(Icons.add),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|