|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 3 | +import 'package:flutter_svg/flutter_svg.dart'; |
| 4 | +import 'package:go_router/go_router.dart'; |
| 5 | +import 'package:on_time_front/domain/entities/alarm_entities.dart'; |
| 6 | +import 'package:on_time_front/l10n/app_localizations.dart'; |
| 7 | +import 'package:on_time_front/presentation/app/cubit/alarm_gate_cubit.dart'; |
| 8 | +import 'package:on_time_front/presentation/shared/constants/app_colors.dart'; |
| 9 | + |
| 10 | +class AlarmAllowScreen extends StatefulWidget { |
| 11 | + const AlarmAllowScreen({super.key}); |
| 12 | + |
| 13 | + @override |
| 14 | + State<AlarmAllowScreen> createState() => _AlarmAllowScreenState(); |
| 15 | +} |
| 16 | + |
| 17 | +class _AlarmAllowScreenState extends State<AlarmAllowScreen> |
| 18 | + with WidgetsBindingObserver { |
| 19 | + bool _isRequesting = false; |
| 20 | + |
| 21 | + @override |
| 22 | + void initState() { |
| 23 | + super.initState(); |
| 24 | + WidgetsBinding.instance.addObserver(this); |
| 25 | + } |
| 26 | + |
| 27 | + @override |
| 28 | + void didChangeAppLifecycleState(AppLifecycleState state) { |
| 29 | + if (state != AppLifecycleState.resumed) return; |
| 30 | + context.read<AlarmGateCubit>().refreshPermission( |
| 31 | + disableAlarmsWhenPermissionMissing: true, |
| 32 | + enableAlarmsOnGrant: true, |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + @override |
| 37 | + void dispose() { |
| 38 | + WidgetsBinding.instance.removeObserver(this); |
| 39 | + super.dispose(); |
| 40 | + } |
| 41 | + |
| 42 | + Future<void> _requestPermission() async { |
| 43 | + setState(() { |
| 44 | + _isRequesting = true; |
| 45 | + }); |
| 46 | + final permission = await context.read<AlarmGateCubit>().requestPermission(); |
| 47 | + if (!mounted) return; |
| 48 | + setState(() { |
| 49 | + _isRequesting = false; |
| 50 | + }); |
| 51 | + if (permission == AlarmPermissionState.granted || |
| 52 | + permission == AlarmPermissionState.unsupported) { |
| 53 | + context.go('/home'); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + Future<void> _dismiss() async { |
| 58 | + await context.read<AlarmGateCubit>().dismissPrompt(); |
| 59 | + if (!mounted) return; |
| 60 | + context.go('/home'); |
| 61 | + } |
| 62 | + |
| 63 | + @override |
| 64 | + Widget build(BuildContext context) { |
| 65 | + return Scaffold( |
| 66 | + body: Padding( |
| 67 | + padding: const EdgeInsets.only(bottom: 72.0), |
| 68 | + child: Column( |
| 69 | + mainAxisSize: MainAxisSize.max, |
| 70 | + mainAxisAlignment: MainAxisAlignment.start, |
| 71 | + crossAxisAlignment: CrossAxisAlignment.center, |
| 72 | + spacing: 68.50, |
| 73 | + children: [ |
| 74 | + Expanded( |
| 75 | + child: Center( |
| 76 | + child: Column( |
| 77 | + mainAxisSize: MainAxisSize.min, |
| 78 | + mainAxisAlignment: MainAxisAlignment.start, |
| 79 | + crossAxisAlignment: CrossAxisAlignment.center, |
| 80 | + spacing: 40, |
| 81 | + children: const [_Image(), _Title()], |
| 82 | + ), |
| 83 | + ), |
| 84 | + ), |
| 85 | + _Buttons( |
| 86 | + isRequesting: _isRequesting, |
| 87 | + onAllow: _requestPermission, |
| 88 | + onDismiss: _dismiss, |
| 89 | + ), |
| 90 | + ], |
| 91 | + ), |
| 92 | + ), |
| 93 | + ); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +class _Buttons extends StatelessWidget { |
| 98 | + const _Buttons({ |
| 99 | + required this.isRequesting, |
| 100 | + required this.onAllow, |
| 101 | + required this.onDismiss, |
| 102 | + }); |
| 103 | + |
| 104 | + final bool isRequesting; |
| 105 | + final Future<void> Function() onAllow; |
| 106 | + final Future<void> Function() onDismiss; |
| 107 | + |
| 108 | + @override |
| 109 | + Widget build(BuildContext context) { |
| 110 | + final textTheme = Theme.of(context).textTheme; |
| 111 | + final colorScheme = Theme.of(context).colorScheme; |
| 112 | + return Column( |
| 113 | + mainAxisSize: MainAxisSize.min, |
| 114 | + mainAxisAlignment: MainAxisAlignment.start, |
| 115 | + crossAxisAlignment: CrossAxisAlignment.center, |
| 116 | + spacing: 24, |
| 117 | + children: [ |
| 118 | + FilledButton( |
| 119 | + onPressed: isRequesting ? null : () => onAllow(), |
| 120 | + child: Text( |
| 121 | + AppLocalizations.of(context)!.allowAlarms, |
| 122 | + textAlign: TextAlign.center, |
| 123 | + style: textTheme.titleMedium?.copyWith( |
| 124 | + color: colorScheme.onPrimary, |
| 125 | + ), |
| 126 | + ), |
| 127 | + ), |
| 128 | + GestureDetector( |
| 129 | + onTap: isRequesting ? null : () => onDismiss(), |
| 130 | + child: SizedBox( |
| 131 | + width: 358, |
| 132 | + child: Text( |
| 133 | + AppLocalizations.of(context)!.doItLater, |
| 134 | + textAlign: TextAlign.center, |
| 135 | + style: textTheme.bodyLarge?.copyWith( |
| 136 | + color: AppColors.grey[400], |
| 137 | + decoration: TextDecoration.underline, |
| 138 | + decorationColor: AppColors.grey[400], |
| 139 | + ), |
| 140 | + ), |
| 141 | + ), |
| 142 | + ), |
| 143 | + ], |
| 144 | + ); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +class _Title extends StatelessWidget { |
| 149 | + const _Title(); |
| 150 | + |
| 151 | + @override |
| 152 | + Widget build(BuildContext context) { |
| 153 | + final textTheme = Theme.of(context).textTheme; |
| 154 | + final colorScheme = Theme.of(context).colorScheme; |
| 155 | + return Column( |
| 156 | + mainAxisSize: MainAxisSize.min, |
| 157 | + mainAxisAlignment: MainAxisAlignment.start, |
| 158 | + crossAxisAlignment: CrossAxisAlignment.center, |
| 159 | + spacing: 12, |
| 160 | + children: [ |
| 161 | + Text( |
| 162 | + AppLocalizations.of(context)!.pleaseAllowAlarms, |
| 163 | + textAlign: TextAlign.center, |
| 164 | + style: textTheme.headlineMedium?.copyWith(color: colorScheme.primary), |
| 165 | + ), |
| 166 | + SizedBox( |
| 167 | + width: 282, |
| 168 | + child: Text( |
| 169 | + AppLocalizations.of(context)!.alarmPermissionDescription, |
| 170 | + textAlign: TextAlign.center, |
| 171 | + style: textTheme.titleMedium?.copyWith(color: colorScheme.outline), |
| 172 | + ), |
| 173 | + ), |
| 174 | + ], |
| 175 | + ); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +class _Image extends StatelessWidget { |
| 180 | + const _Image(); |
| 181 | + |
| 182 | + @override |
| 183 | + Widget build(BuildContext context) { |
| 184 | + final colorScheme = Theme.of(context).colorScheme; |
| 185 | + return Container( |
| 186 | + width: 70, |
| 187 | + height: 70, |
| 188 | + padding: const EdgeInsets.all(17.50), |
| 189 | + decoration: ShapeDecoration( |
| 190 | + color: colorScheme.primaryContainer, |
| 191 | + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(35)), |
| 192 | + ), |
| 193 | + child: SvgPicture.asset( |
| 194 | + 'bell-ringing.svg', |
| 195 | + package: 'assets', |
| 196 | + colorFilter: ColorFilter.mode(colorScheme.primary, BlendMode.srcIn), |
| 197 | + ), |
| 198 | + ); |
| 199 | + } |
| 200 | +} |
0 commit comments