|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter/services.dart'; |
| 3 | +import 'package:provider/provider.dart'; |
| 4 | +import 'package:pslab/constants.dart'; |
| 5 | +import 'package:pslab/providers/luxmeter_config_provider.dart'; |
| 6 | +import 'package:pslab/view/widgets/config_widgets.dart'; |
| 7 | + |
| 8 | +import '../theme/colors.dart'; |
| 9 | + |
| 10 | +class LuxMeterConfigScreen extends StatefulWidget { |
| 11 | + const LuxMeterConfigScreen({super.key}); |
| 12 | + |
| 13 | + @override |
| 14 | + State<LuxMeterConfigScreen> createState() => _LuxMeterConfigScreenState(); |
| 15 | +} |
| 16 | + |
| 17 | +class _LuxMeterConfigScreenState extends State<LuxMeterConfigScreen> { |
| 18 | + final TextEditingController _updatePeriodController = TextEditingController(); |
| 19 | + final TextEditingController _highLimitController = TextEditingController(); |
| 20 | + final TextEditingController _sensorGainController = TextEditingController(); |
| 21 | + |
| 22 | + @override |
| 23 | + void initState() { |
| 24 | + super.initState(); |
| 25 | + WidgetsBinding.instance.addPostFrameCallback((_) { |
| 26 | + final provider = |
| 27 | + Provider.of<LuxMeterConfigProvider>(context, listen: false); |
| 28 | + _updatePeriodController.text = provider.config.updatePeriod.toString(); |
| 29 | + _highLimitController.text = provider.config.highLimit.toString(); |
| 30 | + _sensorGainController.text = provider.config.sensorGain.toString(); |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + @override |
| 35 | + void dispose() { |
| 36 | + _updatePeriodController.dispose(); |
| 37 | + _highLimitController.dispose(); |
| 38 | + _sensorGainController.dispose(); |
| 39 | + super.dispose(); |
| 40 | + } |
| 41 | + |
| 42 | + @override |
| 43 | + Widget build(BuildContext context) { |
| 44 | + return Scaffold( |
| 45 | + backgroundColor: Theme.of(context).colorScheme.surface, |
| 46 | + resizeToAvoidBottomInset: true, |
| 47 | + appBar: AppBar( |
| 48 | + systemOverlayStyle: SystemUiOverlayStyle(statusBarColor: appBarColor), |
| 49 | + leading: Builder(builder: (context) { |
| 50 | + return IconButton( |
| 51 | + onPressed: () { |
| 52 | + if (Navigator.canPop(context) && |
| 53 | + ModalRoute.of(context)?.settings.name == '/luxmeter') { |
| 54 | + Navigator.popUntil(context, ModalRoute.withName('/luxmeter')); |
| 55 | + } else { |
| 56 | + Navigator.pushNamedAndRemoveUntil( |
| 57 | + context, |
| 58 | + '/luxmeter', |
| 59 | + (route) => route.isFirst, |
| 60 | + ); |
| 61 | + } |
| 62 | + }, |
| 63 | + icon: Icon( |
| 64 | + Icons.arrow_back, |
| 65 | + color: appBarContentColor, |
| 66 | + ), |
| 67 | + ); |
| 68 | + }), |
| 69 | + backgroundColor: primaryRed, |
| 70 | + title: Text( |
| 71 | + luxmeterConfigurations, |
| 72 | + style: TextStyle( |
| 73 | + color: appBarContentColor, |
| 74 | + fontSize: 15, |
| 75 | + ), |
| 76 | + ), |
| 77 | + ), |
| 78 | + body: SafeArea( |
| 79 | + child: Padding( |
| 80 | + padding: const EdgeInsets.symmetric(horizontal: 16.0), |
| 81 | + child: Consumer<LuxMeterConfigProvider>( |
| 82 | + builder: (context, provider, child) { |
| 83 | + return SingleChildScrollView( |
| 84 | + child: Column( |
| 85 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 86 | + children: [ |
| 87 | + ConfigInputItem( |
| 88 | + title: updatePeriod, |
| 89 | + value: '${provider.config.updatePeriod} $ms', |
| 90 | + controller: _updatePeriodController, |
| 91 | + onChanged: (value) { |
| 92 | + final intValue = int.tryParse(value); |
| 93 | + if (intValue != null && |
| 94 | + intValue >= 100 && |
| 95 | + intValue <= 1000) { |
| 96 | + provider.updateUpdatePeriod(intValue); |
| 97 | + } else { |
| 98 | + ScaffoldMessenger.of(context).showSnackBar( |
| 99 | + SnackBar( |
| 100 | + content: Text( |
| 101 | + updatePeriodErrorMessage, |
| 102 | + style: TextStyle(color: snackBarContentColor), |
| 103 | + ), |
| 104 | + backgroundColor: snackBarBackgroundColor), |
| 105 | + ); |
| 106 | + } |
| 107 | + }, |
| 108 | + hint: updatePeriodHint, |
| 109 | + ), |
| 110 | + ConfigInputItem( |
| 111 | + title: highLimit, |
| 112 | + value: '${provider.config.highLimit} $lx', |
| 113 | + controller: _highLimitController, |
| 114 | + onChanged: (value) { |
| 115 | + final intValue = int.tryParse(value); |
| 116 | + if (intValue != null && |
| 117 | + intValue >= 10 && |
| 118 | + intValue <= 10000) { |
| 119 | + provider.updateHighLimit(intValue); |
| 120 | + } else { |
| 121 | + ScaffoldMessenger.of(context).showSnackBar( |
| 122 | + SnackBar( |
| 123 | + content: Text( |
| 124 | + highLimitErrorMessage, |
| 125 | + style: TextStyle(color: snackBarContentColor), |
| 126 | + ), |
| 127 | + backgroundColor: snackBarBackgroundColor), |
| 128 | + ); |
| 129 | + } |
| 130 | + }, |
| 131 | + hint: highLimitHint, |
| 132 | + ), |
| 133 | + ConfigDropdownItem( |
| 134 | + title: activeSensor, |
| 135 | + selectedValue: provider.config.activeSensor, |
| 136 | + options: [ |
| 137 | + ConfigOption( |
| 138 | + value: 'In-built Sensor', |
| 139 | + displayName: inBuiltSensor), |
| 140 | + ConfigOption(value: 'BH1750', displayName: 'BH1750'), |
| 141 | + ConfigOption(value: 'TSL2561', displayName: 'TSL2561'), |
| 142 | + ], |
| 143 | + onChanged: (value) { |
| 144 | + provider.updateActiveSensor(value); |
| 145 | + }, |
| 146 | + ), |
| 147 | + ConfigInputItem( |
| 148 | + title: sensorGain, |
| 149 | + value: provider.config.sensorGain.toString(), |
| 150 | + controller: _sensorGainController, |
| 151 | + onChanged: (value) { |
| 152 | + final intValue = int.tryParse(value); |
| 153 | + if (intValue != null) { |
| 154 | + provider.updateSensorGain(intValue); |
| 155 | + } |
| 156 | + }, |
| 157 | + hint: sensorGainHint, |
| 158 | + ), |
| 159 | + ConfigCheckboxItem( |
| 160 | + title: locationData, |
| 161 | + subtitle: locationDataHint, |
| 162 | + value: provider.config.includeLocationData, |
| 163 | + onChanged: (value) { |
| 164 | + provider.updateIncludeLocationData(value); |
| 165 | + }, |
| 166 | + ), |
| 167 | + ], |
| 168 | + ), |
| 169 | + ); |
| 170 | + }, |
| 171 | + ), |
| 172 | + ), |
| 173 | + ), |
| 174 | + ); |
| 175 | + } |
| 176 | +} |
0 commit comments