CRUD working
This commit is contained in:
@@ -1,10 +1,87 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:frontend/provider/quote_provider.dart';
|
||||
import 'package:frontend/model/quote.dart';
|
||||
|
||||
import 'package:frontend/strings.dart';
|
||||
|
||||
class AddPage extends StatelessWidget {
|
||||
const AddPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const Center(child: Text('add page!'));
|
||||
final bodyController = TextEditingController(text: '');
|
||||
final authorController = TextEditingController(text: '');
|
||||
final provider = QuoteProvider();
|
||||
return Card(
|
||||
margin: const EdgeInsets.all(16),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
Strings.createQuote,
|
||||
style: const TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 12),
|
||||
|
||||
TextField(
|
||||
autofocus: true,
|
||||
controller: bodyController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: Strings.quote,
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 8),
|
||||
|
||||
TextField(
|
||||
controller: authorController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: Strings.author,
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
bodyController.clear();
|
||||
authorController.clear();
|
||||
},
|
||||
child: const Text(Strings.cancel),
|
||||
),
|
||||
|
||||
const SizedBox(width: 8),
|
||||
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
final quote = Quote(
|
||||
id: 0,
|
||||
body: bodyController.text,
|
||||
author: authorController.text,
|
||||
);
|
||||
|
||||
provider.createQuote(quote);
|
||||
|
||||
bodyController.clear();
|
||||
authorController.clear();
|
||||
},
|
||||
child: const Text(Strings.save),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,153 @@
|
||||
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) {
|
||||
return Center(child: const Text('list page!'));
|
||||
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),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user