This repository has been archived on 2024-06-08. You can view files and clone it, but cannot push or open issues or pull requests.
SolarAgent/lib/screens/notifications.dart
2024-03-23 23:41:45 +08:00

106 lines
3.3 KiB
Dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:solaragent/auth.dart';
import 'package:solaragent/models/notification.dart' as model;
import 'package:solaragent/models/pagination.dart';
import 'package:solaragent/widgets/notification.dart';
class NotificationScreen extends StatefulWidget {
const NotificationScreen({super.key});
@override
State<NotificationScreen> createState() => _NotificationScreenState();
}
class _NotificationScreenState extends State<NotificationScreen> {
List<dynamic> notifications = List.empty();
@override
void initState() {
super.initState();
pullNotifications();
}
Future<void> pullNotifications() async {
if (await authClient.isAuthorized()) {
var uri =
Uri.parse('https://id.solsynth.dev/api/notifications?skip=0&take=25');
var res = await authClient.client!.get(uri);
if (res.statusCode == 200) {
final result =
PaginationResult.fromJson(jsonDecode(utf8.decode(res.bodyBytes)));
setState(() {
notifications =
result.data?.map((x) => model.Notification.fromJson(x)).toList() ??
List.empty();
});
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: RefreshIndicator(
onRefresh: pullNotifications,
child: CustomScrollView(
slivers: [
// Title
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.only(left: 10, right: 10, top: 20),
child: ListTile(
title: Text(
'Notifications',
style: Theme.of(context).textTheme.headlineSmall,
),
),
),
),
// Content
notifications.isEmpty
? SliverToBoxAdapter(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 10),
color: Colors.grey[50],
child: const ListTile(
leading: Icon(Icons.check),
title: Text('You\'re done!'),
subtitle: Text(
'There are no notifications unread for you.',
),
),
),
)
: SliverList.builder(
itemCount: notifications.length,
itemBuilder: (BuildContext context, int index) {
var element = notifications[index];
return NotificationItem(
index: index,
item: element,
onDismiss: () => setState(() {
notifications.removeAt(index);
}),
);
},
),
// Tips
SliverToBoxAdapter(
child: Container(
padding: const EdgeInsets.only(top: 12),
child: Text(
"Pull to refresh, swipe to dismiss",
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.bodySmall,
),
),
),
],
),
),
);
}
}