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 createState() => _NotificationScreenState(); } class _NotificationScreenState extends State { List notifications = List.empty(); @override void initState() { super.initState(); pullNotifications(); } Future 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, ), ), ), ], ), ), ); } }