Zeroing a slider’s default margin on flutter

Asked

Viewed 73 times

-1

How do I reset the margins of a slider in the flutter

Slider(
  value: valueHolder.toDouble(),
  min: 0,
  max: 100,
  divisions: 100,
  activeColor: Colors.green,
  inactiveColor: Colors.grey,
  label: '${valueHolder.round()}',
  onChanged: (double newValue) {
    setState(() {
      valueHolder = newValue.round();
    });
  },
  semanticFormatterCallback: (double newValue) {
    return '${newValue.round()}';
  }
),

inserir a descrição da imagem aqui

I remember that in conventional android I had to do something to remove the edges on a button similar now in flutter do not know how it should be done.

1 answer

1

You can create a custom theme for your Slider, follows an example:

class _MyHomePageState extends State<MyHomePage> {
  double _value = 0;

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body: Center(
        child: SliderTheme(
          data: SliderThemeData(
            trackShape: CustomTrackShape(),
          ),
          child: Slider(
            value: _value,
            onChanged: (double value) {
              setState(() {
                _value = value;
              });
            },
          ),
        ),
      ),
    );
  }
}

class CustomTrackShape extends RoundedRectSliderTrackShape {
  Rect getPreferredRect({
    @required RenderBox parentBox,
    Offset offset = Offset.zero,
    @required SliderThemeData sliderTheme,
    bool isEnabled = false,
    bool isDiscrete = false,
  }) {
    final double trackHeight = sliderTheme.trackHeight;
    final double trackLeft = offset.dx;
    final double trackTop = offset.dy + (parentBox.size.height - trackHeight) / 2;
    final double trackWidth = parentBox.size.width;
    return Rect.fromLTWH(trackLeft, trackTop, trackWidth, trackHeight);
  }
}

Source: Ability to create Slider without margins on Both Sides

Browser other questions tagged

You are not signed in. Login or sign up in order to post.