如何在 flutter 中自定义我的 Slider 小部件的拇指颜色?

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

我想更改小部件圆形部分的颜色。此颜色应不同于 activeColorinactiveColor 颜色。这是我当前的代码:

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

下面是结果滑块:

这是我的目标:

请帮忙。

您要更改的部分由 SliderTheme

控制

您可以在文档中阅读整个部分:https://api.flutter.dev/flutter/material/SliderThemeData-class.html

只需设置适合您的值即可。

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

并且你的活跃颜色为Colors.white

您可以复制粘贴 运行 下面的完整代码
您可以使用 SliderTheme 并设置 activeTrackColorinactiveTrackColor

代码片段

    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(),

工作演示

完整代码

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),
      ),
    );
  }
}

用 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();
          });
        },
      ),
    );

我从你所有的回答中得到了灵感。谢谢!这是我得到的解决方案:

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();
      });
    },
  ),
),