Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

custom radio button

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.

installation the radio button

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.

examples

radio button

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),
        )
      )
    );
  }
)

simple_example

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]
          ),
        )
      )
    );
  },
)

dynamic_example

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,
              ),
            ),
          ]
        ),
      )
    );
  }
)

radio_clone_example


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.