如何在flutter中自定义滑块小部件的拇指颜色?
我想更改小部件的圆形部分的颜色。此颜色应不同于activeColor和inactiveColor颜色。这是我当前的代码:如何在flutter中自定义滑块小部件的拇指颜色?,flutter,slider,themes,Flutter,Slider,Themes,我想更改小部件的圆形部分的颜色。此颜色应不同于activeColor和inactiveColor颜色。这是我当前的代码: Slider( value: sliderValue.toDouble(), max: 100.0, min: 0.0, activeColor: Colors.red, inactiveColor: Colors.grey, onChanged: (double newValue) { setState(() { sliderVa
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,
thumbShape: RoundSliderThumbShape(enabledThumbRadius: 20)),
child: Slider(
...
},
),
),
将活动颜色转换为颜色。白色要更改的部分由滑块主题控制 您可以在文档中阅读整个部分: 只需将值设置为适合的值
SliderTheme(
data: SliderThemeData(
thumbColor: Colors.red,
thumbShape: RoundSliderThumbShape(enabledThumbRadius: 20)),
child: Slider(
...
},
),
),
将活动颜色转换为颜色。白色您可以复制粘贴运行下面的完整代码 您可以使用SliderTheme并设置activeTrackColor和inactiveTrackColor 代码片段
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并设置activeTrackColor和inactiveTrackColor 代码片段
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),
),
);
}
}
用滑梯缠绕
幻灯片主题
数据:SliderThemeData
拇指颜色:颜色。白色,
子:滑块
值:sliderValue.toDouble,
最高:100.0,
最小值:0.0,
activeColor:Colors.red,
不活动颜色:颜色。灰色,
onChanged:double newValue{
设定状态{
sliderValue=newValue.round;
};
},
,
;
用滑梯缠绕
幻灯片主题
数据:SliderThemeData
拇指颜色:颜色。白色,
子:滑块
值:sliderValue.toDouble,
最高:100.0,
最小值:0.0,
activeColor:Colors.red,
不活动颜色:颜色。灰色,
onChanged:double newValue{
设定状态{
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();
});
},
),
),
我从你所有的答案中得到了这个想法。谢谢以下是我的最终解决方案:
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();
});
},
),
),