|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter/services.dart'; |
| 3 | +import 'package:provider/provider.dart'; |
| 4 | +import 'package:pslab/providers/dust_sensor_config_provider.dart'; |
| 5 | +import 'package:pslab/view/widgets/config_widgets.dart'; |
| 6 | +import '../l10n/app_localizations.dart'; |
| 7 | +import '../providers/locator.dart'; |
| 8 | +import '../theme/colors.dart'; |
| 9 | + |
| 10 | +class DustSensorConfigScreen extends StatefulWidget { |
| 11 | + const DustSensorConfigScreen({super.key}); |
| 12 | + @override |
| 13 | + State<DustSensorConfigScreen> createState() => _DustSensorConfigScreenState(); |
| 14 | +} |
| 15 | + |
| 16 | +class _DustSensorConfigScreenState extends State<DustSensorConfigScreen> { |
| 17 | + AppLocalizations appLocalizations = getIt.get<AppLocalizations>(); |
| 18 | + final TextEditingController _updatePeriodController = TextEditingController(); |
| 19 | + final TextEditingController _highLimitController = TextEditingController(); |
| 20 | + |
| 21 | + @override |
| 22 | + void dispose() { |
| 23 | + _updatePeriodController.dispose(); |
| 24 | + _highLimitController.dispose(); |
| 25 | + super.dispose(); |
| 26 | + } |
| 27 | + |
| 28 | + @override |
| 29 | + Widget build(BuildContext context) { |
| 30 | + return Scaffold( |
| 31 | + backgroundColor: Theme.of(context).colorScheme.surface, |
| 32 | + resizeToAvoidBottomInset: true, |
| 33 | + appBar: AppBar( |
| 34 | + systemOverlayStyle: SystemUiOverlayStyle( |
| 35 | + statusBarColor: appBarColor, |
| 36 | + statusBarIconBrightness: Brightness.light, |
| 37 | + statusBarBrightness: Brightness.dark, |
| 38 | + ), |
| 39 | + leading: Builder(builder: (context) { |
| 40 | + return IconButton( |
| 41 | + onPressed: () { |
| 42 | + Navigator.maybePop(context); |
| 43 | + }, |
| 44 | + icon: Icon( |
| 45 | + Icons.arrow_back, |
| 46 | + color: appBarContentColor, |
| 47 | + ), |
| 48 | + ); |
| 49 | + }), |
| 50 | + backgroundColor: primaryRed, |
| 51 | + title: Text( |
| 52 | + appLocalizations.dustSensorConfig, |
| 53 | + style: TextStyle( |
| 54 | + color: appBarContentColor, |
| 55 | + fontSize: 15, |
| 56 | + ), |
| 57 | + ), |
| 58 | + ), |
| 59 | + body: SafeArea( |
| 60 | + child: Padding( |
| 61 | + padding: const EdgeInsets.symmetric(horizontal: 16.0), |
| 62 | + child: Consumer<DustSensorConfigProvider>( |
| 63 | + builder: (context, provider, child) { |
| 64 | + _updatePeriodController.text = |
| 65 | + provider.config.updatePeriod.toString(); |
| 66 | + _highLimitController.text = provider.config.highLimit.toString(); |
| 67 | + |
| 68 | + return SingleChildScrollView( |
| 69 | + child: Column( |
| 70 | + children: [ |
| 71 | + ConfigInputItem( |
| 72 | + title: appLocalizations.updatePeriod, |
| 73 | + value: |
| 74 | + "${provider.config.updatePeriod} ${appLocalizations.ms}", |
| 75 | + controller: _updatePeriodController, |
| 76 | + hint: appLocalizations.dustUpdatePeriodHint, |
| 77 | + onChanged: (value) { |
| 78 | + try { |
| 79 | + int updatePeriod = int.parse(value); |
| 80 | + if (updatePeriod > 1000 || updatePeriod < 100) { |
| 81 | + throw const FormatException(); |
| 82 | + } |
| 83 | + provider.updateUpdatePeriod(updatePeriod); |
| 84 | + } catch (e) { |
| 85 | + ScaffoldMessenger.of(context).showSnackBar(SnackBar( |
| 86 | + content: Text( |
| 87 | + appLocalizations.updatePeriodErrorMessage))); |
| 88 | + } |
| 89 | + }, |
| 90 | + ), |
| 91 | + ConfigInputItem( |
| 92 | + title: appLocalizations.highLimit, |
| 93 | + value: |
| 94 | + "${provider.config.highLimit} ${appLocalizations.ppm}", |
| 95 | + controller: _highLimitController, |
| 96 | + hint: appLocalizations.dustHighLimitHint, |
| 97 | + onChanged: (value) { |
| 98 | + try { |
| 99 | + double highLimit = double.parse(value); |
| 100 | + if (highLimit > 5.0 || highLimit < 0.0) { |
| 101 | + throw const FormatException(); |
| 102 | + } |
| 103 | + provider.updateHighLimit(highLimit); |
| 104 | + } catch (e) { |
| 105 | + ScaffoldMessenger.of(context).showSnackBar(SnackBar( |
| 106 | + content: Text( |
| 107 | + appLocalizations.highLimitErrorMessage))); |
| 108 | + } |
| 109 | + }, |
| 110 | + ), |
| 111 | + ConfigDropdownItem( |
| 112 | + title: appLocalizations.activeSensor, |
| 113 | + selectedValue: provider.config.activeSensor, |
| 114 | + options: [ |
| 115 | + ConfigOption( |
| 116 | + value: 'In-built Sensor', |
| 117 | + displayName: appLocalizations.inBuiltSensor), |
| 118 | + const ConfigOption( |
| 119 | + value: 'SDS011', displayName: 'SDS011'), |
| 120 | + ], |
| 121 | + onChanged: (value) { |
| 122 | + provider.updateActiveSensor(value); |
| 123 | + }, |
| 124 | + ), |
| 125 | + ConfigCheckboxItem( |
| 126 | + title: appLocalizations.locationData, |
| 127 | + subtitle: appLocalizations.locationDataHint, |
| 128 | + value: provider.config.includeLocationData, |
| 129 | + onChanged: (value) { |
| 130 | + provider.updateIncludeLocationData(value); |
| 131 | + }, |
| 132 | + ), |
| 133 | + ], |
| 134 | + ), |
| 135 | + ); |
| 136 | + }, |
| 137 | + ), |
| 138 | + ), |
| 139 | + ), |
| 140 | + ); |
| 141 | + } |
| 142 | +} |
0 commit comments