user1506104
user1506104

Reputation: 7106

How to customize my Slider widget's thumb color in flutter?

I want to change the color of the circular part of my widget. This color should be different from the activeColor and inactiveColor colors. This is my current code:

Slider(
  value: sliderValue.toDouble(),
  max: 100.0,
  min: 0.0,
  activeColor: Colors.red,
  inactiveColor: Colors.grey,
  onChanged: (double newValue) {
    setState(() {
      sliderValue = newValue.round();
    });
  },
),

Below is the resulting slider:

enter image description here

This is my goal:

enter image description here

Help please.

Upvotes: 2

Views: 14690

Answers (4)

user1506104
user1506104

Reputation: 7106

I got the idea from all your answers. Thanks! Here is my resulting solution:

SliderTheme(
  data: SliderThemeData(
    thumbColor: Colors.red,
    activeTrackColor: Colors.white,
    inactiveTrackColor: Colors.grey,
  ),
  child: Slider(
    value: sliderValue.toDouble(),
    max: 100.0,
    min: 0.0,
    onChanged: (double newValue) {
      setState(() {
        sliderValue = newValue.round();
      });
    },
  ),
),

Upvotes: 1

Reza Mohammadzadeh
Reza Mohammadzadeh

Reputation: 355

wrap with SliderTheme

SliderTheme(
      data: SliderThemeData(
          thumbColor: Colors.white,
      child: Slider(
        value: sliderValue.toDouble(),
        max: 100.0,
        min: 0.0,
        activeColor: Colors.red,
        inactiveColor: Colors.grey,
        onChanged: (double newValue) {
          setState(() {
            sliderValue = newValue.round();
          });
        },
      ),
    );

Upvotes: 2

chunhunghan
chunhunghan

Reputation: 54427

You can copy paste run full code below
You can use SliderTheme and set activeTrackColor and inactiveTrackColor

code snippet

    SliderTheme(
        data: SliderTheme.of(context).copyWith(
          activeTrackColor: Colors.white,
          inactiveTrackColor: Colors.grey,
          trackShape: RectangularSliderTrackShape(),
          trackHeight: 4.0,
          thumbColor: Colors.redAccent,
          thumbShape: RoundSliderThumbShape(enabledThumbRadius: 12.0),
          overlayColor: Colors.grey,
          overlayShape: RoundSliderOverlayShape(overlayRadius: 28.0),
        ),
        child: Slider(
          value: sliderValue.toDouble(),

working demo

enter image description here

full code

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  int sliderValue = 50;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              color: Colors.black,
              child: SliderTheme(
                data: SliderTheme.of(context).copyWith(
                  activeTrackColor: Colors.white,
                  inactiveTrackColor: Colors.grey,
                  trackShape: RectangularSliderTrackShape(),
                  trackHeight: 4.0,
                  thumbColor: Colors.redAccent,
                  thumbShape: RoundSliderThumbShape(enabledThumbRadius: 12.0),
                  overlayColor: Colors.grey,
                  overlayShape: RoundSliderOverlayShape(overlayRadius: 28.0),
                ),
                child: Slider(
                  value: sliderValue.toDouble(),
                  max: 100.0,
                  min: 0.0,
                  onChanged: (double newValue) {
                    setState(() {
                      sliderValue = newValue.round();
                    });
                  },
                ),
              ),
            ),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

Upvotes: 5

Marcel Dz
Marcel Dz

Reputation: 2714

the part you want to change is controlled with the SliderTheme

you can read the whole part in the docs: https://api.flutter.dev/flutter/material/SliderThemeData-class.html

just set the value to your fits.

SliderTheme(
    data: SliderThemeData(
            thumbColor: Colors.red,
            thumbShape: RoundSliderThumbShape(enabledThumbRadius: 20)),
    child: Slider(
          ...
          },
        ),
      ),

And your active color to Colors.white

Upvotes: 7

Related Questions