flutter customizable circular
a customizable circular slider for flutter.
installation a customizable circular
add
flutter_circular_slider : ^lastest_version
to your pubspec.yaml, and run
flutter packages get
in your project’s root directory.
basic usage for customizable circular
create a new project with command
flutter create myapp
edit lib/main.dart like this:
import 'package:flutter/material.dart';
import 'package:flutter_circular_slider/flutter_circular_slider.dart';
void main() => runapp(myapp());
class myapp extends statelesswidget {
@override
widget build(buildcontext context) {
return materialapp(
theme: themedata(
primaryswatch: colors.blue,
),
home: myhomepage(),
);
}
}
class myhomepage extends statelesswidget {
@override
widget build(buildcontext context) {
return scaffold(
backgroundcolor: colors.bluegrey,
body: center(
child: container(child: doublecircularslider(100, 0, 20)),
));
}
}
there are two different options:
- singlecircularslider: has only one handler and can be moved either dragging the handler or just by clicking on different parts of the slider.
- doublecircularslider: has two handlers and both have to be moved by dragging them.
constructor
parameter | default | description |
---|---|---|
divisions | the number of sections in which the circle will be divided for selection. | |
init | (only for doublecircularslider) the initial value in the selection. has to be bigger than 0 and smaller than divisions. | |
end | (only for doublecircularslider) the end value in the selection. has to be bigger than 0 and smaller than divisions. | |
position | (only for singlecircularslider) the selection. has to be bigger than 0 and smaller than divisions. | |
height | 220.0 | height of the canvas where the slider is rendered. |
width | 220.0 | width of the canvas where the slider is rendered. |
primarysectors | 0 | number of sectors painted in the base circle. painted in selectioncolor. |
secondarysectors | 0 | number of secondary sectors painted in the base circle. painted in basecolor. |
child | null | widget that will be inserted in the center of the circular slider. |
onselectionchange | void onselectionchange(int init, int end) | triggered every time the user interacts with the slider and changes the init and end values. |
basecolor | color.fromrgbo(255, 255, 255, 0.1) | the color used for the base of the circle. |
selectioncolor | color.fromrgbo(255, 255, 255, 0.3) | the color used for the selection in the circle. |
handlercolor | colors.white | the color used for the handlers. |
handleroutterradius | 12.0 | the radius for the outter circle around the handler. |
use cases
sleep time selection
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_circular_slider/flutter_circular_slider.dart';
void main() => runapp(myapp());
class myapp extends statelesswidget {
@override
widget build(buildcontext context) {
return materialapp(
theme: themedata(
primaryswatch: colors.blue,
),
home: myhomepage(),
);
}
}
class myhomepage extends statelesswidget {
@override
widget build(buildcontext context) {
return scaffold(
body: safearea(
child: container(
decoration: boxdecoration(
image: decorationimage(
image: assetimage('images/background_morning.png'),
fit: boxfit.cover,
),
),
child: sleeppage()),
));
}
}
class sleeppage extends statefulwidget {
@override
_sleeppagestate createstate() => _sleeppagestate();
}
class _sleeppagestate extends state<sleeppage> {
final basecolor = color.fromrgbo(255, 255, 255, 0.3);
int inittime;
int endtime;
int inbedtime;
int outbedtime;
@override
void initstate() {
super.initstate();
_shuffle();
}
void _shuffle() {
setstate(() {
inittime = _generaterandomtime();
endtime = _generaterandomtime();
inbedtime = inittime;
outbedtime = endtime;
});
}
void _updatelabels(int init, int end) {
setstate(() {
inbedtime = init;
outbedtime = end;
});
}
@override
widget build(buildcontext context) {
return column(
mainaxisalignment: mainaxisalignment.spaceevenly,
children: [
text(
'how long did you stay in bed?',
style: textstyle(color: colors.white),
),
circularslider(
288,
inittime,
endtime,
height: 220.0,
width: 220.0,
basecolor: color.fromrgbo(255, 255, 255, 0.1),
selectioncolor: basecolor,
handlercolor: colors.white,
handleroutterradius: 12.0,
onselectionchange: _updatelabels,
child: padding(
padding: const edgeinsets.all(12.0),
child: center(
child: text('${_formatintervaltime(inbedtime, outbedtime)}',
style: textstyle(fontsize: 36.0, color: colors.white))),
),
),
row(mainaxisalignment: mainaxisalignment.spaceevenly, children: [
_formatbedtime('in the', inbedtime),
_formatbedtime('out of', outbedtime),
]),
flatbutton(
child: text('s h u f f l e'),
color: basecolor,
textcolor: colors.white,
shape: roundedrectangleborder(
borderradius: borderradius.circular(50.0),
),
onpressed: _shuffle,
),
],
);
}
widget _formatbedtime(string pre, int time) {
return column(
children: [
text(pre, style: textstyle(color: basecolor)),
text('bed at', style: textstyle(color: basecolor)),
text(
'${_formattime(time)}',
style: textstyle(color: colors.white),
)
],
);
}
string _formattime(int time) {
if (time == 0 || time == null) {
return '00:00';
}
var hours = time ~/ 12;
var minutes = (time % 12) * 5;
return '$hours:$minutes';
}
string _formatintervaltime(int init, int end) {
var sleeptime = end > init ? end - init : 288 - init + end;
var hours = sleeptime ~/ 12;
var minutes = (sleeptime % 12) * 5;
return '${hours}h${minutes}m';
}
int _generaterandomtime() => random().nextint(288);
}
Comments are closed.