Created
December 24, 2019 11:13
-
-
Save GAM3RG33K/df88e40f75ae9f3860fd97e898e29fcd to your computer and use it in GitHub Desktop.
Cutom Thumb shape Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | |
// for details. All rights reserved. Use of this source code is governed by a | |
// BSD-style license that can be found in the LICENSE file. | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Custom Slider Thumb Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
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> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: SliderTheme( | |
child: RangeSlider( | |
min: 0, | |
max: 100, | |
divisions: 10, | |
onChanged: (RangeValues value) { | |
if (value.end - value.start <= 10) return; | |
print('current progress: $value'); | |
}, | |
values: RangeValues(0, 100), | |
), | |
data: SliderThemeData( | |
// change size here to customize the thumb | |
rangeThumbShape: SliderThumbShape(thumbSize: 15.0), | |
), | |
), | |
), | |
); | |
} | |
} | |
class SliderThumbShape extends RangeSliderThumbShape { | |
final double thumbSize; | |
const SliderThumbShape({this.thumbSize = 10.0}); | |
@override | |
Size getPreferredSize(bool isEnabled, bool isDiscrete) { | |
return Size.fromWidth(thumbSize); | |
} | |
@override | |
void paint( | |
PaintingContext context, | |
Offset center, { | |
Animation<double> activationAnimation, | |
@required Animation<double> enableAnimation, | |
bool isDiscrete, | |
bool isEnabled, | |
bool isOnTop, | |
TextDirection textDirection, | |
SliderThemeData sliderTheme, | |
Thumb thumb, | |
}) { | |
assert(context != null); | |
assert(center != null); | |
assert(enableAnimation != null); | |
assert(sliderTheme != null); | |
assert(sliderTheme.disabledThumbColor != null); | |
assert(sliderTheme.thumbColor != null); | |
final Canvas canvas = context.canvas; | |
final ColorTween colorTween = ColorTween( | |
begin: sliderTheme.disabledThumbColor, | |
end: sliderTheme.thumbColor, | |
); | |
canvas.drawRect( | |
Rect.fromCenter(center: center, height: thumbSize/2, width: thumbSize), | |
Paint()..color = colorTween.evaluate(enableAnimation), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment