203 lines
6.5 KiB
Dart
203 lines
6.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:insparkspokalen_ui/models/informationModel.dart';
|
|
import 'package:insparkspokalen_ui/services/backend/informationService.dart';
|
|
|
|
class AdminInformationPage extends StatefulWidget {
|
|
const AdminInformationPage({super.key});
|
|
|
|
@override
|
|
State<AdminInformationPage> createState() => _AdminInformationPageState();
|
|
}
|
|
|
|
class _AdminInformationPageState extends State<AdminInformationPage> {
|
|
List<InformationModel> informationList = [];
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadInformation();
|
|
}
|
|
|
|
Future<void> _loadInformation() async {
|
|
try {
|
|
final info = await InformationService().fetchInformation();
|
|
info.sort((a, b) => a.createdAt.compareTo(b.createdAt));
|
|
setState(() {
|
|
informationList = info;
|
|
});
|
|
} catch (e) {
|
|
print("Fel vid inläsning av information: $e");
|
|
}
|
|
}
|
|
|
|
void _deleteInformation(InformationModel info) async {
|
|
try {
|
|
await InformationService().deleteInformation(info);
|
|
setState(() => informationList.remove(info));
|
|
} catch (e) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text("Kunde inte ta bort information")),
|
|
);
|
|
}
|
|
}
|
|
|
|
void _showAddDialog() {
|
|
final titleController = TextEditingController();
|
|
final contentController = TextEditingController();
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder:
|
|
(context) => AlertDialog(
|
|
backgroundColor: Colors.white,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
|
|
title: const Text(
|
|
"Lägg till information",
|
|
style: TextStyle(color: Colors.black),
|
|
),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
TextField(
|
|
controller: titleController,
|
|
decoration: const InputDecoration(
|
|
labelText: "Titel",
|
|
labelStyle: TextStyle(color: Colors.black),
|
|
),
|
|
style: const TextStyle(color: Colors.black),
|
|
),
|
|
TextField(
|
|
controller: contentController,
|
|
decoration: const InputDecoration(
|
|
labelText: "Innehåll",
|
|
labelStyle: TextStyle(color: Colors.black),
|
|
),
|
|
style: const TextStyle(color: Colors.black),
|
|
),
|
|
],
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
child: const Text(
|
|
"Avbryt",
|
|
style: TextStyle(color: Colors.black),
|
|
),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: () async {
|
|
final newInfo = InformationModel(
|
|
title: titleController.text.trim(),
|
|
content: contentController.text.trim(),
|
|
author: 'Admin',
|
|
createdAt: DateTime.now(),
|
|
);
|
|
try {
|
|
await InformationService().createInformation(newInfo);
|
|
Navigator.of(context).pop();
|
|
_loadInformation();
|
|
} catch (e) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text("Kunde inte skapa information"),
|
|
),
|
|
);
|
|
}
|
|
},
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.yellow,
|
|
foregroundColor: Colors.black,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
),
|
|
child: const Text("Lägg till"),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void _confirmDelete(BuildContext context, InformationModel info) {
|
|
showDialog(
|
|
context: context,
|
|
builder:
|
|
(context) => AlertDialog(
|
|
title: const Text("Ta bort inlägg"),
|
|
content: const Text(
|
|
"Är du säker på att du vill ta bort detta inlägg?",
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
child: const Text("Avbryt"),
|
|
),
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
_deleteInformation(info);
|
|
},
|
|
child: const Text(
|
|
"Ta bort",
|
|
style: TextStyle(color: Colors.red),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFF2E2E2E),
|
|
appBar: AppBar(
|
|
title: const Text("Information"),
|
|
backgroundColor: const Color(0xFF2E2E2E),
|
|
foregroundColor: Color(0xFFDAA520),
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: _showAddDialog,
|
|
backgroundColor: Colors.white,
|
|
foregroundColor: Colors.black,
|
|
child: const Icon(Icons.add),
|
|
),
|
|
body:
|
|
informationList.isEmpty
|
|
? const Center(
|
|
child: CircularProgressIndicator(color: Color(0xFFDAA520)),
|
|
)
|
|
: ListView.builder(
|
|
itemCount: informationList.length,
|
|
itemBuilder: (context, index) {
|
|
final info = informationList[index];
|
|
return Card(
|
|
color: Colors.white,
|
|
margin: const EdgeInsets.symmetric(
|
|
horizontal: 16,
|
|
vertical: 8,
|
|
),
|
|
child: ListTile(
|
|
title: Text(
|
|
info.title,
|
|
style: const TextStyle(
|
|
fontWeight: FontWeight.normal,
|
|
fontSize: 18,
|
|
),
|
|
),
|
|
subtitle: Text(info.content),
|
|
trailing: IconButton(
|
|
icon: const Icon(Icons.delete, color: Colors.red),
|
|
onPressed: () => _confirmDelete(context, info),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|