Skip to content

Commit

Permalink
Refactor slider for better mobile use (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfiannaca authored Nov 9, 2023
1 parent ca8f3b9 commit df35316
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions lib/widgets/settings_widgets/maid_slider.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import 'package:flutter/material.dart';

class MaidSlider extends StatelessWidget{
class MaidSlider extends StatelessWidget {
final String labelText;
final num inputValue;
final double sliderMin;
final double sliderMax;
final int sliderDivisions;
final Function(double) onValueChanged;

const MaidSlider({super.key,
const MaidSlider({
super.key,
required this.labelText,
required this.inputValue,
required this.sliderMin,
Expand All @@ -20,6 +21,9 @@ class MaidSlider extends StatelessWidget{
@override
Widget build(BuildContext context) {
String labelValue;
TextEditingController textController = TextEditingController(
text: inputValue.toString(),
);

// I finput value is a double
if (inputValue is int) {
Expand All @@ -28,7 +32,7 @@ class MaidSlider extends StatelessWidget{
} else {
labelValue = inputValue.toStringAsFixed(3);
}

return ListTile(
title: Row(
children: [
Expand All @@ -37,7 +41,7 @@ class MaidSlider extends StatelessWidget{
child: Text(labelText),
),
Expanded(
flex: 7,
flex: 6,
child: Slider(
value: inputValue.toDouble(),
min: sliderMin,
Expand All @@ -50,11 +54,26 @@ class MaidSlider extends StatelessWidget{
),
),
Expanded(
flex: 1,
child: Text(labelValue),
flex: 2,
child: TextFormField(
textAlign: TextAlign.center,
keyboardType: TextInputType.number,
controller: textController,
onEditingComplete: () {
final parsedValue =
double.tryParse(textController.text) ?? sliderMin;
if (parsedValue < sliderMin) {
onValueChanged(sliderMin);
} else if (parsedValue > sliderMax) {
onValueChanged(sliderMax);
} else {
onValueChanged(parsedValue);
}
},
),
),
],
),
);
}
}
}

0 comments on commit df35316

Please sign in to comment.