128 lines
4.0 KiB
Dart
128 lines
4.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:insparkspokalen_ui/models/informationModel.dart';
|
|
import 'package:insparkspokalen_ui/services/informationService.dart';
|
|
|
|
class AdminInformationPage extends StatefulWidget {
|
|
const AdminInformationPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<AdminInformationPage> createState() => _AdminInformationPageState();
|
|
}
|
|
|
|
class _AdminInformationPageState extends State<AdminInformationPage> {
|
|
List<InformationModel> informationList = [];
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_fetchInformation();
|
|
}
|
|
|
|
void _fetchInformation() async {
|
|
try {
|
|
List<InformationModel> fetchedInfo = await InformationService().fetchInformation();
|
|
setState(() {
|
|
informationList = fetchedInfo;
|
|
});
|
|
} catch (e) {
|
|
print("Fel vid hämtning: $e");
|
|
}
|
|
}
|
|
|
|
void _showAddDialog() {
|
|
final titleController = TextEditingController();
|
|
final contentController = TextEditingController();
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text("Lägg till informationsmeddelande"),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
TextField(controller: titleController, decoration: const InputDecoration(labelText: "Titel")),
|
|
TextField(controller: contentController, decoration: const InputDecoration(labelText: "Innehåll")),
|
|
],
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const Text("Avbryt"),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: () async {
|
|
final info = InformationModel(
|
|
title: titleController.text,
|
|
content: contentController.text,
|
|
author: 'Admin',
|
|
createdAt: DateTime.now(),
|
|
);
|
|
try {
|
|
await InformationService().createInformation(info);
|
|
setState(() => informationList.add(info));
|
|
} catch (e) {
|
|
print("Misslyckades att skapa: $e");
|
|
}
|
|
Navigator.pop(context);
|
|
},
|
|
child: const Text("Lägg till"),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void _deleteInformation(InformationModel info) async {
|
|
try {
|
|
await InformationService().deleteInformation(info);
|
|
setState(() => informationList.remove(info));
|
|
} catch (e) {
|
|
print("Misslyckades att ta bort: $e");
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text("Hantera Information"),
|
|
backgroundColor: const Color(0xFF2E2E2E),
|
|
foregroundColor: const Color(0xFFDAA520),
|
|
),
|
|
backgroundColor: const Color(0xFF2E2E2E),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: _showAddDialog,
|
|
backgroundColor: const Color(0xFFDAA520),
|
|
child: const Icon(Icons.add, color: Colors.black),
|
|
),
|
|
body: ListView.builder(
|
|
itemCount: informationList.length,
|
|
itemBuilder: (context, index) {
|
|
final info = informationList[index];
|
|
return Card(
|
|
margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
|
|
child: ListTile(
|
|
title: Text(info.title),
|
|
subtitle: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(info.content),
|
|
const SizedBox(height: 4),
|
|
Text("Av: ${info.author}", style: const TextStyle(fontSize: 12, fontStyle: FontStyle.italic)),
|
|
Text("Skapad: ${DateFormat('yyyy-MM-dd HH:mm').format(info.createdAt)}", style: const TextStyle(fontSize: 12)),
|
|
],
|
|
),
|
|
trailing: IconButton(
|
|
icon: const Icon(Icons.delete, color: Colors.red),
|
|
onPressed: () => _deleteInformation(info),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|