如何为Flutter中的Slider设置标签颜色?

tjvv9vkg  于 2023-11-21  发布在  Flutter
关注(0)|答案(2)|浏览(132)

是否可以将标签颜色设置为与活动颜色不同?我尝试了很多选项(见下文),但都不起作用。

import 'package:flutter/material.dart';

void main() {
  var themeData = ThemeData(
    useMaterial3: true,
  );
  runApp(MaterialApp(
    theme: themeData.copyWith(
        sliderTheme: themeData.sliderTheme.copyWith(
      valueIndicatorColor: themeData.primaryColor,
      disabledActiveTickMarkColor: themeData.primaryColor,
      disabledActiveTrackColor: themeData.primaryColor,
      disabledInactiveTickMarkColor: themeData.primaryColor,
      disabledInactiveTrackColor: themeData.primaryColor,
      disabledSecondaryActiveTrackColor: themeData.primaryColor,
      activeTickMarkColor: themeData.primaryColor,
      activeTrackColor: themeData.primaryColor,
      secondaryActiveTrackColor: themeData.primaryColor,
      inactiveTickMarkColor: themeData.primaryColor,
      inactiveTrackColor: themeData.primaryColor,
    )),
    home: const MyHomePage(),
  ));
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  double sliderValue = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Slider(
        value: sliderValue,
        max: 23,
        divisions: 23,
        label: "$sliderValue",
        inactiveColor: Theme.of(context).primaryColor,
        activeColor: Theme.of(context).disabledColor,
        thumbColor: Theme.of(context).primaryColor,
        onChanged: (double value) {
          setState(() {
            sliderValue = value;
          });
        },
      ),
    );
  }
}

字符串

41zrol4v

41zrol4v1#

你可以通过像这样使用SliderTheme给滑块给予标签颜色

SliderTheme(
         data: const SliderThemeData(
                 valueIndicatorTextStyle: TextStyle(
                  color: Colors.red, // Change the label color here
                 ),
          ),
      child: Slider(
        value: sliderValue,
        max: 23,
        divisions: 23,
        label: "$sliderValue",
        inactiveColor: Theme.of(context).primaryColor,
        activeColor: Theme.of(context).disabledColor,
        thumbColor: Theme.of(context).primaryColor,
        onChanged: (double value) {
          setState(() {
            sliderValue = value;
          });
        },
      ),
),

字符串

06odsfpq

06odsfpq2#

当然可以!此代码片段演示了使用工具提示小部件单独自定义标签颜色和滑块的活动颜色。它将标签放置在滑块附近,以红色文本样式显示值。根据需要调整颜色或任何其他文本样式属性以适合您的设计首选项。

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Slider(
            value: sliderValue,
            max: 23,
            divisions: 23,
            inactiveColor: Theme.of(context).primaryColor,
            activeColor: Theme.of(context).disabledColor,
            onChanged: (double value) {
              setState(() {
                sliderValue = value;
              });
            },
          ),
          SizedBox(height: 20),
          // Creating a custom label using Tooltip widget
          Tooltip(
            message: '$sliderValue',
            preferBelow: false, // Adjust this to change tooltip position
            child: Text(
              '$sliderValue',
              style: TextStyle(
                color: Colors.red, // Set your desired label color here
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

字符串

相关问题