32
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

FlutterのSliderの色設定を図にまとめた

Posted at

指定した範囲から値を選ぶ Slider というWidgetを初めて使用した。

デフォルトはこんな感じ

このSliderの色をカスタマイズするとき、**何のプロパティがどこの色を変えられるのか?**がややこしかったので図にまとめた。

スクリーンショット 2020-02-07 8.47.22.png

※分かりやすいように各部品に色を指定している

Sliderのコードは以下

SliderTheme(
  data: SliderTheme.of(context).copyWith(
    trackHeight: 10,
    thumbColor: Colors.purpleAccent,
    thumbShape: RoundSliderThumbShape(enabledThumbRadius: 12),
    valueIndicatorColor: Colors.orange,
    overlayColor: Colors.orange.withAlpha(80),
    activeTrackColor: Colors.black,
    inactiveTrackColor: Colors.amber,
    inactiveTickMarkColor: Colors.blue,
    activeTickMarkColor: Colors.green,
  ),
  child: Slider(
    min: 0,
    max: 100,
    divisions: 10,
    value: _customSliderValue,
    label: '${_customSliderValue.floor()}',
    onChanged: (d) => setState(() {
      _customSliderValue = d;
    }),
  ),
)

上記のSliderTheme.of(context).copyWithの中身を、MaterialAppのsliderThemeにそのまま使うことができる。

SliderThemeDataのコード

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // スライダーのテーマ指定
        sliderTheme: SliderThemeData(
          trackHeight: 10,
          thumbColor: Colors.purpleAccent,
          thumbShape: RoundSliderThumbShape(enabledThumbRadius: 12),
          valueIndicatorColor: Colors.orange,
          overlayColor: Colors.orange.withAlpha(80),
          activeTrackColor: Colors.black,
          inactiveTrackColor: Colors.amber,
          inactiveTickMarkColor: Colors.blue,
          activeTickMarkColor: Colors.green,
        ),
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

使用したコードのgithub
https://github.com/ikemura23/flutter_lab/blob/slider/lib/main.dart

32
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?