繁体   English   中英

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

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

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

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

下面是生成的 slider:

在此处输入图像描述

这是我的目标:

在此处输入图像描述

请帮忙。

用 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控制

您可以阅读文档中的整个部分: 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(
  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();
      });
    },
  ),
),

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM