An animatable radio button that can be customized to the max.
I found it strange that flutter only provides two radio widgets: Radio and RadioListTile The main issue with these widgets is that both of them force the use of the default Android leading animated circular icon. This widget leaves everything up to the user by allowing them to provide their own builder function. On top of that, an animations builder can also be provided. This gets passed a parent animation controller with which the user can then use to create a list of animations that can animate the widgets transition between states.
Simply add custom_radio: ^0.1.2
as a dependancy in your pubspec.yaml file.
Then import 'package:custom_radio/custom_radio.dart';
wherever you need it.
If only one animation type is required then it can be specified to enable stronger typing.
CustomRadio<String, double>(
value: 'First',
groupValue: widget.radioValue,
duration: Duration(milliseconds: 500),
animsBuilder: (AnimationController controller) => [
CurvedAnimation(
parent: controller,
curve: Curves.easeInOut
)
],
builder: (BuildContext context, List<double> animValues, Function updateState, String value) {
final alpha = (animValues[0] * 255).toInt();
return GestureDetector(
onTap: () {
setState(() {
widget.radioValue = value;
});
},
child: Container(
padding: EdgeInsets.all(32.0),
margin: EdgeInsets.all(12.0),
alignment: Alignment.center,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Theme.of(context).primaryColor.withAlpha(alpha),
border: Border.all(
color: Theme.of(context).primaryColor.withAlpha(255 - alpha),
width: 4.0,
)
),
child: Text(
value,
style: Theme.of(context).textTheme.body1.copyWith(fontSize: 24.0),
)
)
);
}
)
But any combination of animation types are supported.
CustomRadio<String, dynamic>(
value: 'First',
groupValue: widget.radioValue,
animsBuilder: (AnimationController controller) => [
CurvedAnimation(
parent: controller,
curve: Curves.easeInOut
),
ColorTween(
begin: Colors.white,
end: Colors.deepPurple
).animate(controller),
ColorTween(
begin: Colors.deepPurple,
end: Colors.white
).animate(controller),
],
builder: (BuildContext context, List<dynamic> animValues, Function updateState, String value) {
return GestureDetector(
onTap: () {
setState(() {
widget.radioValue = value;
});
},
child: Container(
alignment: Alignment.center,
margin: EdgeInsets.all(18.0),
padding: EdgeInsets.all(32.0 + animValues[0] * 12.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: animValues[1],
border: Border.all(
color: animValues[2],
width: 2.0
)
),
child: Text(
value,
style: Theme.of(context).textTheme.body1.copyWith(
fontSize: 20.0,
color: animValues[2]
),
)
)
);
},
)
You can even recreate the default animation provided by Radio and add your own personal flairs!
Note: The full example can be found in the example
directory
CustomRadio<int, double>(
value: value,
groupValue: widget.radioValue,
duration: Duration(milliseconds: 400),
animsBuilder: (AnimationController controller) => [
CurvedAnimation(
parent: controller,
curve: Curves.ease
)
],
builder: ({ BuildContext context, List<double> animValues, Function updateState, bool checked }) {
return GestureDetector(
onTapDown: (TapDownDetails details) {
setState(() {
if (_controller.status != AnimationStatus.completed)
_controller.forward();
});
},
onTapUp: (TapUpDetails details) {
setState(() {
if (_controller.status != AnimationStatus.dismissed)
_controller.reverse();
});
},
onTap: () {
setState(() {
widget.radioValue = value;
});
},
child: Container(
margin: EdgeInsets.all(8.0),
width: 38.0,
height: 38.0,
alignment: Alignment.center,
decoration: BoxDecoration(
shape: BoxShape.circle,
),
child: Stack(
alignment: Alignment.center,
children: <Widget>[
Container(
width: 38.0 * _animation.value,
height: 38.0 * _animation.value,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Theme.of(context).primaryColor.withAlpha(40)
),
),
Container(
width: 18.0,
height: 18.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.transparent,
border: Border.all(
color: checked ? Theme.of(context).primaryColor : Theme.of(context).hintColor,
width: 2.0
)
),
),
Container(
width: 11.0 * animValues[0],
height: 11.0 * animValues[0],
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Theme.of(context).primaryColor,
),
),
]
),
)
);
}
)
example/main.dart
import 'package:custom_radio/custom_radio.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Custom Radio demo',
home: MyHomePage(title: 'Custom Radio example')
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
String radioValue = 'First';
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
_MyHomePageState() {
customBuilder = (BuildContext context, List<dynamic> animValues, Function updateState, String value) {
return GestureDetector(
onTap: () {
setState(() {
widget.radioValue = value;
});
},
child: Container(
width: double.infinity,
height: animValues[0] * 40 + 60,
color: animValues[1],
child: Center(
child: Text(
value
)
),
),
);
};
simpleBuilder = (BuildContext context, List<double> animValues, Function updateState, String value) {
final alpha = (animValues[0] * 255).toInt();
return GestureDetector(
onTap: () {
setState(() {
widget.radioValue = value;
});
},
child: Container(
padding: EdgeInsets.all(32.0),
margin: EdgeInsets.symmetric(horizontal: 2.0, vertical: 12.0),
alignment: Alignment.center,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Theme.of(context).primaryColor.withAlpha(alpha),
border: Border.all(
color: Theme.of(context).primaryColor.withAlpha(255 - alpha),
width: 4.0,
)
),
child: Text(
value,
style: Theme.of(context).textTheme.body1.copyWith(fontSize: 20.0),
)
)
);
};
dynamicBuilder = (BuildContext context, List<dynamic> animValues, Function updateState, String value) {
return GestureDetector(
onTap: () {
setState(() {
widget.radioValue = value;
});
},
child: Container(
alignment: Alignment.center,
margin: EdgeInsets.symmetric(horizontal: 4.0, vertical: 12.0),
padding: EdgeInsets.all(32.0 + animValues[0] * 12.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: animValues[1],
border: Border.all(
color: animValues[2],
width: 2.0
)
),
child: Text(
value,
style: Theme.of(context).textTheme.body1.copyWith(
fontSize: 20.0,
color: animValues[2]
),
)
)
);
};
}
RadioBuilder<String, dynamic> customBuilder;
RadioBuilder<String, double> simpleBuilder;
RadioBuilder<String, dynamic> dynamicBuilder;
AnimationController _controller;
Animation<double> _animation;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: Duration(milliseconds: 100),
vsync: this
);
_animation = CurvedAnimation(
parent: _controller,
curve: Curves.ease
);
_controller.addListener(() {
setState(() {});
});
}
@override
void dispose() {
_controller?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: ListView(
children: <Widget>[
CustomRadio<String, dynamic>(
value: 'Green',
groupValue: widget.radioValue,
duration: Duration(milliseconds: 800),
animsBuilder: (AnimationController controller) => [
CurvedAnimation(
parent: controller,
curve: Curves.ease,
),
ColorTween(begin: Colors.greenAccent.withAlpha(200), end: Colors.green).animate(controller),
],
builder: customBuilder
),
CustomRadio<String, dynamic>(
value: 'Red',
groupValue: widget.radioValue,
duration: Duration(milliseconds: 800),
animsBuilder: (AnimationController controller) => [
CurvedAnimation(
parent: controller,
curve: Curves.ease,
),
ColorTween(begin: Colors.redAccent.withAlpha(200), end: Colors.red).animate(controller),
],
builder: customBuilder,
),
CustomRadio<String, dynamic>(
value: 'Blue',
groupValue: widget.radioValue,
duration: Duration(milliseconds: 800),
animsBuilder: (AnimationController controller) => [
CurvedAnimation(
parent: controller,
curve: Curves.ease,
),
ColorTween(begin: Colors.blueAccent.withAlpha(200), end: Colors.blue).animate(controller),
],
builder: customBuilder,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CustomRadio<String, double>(
value: 'First',
groupValue: widget.radioValue,
duration: Duration(milliseconds: 500),
animsBuilder: (AnimationController controller) => [
CurvedAnimation(
parent: controller,
curve: Curves.easeInOut
)
],
builder: simpleBuilder
),
CustomRadio<String, double>(
value: 'Second',
groupValue: widget.radioValue,
duration: Duration(milliseconds: 500),
animsBuilder: (AnimationController controller) => [
CurvedAnimation(
parent: controller,
curve: Curves.easeInOut
)
],
builder: simpleBuilder
),
CustomRadio<String, double>(
value: 'Third',
groupValue: widget.radioValue,
duration: Duration(milliseconds: 500),
animsBuilder: (AnimationController controller) => [
CurvedAnimation(
parent: controller,
curve: Curves.easeInOut
)
],
builder: simpleBuilder
),
],
),
Divider(),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CustomRadio<String, dynamic>(
value: 'Fourth',
groupValue: widget.radioValue,
animsBuilder: (AnimationController controller) => [
CurvedAnimation(
parent: controller,
curve: Curves.easeInOut
),
ColorTween(
begin: Colors.white,
end: Colors.deepPurple
).animate(controller),
ColorTween(
begin: Colors.deepPurple,
end: Colors.white
).animate(controller),
],
builder: dynamicBuilder,
),
CustomRadio<String, dynamic>(
value: 'Fifth',
groupValue: widget.radioValue,
animsBuilder: (AnimationController controller) => [
CurvedAnimation(
parent: controller,
curve: Curves.easeInOut
),
ColorTween(
begin: Colors.white,
end: Colors.deepPurple
).animate(controller),
ColorTween(
begin: Colors.deepPurple,
end: Colors.white
).animate(controller),
],
builder: dynamicBuilder,
),
CustomRadio<String, dynamic>(
value: 'Sixth',
groupValue: widget.radioValue,
animsBuilder: (AnimationController controller) => [
CurvedAnimation(
parent: controller,
curve: Curves.easeInOut
),
ColorTween(
begin: Colors.white,
end: Colors.deepPurple
).animate(controller),
ColorTween(
begin: Colors.deepPurple,
end: Colors.white
).animate(controller),
],
builder: dynamicBuilder,
),
]
),
Divider(),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Radio<String>(
value: 'Seventh',
groupValue: widget.radioValue,
onChanged: (String value) {
setState(() {
widget.radioValue = value;
});
}
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 12.0),
child: Text('Radio')
)
],
),
CustomRadio<String, double>(
value: 'Eighth',
groupValue: widget.radioValue,
duration: Duration(milliseconds: 400),
animsBuilder: (AnimationController controller) => [
CurvedAnimation(
parent: controller,
curve: Curves.ease
)
],
builder: (BuildContext context, List<double> animValues, Function updateState, String value) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
GestureDetector(
onTapDown: (TapDownDetails details) {
setState(() {
if (_controller.status != AnimationStatus.completed)
_controller.forward();
});
},
onTapUp: (TapUpDetails details) {
setState(() {
if (_controller.status != AnimationStatus.dismissed)
_controller.reverse();
});
},
onTap: () {
setState(() {
widget.radioValue = value;
});
},
child: Container(
margin: EdgeInsets.all(8.0),
width: 38.0,
height: 38.0,
alignment: Alignment.center,
decoration: BoxDecoration(
shape: BoxShape.circle,
),
child: Stack(
alignment: Alignment.center,
children: <Widget>[
Container(
width: 38.0 * _animation.value,
height: 38.0 * _animation.value,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Theme.of(context).primaryColor.withAlpha(40)
),
),
Container(
width: 18.0,
height: 18.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.transparent,
border: Border.all(
color: animValues[0] >= 0.5 ? Theme.of(context).primaryColor : Theme.of(context).hintColor,
width: 2.0
)
),
),
Container(
width: 9.0 * animValues[0],
height: 9.0 * animValues[0],
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Theme.of(context).primaryColor,
),
),
]
),
)
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 12.0),
child: Text('CustomRadio')
)
]
);
},
),
],
),
);
}
}
Add this to your package's pubspec.yaml file:
dependencies:
custom_radio: ^0.1.2
You can install packages from the command line:
with Flutter:
$ flutter packages get
Alternatively, your editor might support flutter packages get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:custom_radio/custom_radio.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
0.1.2 | Oct 6, 2018 |
|
|
0.1.1 | Sep 23, 2018 |
|
|
0.1.0 | Sep 23, 2018 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
81
|
Health:
Code health derived from static analysis.
[more]
|
75
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
100
|
Overall:
Weighted score of the above.
[more]
|
83
|
We analyzed this package on Feb 14, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter
References Flutter, and has no conflicting libraries.
Fix lib/custom_radio.dart
. (-25 points)
Analysis of lib/custom_radio.dart
failed with 1 error:
line 102 col 36: The argument type '(Null) → Null' can't be assigned to the parameter type '(void) → dynamic'.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=2.0.0 <3.0.0 | ||
flutter | 0.0.0 | ||
Transitive dependencies | |||
collection | 1.14.11 | ||
meta | 1.1.6 | 1.1.7 | |
sky_engine | 0.0.99 | ||
typed_data | 1.1.6 | ||
vector_math | 2.0.8 | ||
Dev dependencies | |||
flutter_test |