Skip to content

Commit 8705262

Browse files
committed
Improve widget handling
1 parent 0f07ffb commit 8705262

File tree

5 files changed

+113
-108
lines changed

5 files changed

+113
-108
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:stelaris/util/formatter/min_value_fomatter.dart';
3+
4+
class BaseIntegerField extends StatefulWidget {
5+
final String label;
6+
final int initialValue;
7+
final int minValue;
8+
final ValueChanged<int> onChanged;
9+
10+
const BaseIntegerField({
11+
required this.label,
12+
required this.initialValue,
13+
required this.minValue,
14+
required this.onChanged,
15+
super.key,
16+
});
17+
18+
@override
19+
State<BaseIntegerField> createState() => _BaseIntegerFieldState();
20+
}
21+
22+
class _BaseIntegerFieldState extends State<BaseIntegerField> {
23+
late final TextEditingController _controller;
24+
25+
@override
26+
void initState() {
27+
super.initState();
28+
_controller = TextEditingController(text: widget.initialValue.toString());
29+
}
30+
31+
@override
32+
void dispose() {
33+
_controller.dispose();
34+
super.dispose();
35+
}
36+
37+
@override
38+
Widget build(BuildContext context) {
39+
return TextFormField(
40+
controller: _controller,
41+
keyboardType: TextInputType.number,
42+
decoration: InputDecoration(
43+
labelText: widget.label,
44+
border: const OutlineInputBorder(),
45+
),
46+
inputFormatters: [
47+
MinValueFormatter(widget.minValue),
48+
],
49+
validator: (v) {
50+
if (v == null || v.isEmpty) return 'Enter a ${widget.label}';
51+
final val = int.tryParse(v);
52+
if (val == null) return 'Enter a valid integer';
53+
return null;
54+
},
55+
onChanged: (v) {
56+
final parsed = int.tryParse(v);
57+
if (parsed != null) widget.onChanged(parsed);
58+
},
59+
);
60+
}
61+
}

lib/feature/sound/modal/section/integer_fields_section.dart

Lines changed: 0 additions & 97 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:stelaris/feature/sound/modal/section/base_section.dart';
3+
import 'package:stelaris/feature/sound/modal/section/base_integer_section_field.dart';
4+
5+
class IntegerFieldsSection extends StatelessWidget {
6+
final int weight;
7+
final int attenuation;
8+
final int minValue;
9+
final ValueChanged<int> onWeightChanged;
10+
final ValueChanged<int> onAttenuationChanged;
11+
12+
const IntegerFieldsSection({
13+
required this.weight,
14+
required this.attenuation,
15+
required this.onWeightChanged,
16+
required this.onAttenuationChanged,
17+
this.minValue = 1,
18+
super.key,
19+
});
20+
21+
@override
22+
Widget build(BuildContext context) {
23+
return BaseSection(
24+
child: Column(
25+
children: [
26+
BaseIntegerField(
27+
label: 'Weight',
28+
initialValue: weight,
29+
minValue: minValue,
30+
onChanged: onWeightChanged,
31+
),
32+
const SizedBox(height: 12),
33+
BaseIntegerField(
34+
label: 'Attenuation Distance',
35+
initialValue: attenuation,
36+
minValue: minValue,
37+
onChanged: onAttenuationChanged,
38+
),
39+
],
40+
),
41+
);
42+
}
43+
}

lib/feature/sound/modal/section/sound_switch_section.dart renamed to lib/feature/sound/modal/type/sound_switch_section.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class SwitchesSection extends StatelessWidget {
2020
@override
2121
Widget build(BuildContext context) {
2222
return BaseSection(
23-
child: Column(
23+
child: Row(
24+
mainAxisSize: MainAxisSize.min,
2425
children: [
2526
_buildSwitchRow(
2627
context: context,
@@ -47,12 +48,11 @@ class SwitchesSection extends StatelessWidget {
4748
required ValueChanged<bool> onChanged,
4849
}) {
4950
return Row(
51+
mainAxisSize: MainAxisSize.min,
5052
children: [
51-
Expanded(
52-
child: Text(
53-
label,
54-
style: Theme.of(context).textTheme.bodyLarge,
55-
),
53+
Text(
54+
label,
55+
style: Theme.of(context).textTheme.bodyLarge,
5656
),
5757
Switch(
5858
value: value,

lib/feature/sound/modal/section/volume_section.dart renamed to lib/feature/sound/modal/type/volume_section.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ class _VolumeSectionState extends State<VolumeSection> {
5656
_currentVolume = newVolume;
5757
});
5858
},
59-
onChangeEnd:
60-
widget.onVolumeFinalized, // Pass through the finalized callback
59+
onChangeEnd: widget.onVolumeFinalized,
6160
),
62-
const SizedBox(height: 12), // Your existing spacer
61+
const SizedBox(height: 12),
6362
SoundSliderRow(
6463
label: 'Pitch',
6564
value: _currentPitch,
@@ -68,8 +67,7 @@ class _VolumeSectionState extends State<VolumeSection> {
6867
_currentPitch = newPitch;
6968
});
7069
},
71-
onChangeEnd:
72-
widget.onPitchFinalized, // Pass through the finalized callback
70+
onChangeEnd: widget.onPitchFinalized,
7371
),
7472
],
7573
),

0 commit comments

Comments
 (0)