153 lines
4.6 KiB
Dart
153 lines
4.6 KiB
Dart
import 'package:flutter/material.dart';
|
||
|
||
import 'package:frontend/provider/quote_provider.dart';
|
||
import 'package:frontend/model/quote.dart';
|
||
import 'package:frontend/strings.dart';
|
||
import 'package:provider/provider.dart';
|
||
|
||
class ListPage extends StatelessWidget {
|
||
const ListPage({super.key});
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final provider = context.watch<QuoteProvider>();
|
||
return RefreshIndicator(
|
||
onRefresh: () async {
|
||
await provider.readQuotes();
|
||
},
|
||
child: _bodyBuilder(provider),
|
||
);
|
||
}
|
||
|
||
Widget _bodyBuilder(QuoteProvider provider) {
|
||
if (provider.loading) {
|
||
return Center(child: CircularProgressIndicator());
|
||
}
|
||
|
||
if (provider.error != null) {
|
||
return Center(child: Text(provider.error!));
|
||
}
|
||
|
||
return
|
||
ListView.builder(
|
||
itemCount: provider.quotes.length,
|
||
itemBuilder: (context, index) {
|
||
final quote = provider.quotes[index];
|
||
|
||
return
|
||
ListTile(
|
||
title: Text(quote.body),
|
||
subtitle: Text("– ${quote.author}"),
|
||
trailing: PopupMenuButton<String>(
|
||
icon: const Icon(Icons.more_vert),
|
||
|
||
onSelected: (value) {
|
||
if (value == 'update') {
|
||
// open a dialog window that allows for mods
|
||
_showUpdateDialog(context, provider, quote);
|
||
} else if (value == 'delete') {
|
||
// open a confirmation dialog window for deletion
|
||
_showDeleteDialog(context, provider, quote);
|
||
}
|
||
},
|
||
|
||
itemBuilder: (context) => [
|
||
const PopupMenuItem(
|
||
value: 'update',
|
||
child: Row(
|
||
children: [
|
||
Icon(Icons.edit),
|
||
SizedBox(width: 10),
|
||
Text(Strings.updateQuote),
|
||
],
|
||
),
|
||
),
|
||
|
||
const PopupMenuItem(
|
||
value: 'delete',
|
||
child: Row(
|
||
children: [
|
||
Icon(Icons.delete),
|
||
SizedBox(width: 10),
|
||
Text(Strings.deleteQuote),
|
||
],
|
||
),
|
||
),
|
||
],
|
||
)
|
||
);
|
||
},
|
||
);
|
||
}
|
||
|
||
void _showUpdateDialog(BuildContext context, QuoteProvider provider, Quote quote) {
|
||
final bodyController = TextEditingController(text: quote.body);
|
||
final authorController = TextEditingController(text: quote.author);
|
||
showDialog(
|
||
context: context,
|
||
builder: (context) {
|
||
return AlertDialog(
|
||
title: Text(Strings.updateQuote),
|
||
content: Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
children: [
|
||
TextField(
|
||
autofocus: true,
|
||
controller: bodyController,
|
||
),
|
||
TextField(
|
||
controller: authorController,
|
||
),
|
||
],
|
||
),
|
||
actions: [
|
||
TextButton(
|
||
onPressed: () => Navigator.pop(context),
|
||
child: const Text(Strings.cancel),
|
||
),
|
||
ElevatedButton(
|
||
onPressed: () {
|
||
final updatedQuote = Quote(
|
||
id: quote.id,
|
||
body: bodyController.text,
|
||
author: authorController.text,
|
||
);
|
||
Navigator.pop(context);
|
||
provider.updateQuote(updatedQuote);
|
||
},
|
||
child: const Text(Strings.save),
|
||
),
|
||
],
|
||
);
|
||
},
|
||
);
|
||
}
|
||
|
||
void _showDeleteDialog(BuildContext context, QuoteProvider provider, Quote quote) {
|
||
showDialog(
|
||
context: context,
|
||
builder: (context) {
|
||
return AlertDialog(
|
||
title: const Text('${Strings.deleteQuote}?'),
|
||
content: const Text(Strings.noUndo),
|
||
actions: [
|
||
TextButton(
|
||
onPressed: () => Navigator.pop(context),
|
||
child: const Text(Strings.cancel),
|
||
),
|
||
ElevatedButton(
|
||
style: ElevatedButton.styleFrom(
|
||
backgroundColor: Colors.red,
|
||
),
|
||
onPressed: () {
|
||
Navigator.pop(context);
|
||
provider.deleteQuote(quote);
|
||
},
|
||
child: const Text(Strings.delete),
|
||
),
|
||
],
|
||
);
|
||
},
|
||
);
|
||
}
|
||
} |