|
| 1 | +import 'package:expansion_tile_card/expansion_tile_card.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 4 | +import 'package:optimize_battery/optimize_battery.dart'; |
| 5 | +import 'package:flood_mobile/Blocs/power_management_bloc/power_management_bloc.dart'; |
| 6 | +import 'package:flood_mobile/Blocs/theme_bloc/theme_bloc.dart'; |
| 7 | +import 'package:flood_mobile/Pages/widgets/text_size.dart'; |
| 8 | +import 'package:flood_mobile/l10n/l10n.dart'; |
| 9 | + |
| 10 | +// ignore: must_be_immutable |
| 11 | +class PowerManagementSection extends StatelessWidget { |
| 12 | + final int themeIndex; |
| 13 | + |
| 14 | + PowerManagementSection({ |
| 15 | + Key? key, |
| 16 | + required this.themeIndex, |
| 17 | + }) : super(key: key); |
| 18 | + |
| 19 | + @override |
| 20 | + Widget build(BuildContext context) { |
| 21 | + final AppLocalizations l10n = context.l10n; |
| 22 | + return ExpansionTileCard( |
| 23 | + key: Key('Power Management Expansion Card'), |
| 24 | + initiallyExpanded: false, |
| 25 | + onExpansionChanged: (value) {}, |
| 26 | + elevation: 0, |
| 27 | + expandedColor: ThemeBloc.theme(themeIndex).primaryColor, |
| 28 | + baseColor: ThemeBloc.theme(themeIndex).primaryColor, |
| 29 | + title: MText(text: l10n.settings_tabs_power_management), |
| 30 | + leading: Icon(Icons.power_settings_new), |
| 31 | + contentPadding: EdgeInsets.all(0), |
| 32 | + expandedTextColor: ThemeBloc.theme(themeIndex).colorScheme.secondary, |
| 33 | + children: [ |
| 34 | + BlocBuilder<PowerManagementBloc, PowerManagementState>( |
| 35 | + builder: (context, state) { |
| 36 | + double batteryLevel = state.batteryLimitLevel.toDouble(); |
| 37 | + return Column( |
| 38 | + key: Key('Power management options display column'), |
| 39 | + children: [ |
| 40 | + SwitchListTile( |
| 41 | + title: Text(l10n.wifi_only_title), |
| 42 | + subtitle: Text( |
| 43 | + l10n.wifi_only_subtitle, |
| 44 | + style: TextStyle(fontSize: 13), |
| 45 | + ), |
| 46 | + value: state.wifiOnlyDownload, |
| 47 | + onChanged: (newValue) { |
| 48 | + BlocProvider.of<PowerManagementBloc>(context).add( |
| 49 | + SetWifiOnlyDownloadEvent(wifiOnlyDownload: newValue), |
| 50 | + ); |
| 51 | + }, |
| 52 | + shape: RoundedRectangleBorder( |
| 53 | + borderRadius: BorderRadius.circular(8.0), |
| 54 | + ), |
| 55 | + contentPadding: |
| 56 | + EdgeInsets.symmetric(horizontal: 10, vertical: 5), |
| 57 | + ), |
| 58 | + SwitchListTile( |
| 59 | + title: Text(l10n.shutdown_when_download_completes_title), |
| 60 | + subtitle: Text( |
| 61 | + l10n.shutdown_when_download_completes_subtitle, |
| 62 | + style: TextStyle(fontSize: 13), |
| 63 | + ), |
| 64 | + value: state.shutDownWhenFinishDownload, |
| 65 | + onChanged: (newValue) { |
| 66 | + BlocProvider.of<PowerManagementBloc>(context).add( |
| 67 | + SetShutDownWhenFinishDownloadEvent( |
| 68 | + shutDownWhenFinishDownload: newValue), |
| 69 | + ); |
| 70 | + }, |
| 71 | + shape: RoundedRectangleBorder( |
| 72 | + borderRadius: BorderRadius.circular(8.0), |
| 73 | + ), |
| 74 | + contentPadding: |
| 75 | + EdgeInsets.symmetric(horizontal: 10, vertical: 5), |
| 76 | + ), |
| 77 | + SwitchListTile( |
| 78 | + title: Text(l10n.keep_CPU_awake_title), |
| 79 | + subtitle: Text( |
| 80 | + l10n.keep_CPU_awake_subtitle, |
| 81 | + style: TextStyle(fontSize: 13), |
| 82 | + ), |
| 83 | + value: state.keepCpuAwake, |
| 84 | + onChanged: (newValue) { |
| 85 | + BlocProvider.of<PowerManagementBloc>(context).add( |
| 86 | + SetKeepCpuAwakeEvent(keepCpuAwake: newValue), |
| 87 | + ); |
| 88 | + }, |
| 89 | + shape: RoundedRectangleBorder( |
| 90 | + borderRadius: BorderRadius.circular(8.0), |
| 91 | + ), |
| 92 | + contentPadding: |
| 93 | + EdgeInsets.symmetric(horizontal: 10, vertical: 5), |
| 94 | + ), |
| 95 | + SwitchListTile( |
| 96 | + title: Text(l10n.charging_only_title), |
| 97 | + subtitle: Text( |
| 98 | + l10n.charging_only_subtitle, |
| 99 | + style: TextStyle(fontSize: 13), |
| 100 | + ), |
| 101 | + value: state.downloadChargingConnected, |
| 102 | + onChanged: (newValue) { |
| 103 | + BlocProvider.of<PowerManagementBloc>(context).add( |
| 104 | + SetDownloadChargingConnectedEvent( |
| 105 | + downloadChargingConnected: newValue), |
| 106 | + ); |
| 107 | + }, |
| 108 | + shape: RoundedRectangleBorder( |
| 109 | + borderRadius: BorderRadius.circular(8.0), |
| 110 | + ), |
| 111 | + contentPadding: |
| 112 | + EdgeInsets.symmetric(horizontal: 10, vertical: 5), |
| 113 | + ), |
| 114 | + SwitchListTile( |
| 115 | + title: Text(l10n.shut_down_wifi_title), |
| 116 | + subtitle: Text( |
| 117 | + l10n.shut_down_wifi_subtitle, |
| 118 | + style: TextStyle(fontSize: 13), |
| 119 | + ), |
| 120 | + value: state.shutDownWifi, |
| 121 | + onChanged: (newValue) { |
| 122 | + BlocProvider.of<PowerManagementBloc>(context).add( |
| 123 | + SetShutDownWifiEvent(shutDownWifi: newValue), |
| 124 | + ); |
| 125 | + }, |
| 126 | + shape: RoundedRectangleBorder( |
| 127 | + borderRadius: BorderRadius.circular(8.0), |
| 128 | + ), |
| 129 | + contentPadding: |
| 130 | + EdgeInsets.symmetric(horizontal: 10, vertical: 5), |
| 131 | + ), |
| 132 | + ListTile( |
| 133 | + title: Text( |
| 134 | + l10n.battery_optimization_title, |
| 135 | + ), |
| 136 | + subtitle: Text( |
| 137 | + l10n.battery_optimization_subtitle, |
| 138 | + style: TextStyle(fontSize: 13), |
| 139 | + ), |
| 140 | + onTap: () { |
| 141 | + OptimizeBattery.openBatteryOptimizationSettings(); |
| 142 | + }, |
| 143 | + shape: RoundedRectangleBorder( |
| 144 | + borderRadius: BorderRadius.circular(8.0), |
| 145 | + ), |
| 146 | + contentPadding: |
| 147 | + EdgeInsets.symmetric(horizontal: 10, vertical: 5), |
| 148 | + ), |
| 149 | + StatefulBuilder( |
| 150 | + builder: (BuildContext context, StateSetter setState) { |
| 151 | + return ListTile( |
| 152 | + title: Row( |
| 153 | + mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 154 | + children: [ |
| 155 | + Text( |
| 156 | + l10n.battery_low_title, |
| 157 | + ), |
| 158 | + Text(batteryLevel.toStringAsFixed(0) + ' %'), |
| 159 | + ], |
| 160 | + ), |
| 161 | + subtitle: SliderTheme( |
| 162 | + data: SliderThemeData(trackShape: CustomTrackShape()), |
| 163 | + child: Slider( |
| 164 | + value: batteryLevel, |
| 165 | + min: 0.0, |
| 166 | + max: 100.0, |
| 167 | + label: batteryLevel.round().toString() + '%', |
| 168 | + onChanged: (value) { |
| 169 | + setState(() { |
| 170 | + batteryLevel = value; |
| 171 | + }); |
| 172 | + }, |
| 173 | + onChangeEnd: (value) { |
| 174 | + BlocProvider.of<PowerManagementBloc>(context).add( |
| 175 | + SetBatteryLimitLevelEvent( |
| 176 | + batteryLimitLevel: value.toInt()), |
| 177 | + ); |
| 178 | + }, |
| 179 | + ), |
| 180 | + ), |
| 181 | + shape: RoundedRectangleBorder( |
| 182 | + borderRadius: BorderRadius.circular(8.0), |
| 183 | + ), |
| 184 | + contentPadding: |
| 185 | + EdgeInsets.symmetric(horizontal: 10, vertical: 5), |
| 186 | + ); |
| 187 | + }), |
| 188 | + ], |
| 189 | + ); |
| 190 | + }, |
| 191 | + ) |
| 192 | + ], |
| 193 | + ); |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +class CustomTrackShape extends RoundedRectSliderTrackShape { |
| 198 | + @override |
| 199 | + Rect getPreferredRect({ |
| 200 | + required RenderBox parentBox, |
| 201 | + Offset offset = Offset.zero, |
| 202 | + required SliderThemeData sliderTheme, |
| 203 | + bool isEnabled = false, |
| 204 | + bool isDiscrete = false, |
| 205 | + }) { |
| 206 | + final trackHeight = sliderTheme.trackHeight; |
| 207 | + final trackLeft = offset.dx; |
| 208 | + final trackTop = offset.dy + (parentBox.size.height - trackHeight!) / 2; |
| 209 | + final trackWidth = parentBox.size.width; |
| 210 | + return Rect.fromLTWH(trackLeft, trackTop, trackWidth, trackHeight); |
| 211 | + } |
| 212 | +} |
0 commit comments