124 lines
4.0 KiB
Dart
124 lines
4.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../models/informationModel.dart';
|
|
import '../services/backend/informationService.dart';
|
|
|
|
class InformationPage extends StatefulWidget {
|
|
const InformationPage({Key? key}) : super(key: key);
|
|
|
|
|
|
@override
|
|
State<InformationPage> createState() => _InformationPageState();
|
|
}
|
|
|
|
|
|
class _InformationPageState extends State<InformationPage> {
|
|
final InformationService _infoService = InformationService();
|
|
List<InformationModel> _infoList = [];
|
|
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
fetchAndSetInformation();
|
|
_markAsRead();
|
|
}
|
|
|
|
Future<void> _markAsRead() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setInt('last_seen_information', DateTime.now().millisecondsSinceEpoch);
|
|
}
|
|
|
|
|
|
Future<void> fetchAndSetInformation() async {
|
|
try {
|
|
final info = await _infoService.fetchInformation();
|
|
info.sort((a, b) => a.createdAt.compareTo(b.createdAt));
|
|
setState(() => _infoList = info);
|
|
} catch (e) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text("Kunde inte ladda information")),
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFF2E2E2E),
|
|
appBar: AppBar(
|
|
backgroundColor: const Color(0xFF2E2E2E),
|
|
title: const Text(
|
|
"Information",
|
|
style: TextStyle(color: Color(0xFFDAA520)),
|
|
),
|
|
iconTheme: const IconThemeData(color: Color(0xFFDAA520)),
|
|
),
|
|
body: _infoList.isEmpty
|
|
? const Center(
|
|
child: CircularProgressIndicator(color: Color(0xFFDAA520)),
|
|
)
|
|
: ListView.builder(
|
|
itemCount: _infoList.length,
|
|
itemBuilder: (context, index) {
|
|
final info = _infoList[index];
|
|
return Card(
|
|
margin: const EdgeInsets.symmetric(
|
|
horizontal: 16,
|
|
vertical: 8,
|
|
),
|
|
child: ListTile(
|
|
leading: const Icon(Icons.info, color: Color(0xFFDAA520)),
|
|
title: Text(
|
|
info.title,
|
|
style: const TextStyle(
|
|
fontWeight: FontWeight.bold,
|
|
fontSize: 16,
|
|
),
|
|
),
|
|
subtitle: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
info.content,
|
|
style: const TextStyle(color: Colors.black87),
|
|
),
|
|
const SizedBox(height: 6),
|
|
Row(
|
|
children: [
|
|
const Icon(Icons.person, size: 16, color: Colors.grey),
|
|
const SizedBox(width: 4),
|
|
Text(
|
|
"Av: ${info.author}",
|
|
style: const TextStyle(color: Colors.grey),
|
|
),
|
|
],
|
|
),
|
|
Row(
|
|
children: [
|
|
const Icon(Icons.access_time, size: 16, color: Colors.grey),
|
|
const SizedBox(width: 4),
|
|
Text(
|
|
DateFormat('yyyy-MM-dd HH:mm').format(info.createdAt),
|
|
style: const TextStyle(
|
|
color: Colors.grey,
|
|
fontSize: 12,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
|