132 lines
4.8 KiB
Dart
132 lines
4.8 KiB
Dart
|
import 'package:diameter/components/progress_indicator.dart';
|
||
|
import 'package:diameter/models/meal_source.dart';
|
||
|
import 'package:diameter/navigation.dart';
|
||
|
import 'package:diameter/screens/meal/meal_source_detail.dart';
|
||
|
import 'package:flutter/material.dart';
|
||
|
|
||
|
class MealSourceListScreen extends StatefulWidget {
|
||
|
static const String routeName = '/meal-sources';
|
||
|
|
||
|
const MealSourceListScreen({Key? key}) : super(key: key);
|
||
|
|
||
|
@override
|
||
|
_MealSourceListScreenState createState() => _MealSourceListScreenState();
|
||
|
}
|
||
|
|
||
|
class _MealSourceListScreenState extends State<MealSourceListScreen> {
|
||
|
late Future<List<MealSource>?> _mealSources;
|
||
|
|
||
|
void refresh({String? message}) {
|
||
|
setState(() {
|
||
|
_mealSources = MealSource.fetchAll();
|
||
|
});
|
||
|
setState(() {
|
||
|
if (message != null) {
|
||
|
var snackBar = SnackBar(
|
||
|
content: Text(message),
|
||
|
duration: const Duration(seconds: 2),
|
||
|
);
|
||
|
ScaffoldMessenger.of(context)
|
||
|
..removeCurrentSnackBar()
|
||
|
..showSnackBar(snackBar);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void initState() {
|
||
|
super.initState();
|
||
|
refresh();
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return Scaffold(
|
||
|
appBar: AppBar(
|
||
|
title: const Text('Meal Sources'),
|
||
|
actions: <Widget>[
|
||
|
IconButton(onPressed: refresh, icon: const Icon(Icons.refresh))
|
||
|
],
|
||
|
),
|
||
|
drawer: const Navigation(currentLocation: MealSourceListScreen.routeName),
|
||
|
body: Column(
|
||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||
|
children: <Widget>[
|
||
|
Expanded(
|
||
|
child: FutureBuilder<List<MealSource>?>(
|
||
|
future: _mealSources,
|
||
|
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 Meal Sources'),
|
||
|
)
|
||
|
],
|
||
|
)
|
||
|
: ListView.builder(
|
||
|
padding: const EdgeInsets.only(top: 10.0),
|
||
|
itemCount: snapshot.data != null
|
||
|
? snapshot.data!.length
|
||
|
: 0,
|
||
|
itemBuilder: (context, index) {
|
||
|
final mealSource = snapshot.data![index];
|
||
|
return ListTile(
|
||
|
onTap: () {
|
||
|
Navigator.push(
|
||
|
context,
|
||
|
MaterialPageRoute(
|
||
|
builder: (context) =>
|
||
|
MealSourceDetailScreen(
|
||
|
mealSource: mealSource,
|
||
|
),
|
||
|
),
|
||
|
).then(
|
||
|
(message) => refresh(message: message));
|
||
|
},
|
||
|
title: Text(mealSource.value),
|
||
|
subtitle: Text(mealSource.notes ?? ''),
|
||
|
trailing: Row(
|
||
|
mainAxisSize: MainAxisSize.min,
|
||
|
children: [
|
||
|
IconButton(
|
||
|
icon: const Icon(
|
||
|
Icons.delete,
|
||
|
color: Colors.blue,
|
||
|
),
|
||
|
onPressed: () async {
|
||
|
// add confirmation dialog
|
||
|
await mealSource.delete().then((_) {
|
||
|
refresh(
|
||
|
message: 'Meal Source deleted');
|
||
|
});
|
||
|
},
|
||
|
),
|
||
|
],
|
||
|
),
|
||
|
);
|
||
|
}));
|
||
|
},
|
||
|
),
|
||
|
),
|
||
|
],
|
||
|
),
|
||
|
floatingActionButton: FloatingActionButton(
|
||
|
onPressed: () {
|
||
|
Navigator.push(
|
||
|
context,
|
||
|
MaterialPageRoute(
|
||
|
builder: (context) => const MealSourceDetailScreen(),
|
||
|
),
|
||
|
).then((message) => refresh(message: message));
|
||
|
},
|
||
|
child: const Icon(Icons.add),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|