import 'package:flutter/material.dart'; import 'package:flutter_broadcast_receiver/flutter_broadcast_receiver.dart'; import 'package:isar/isar.dart'; import 'package:stadtbote/content_views/assigned_jobs_page.dart'; import 'package:stadtbote/content_views/finished_jobs_page.dart'; import 'package:stadtbote/content_views/messages_page.dart'; import 'package:stadtbote/content_views/utils/content_view_widget.dart'; import 'package:stadtbote/content_views/utils/header_info.dart'; import 'package:stadtbote/model/job.dart'; import 'package:stadtbote/model/status.dart'; import 'package:stadtbote/popups/infos_popup.dart'; import 'package:stadtbote/tools/session_info_update.dart'; import 'package:badges/badges.dart' as badges; import '../main.dart'; import '../tools/constants.dart'; import '../tools/util.dart'; class MainPage extends ContentViewWidget { final ValueNotifier _zipValueNotifier = ValueNotifier("----"); final ValueNotifier _sidValueNotifier = ValueNotifier("----"); MainPage({super.key}) { Status? status = MyApp.isarInstance.status.where().findFirstSync(); if (status != null) { _zipValueNotifier.value = status.locZip; _sidValueNotifier.value = status.sid; } } @override State createState() => _MainPageState(); @override Future getHeaderInfo(BuildContext context) async { return HeaderInfo( titleWidget: Container( height: 100, padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 0), child: Column(children: [ ValueListenableBuilder( valueListenable: _zipValueNotifier, builder: (context, value, child) { return Text( value, style: const TextStyle( color: Colors.white, fontSize: 25.0, ), ); }, ), const SizedBox(height: 5), ValueListenableBuilder( valueListenable: _sidValueNotifier, builder: (context, value, child) { return Text( value, style: const TextStyle( color: Colors.white, fontSize: 25.0, ), ); }, ), ]), ), rightButtonEnabled: true, rightButtonVisible: true, rightButtonImage: Util.fetchFutureIcon(const Icon(Icons.info_outline, size: 20.0, color: Colors.white)), rightButtonOnPressed: () async { showInfosPopup(context); }); } } class _MainPageState extends State { final ValueNotifier _assignedJobsValueNotifier = ValueNotifier(0); final ValueNotifier _acceptedJobsValueNotifier = ValueNotifier(0); final ValueNotifier _messagesValueNotifier = ValueNotifier(0); @override void initState() { super.initState(); _initBadges(); _initMessages(); } @override Widget build(BuildContext context) { return Stack( children: [ Padding( padding: const EdgeInsets.all(16.0), child: Column( mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ ValueListenableBuilder( valueListenable: _assignedJobsValueNotifier, builder: (context, value, child) { return badges.Badge( showBadge: value > 0, position: badges.BadgePosition.topEnd(top: 0, end: -5), badgeContent: Text( value.toString(), style: const TextStyle( color: Colors.white, fontSize: 13.0, ), ), badgeStyle: badges.BadgeStyle( badgeColor: Colors.red, padding: const EdgeInsets.all(10), borderRadius: BorderRadius.circular(4), borderSide: const BorderSide(color: Colors.white, width: 0), elevation: 0, ), child: getDefaultButton( 'Zugewiesene Aufträge', image: Image.asset('assets/zugewiesene_auftraege.png', width: 30, height: 30), () => {BroadcastReceiver().publish(Constants.messageTopicPushContentView, arguments: const AssignedJobsPage())}) ); }, ), const SizedBox(height: 10), ValueListenableBuilder( valueListenable: _acceptedJobsValueNotifier, builder: (context, value, child) { return badges.Badge( showBadge: value > 0, position: badges.BadgePosition.topEnd(top: 0, end: -5), badgeContent: Text( value.toString(), style: const TextStyle( color: Colors.white, fontSize: 13.0, ), ), badgeStyle: badges.BadgeStyle( badgeColor: Colors.red, padding: const EdgeInsets.all(10), borderRadius: BorderRadius.circular(4), borderSide: const BorderSide(color: Colors.white, width: 0), elevation: 0, ), child: getDefaultButton( 'Angenommene Aufträge', image: Image.asset('assets/angenommene_auftraege.png', width: 30, height: 30), () => {BroadcastReceiver().publish(Constants.messageTopicPushContentView, arguments: const AssignedJobsPage())}) ); }, ), const SizedBox(height: 10), getDefaultButton( 'Erledigte Aufträge', image: Image.asset('assets/erledigte_auftraege.png', width: 30, height: 30), () => {BroadcastReceiver().publish(Constants.messageTopicPushContentView, arguments: const FinishedJobsPage())}), const SizedBox(height: 10), ValueListenableBuilder( valueListenable: _messagesValueNotifier, builder: (context, value, child) { return badges.Badge( showBadge: value > 0, position: badges.BadgePosition.topEnd(top: 0, end: -5), badgeContent: Text( value.toString(), style: const TextStyle( color: Colors.white, fontSize: 13.0, ), ), badgeStyle: badges.BadgeStyle( badgeColor: Colors.red, padding: const EdgeInsets.all(10), borderRadius: BorderRadius.circular(4), borderSide: const BorderSide(color: Colors.white, width: 0), elevation: 0, ), child: getDefaultButton( 'Nachrichten', image: Image.asset('assets/nachrichten.png', width: 30, height: 30), () => {BroadcastReceiver().publish(Constants.messageTopicPushContentView, arguments: const MessagesPage())}) ); }, ), const SizedBox(height: 10), getDefaultButton( 'Anfahrtsbereich', image: Image.asset('assets/anfahrtsbereich.png', width: 30, height: 30), () => { //BroadcastReceiver().publish(Constants.messageTopicPushContentView, arguments: const FinishedJobsPage()) }), const SizedBox(height: 10), getDefaultButton( 'Abmelden', image: Image.asset('assets/abmelden.png', width: 30, height: 30), () => {BroadcastReceiver().publish(Constants.messageTopicPushContentView, arguments: const FinishedJobsPage())}), Expanded( child: Container( decoration: BoxDecoration( color: Colors.transparent, border: Border.all( color: Colors.transparent, width: 2.0, ), ), ), ), Image.asset( 'assets/sb_logo.png', width: 100, height: 100, ), ], ), ), ], ); } /*_test() async { var json = await rootBundle.loadString('assets/job.json'); var jsonMap = jsonDecode(json); var payload = jsonMap["payload"]; var jobsPayload = JobsPayload.fromJson(payload); if (jobsPayload.jobs != null) { MyApp.isarInstance.writeTxnSync(() { for (var job in jobsPayload.jobs as List) { MyApp.isarInstance.jobs.putSync(job); } }); } }*/ _initMessages() { BroadcastReceiver().subscribe(Constants.messageTopicStatusInfoUpdate, (statusInfoUpdate) { widget._sidValueNotifier.value = statusInfoUpdate.status.sid; }); } void _initBadges() async { _assignedJobsValueNotifier.value = await MyApp.isarInstance.jobs.filter().stateEqualTo(0).count(); _acceptedJobsValueNotifier.value = await MyApp.isarInstance.jobs.filter().stateEqualTo(1).count(); } }