Flutter widget to create a group of buttons fast ✈️

How often you see buttons in applications🧐?

I think in every application that you seen in life.
And buttons are different

Like that
Or like that
Or like that if your designer is stuck in the past

But all these buttons are ofte…


This content originally appeared on DEV Community and was authored by DEV Community

How often you see buttons in applications🧐?

I think in every application that you seen in life.
And buttons are different

Like that Or like that Or like that if your designer is stuck in the past
Image description Image description Image description

But all these buttons are often grouped together for some purposes.

For multiselect forms Select gender
Image description Image description
For filters Another selection
Image description Image description

And a lot of other cases...

And how to make similar widgets in Flutter ?

Flutter have base Radio widget for select one of all items logic.
It is very easy to use.
You need to tell the value of this element and the current selected value for the entire group

Column(
    mainAxisAlignment: MainAxisAlignment.start,
    children: [
      ListTile(
        title: Text("Male"),
        leading: Radio(
          value: 1,
          groupValue: val,
          onChanged: (value) {
            setState(() {
              val = value;
            });
          },
          activeColor: Colors.green,
        ),
      ),
      ListTile(
        title: Text("Female"),
        leading: Radio(
          value: 2,
          groupValue: val,
          onChanged: (value) {
            setState(() {
              val = value;
            });
          },
          activeColor: Colors.green,
        ),
      ),
    ],
  )

For multiple choice we have a Checkbox.
Everything is not so simple with him, the work of several checkboxes still needs to be organized.

Checkbox(
  value: false,
  onChanged: (value) {
    setState(() {
      _value = value;
    });
  },
)

What if I don 't want to spend a lot of time releasing similar widgets with logic ?

Or maybe I want to make the widgets custom, not the default Radio or Checkbox ?

That's why I'm writing this post
I have created a library to creating the selection buttons in minimum of time.

For example - see how you can make a group of time selection buttons in 5 lines

GroupButton(
    isRadio: false,
    onSelected: (index, isSelected) => print('$index button is selected'),
    buttons: ["12:00", "13:00", "14:30", "18:00", "19:00", "21:40", "22:00", "23:30"],
)

Here result:
Image description

Well, what if I want to use default checkboxes?

It will be very simple

  • First we will put the buttons in a variable and create a controller
  • After that we will add a buttonBuilder to build any of your most interesting button UI
  • That's all ...
final _controller = GroupButtonController();

final _buttons = ["12:00", "13:00", "14:30", "18:00", "19:00", "21:40", "22:00", "23:30"];

GroupButton(
  isRadio: false,
  controller: _controller,
  onSelected: (index, isSelected) =>
    print('$index button is selected'),
  buttons: _buttons,
  buttonBuilder: (selected, i, context) => CheckBoxTile(
    title: _buttons[i],
    selected: selected,
    onTap: () => _controller.toggleIndexes([i]),
  ),
),

Here result:
Image description

I could tell you a lot more about this package, but I think its repository will tell everything instead of me.

Package link

GroupButton
Support this project with an ⭐️asterisk on Github. It's not difficult, but it's very nice

Source code

A full example for this package can be found here


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-03-07T21:21:04+00:00) Flutter widget to create a group of buttons fast ✈️. Retrieved from https://www.scien.cx/2022/03/07/flutter-widget-to-create-a-group-of-buttons-fast-%e2%9c%88%ef%b8%8f/

MLA
" » Flutter widget to create a group of buttons fast ✈️." DEV Community | Sciencx - Monday March 7, 2022, https://www.scien.cx/2022/03/07/flutter-widget-to-create-a-group-of-buttons-fast-%e2%9c%88%ef%b8%8f/
HARVARD
DEV Community | Sciencx Monday March 7, 2022 » Flutter widget to create a group of buttons fast ✈️., viewed ,<https://www.scien.cx/2022/03/07/flutter-widget-to-create-a-group-of-buttons-fast-%e2%9c%88%ef%b8%8f/>
VANCOUVER
DEV Community | Sciencx - » Flutter widget to create a group of buttons fast ✈️. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/07/flutter-widget-to-create-a-group-of-buttons-fast-%e2%9c%88%ef%b8%8f/
CHICAGO
" » Flutter widget to create a group of buttons fast ✈️." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/03/07/flutter-widget-to-create-a-group-of-buttons-fast-%e2%9c%88%ef%b8%8f/
IEEE
" » Flutter widget to create a group of buttons fast ✈️." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/03/07/flutter-widget-to-create-a-group-of-buttons-fast-%e2%9c%88%ef%b8%8f/. [Accessed: ]
rf:citation
» Flutter widget to create a group of buttons fast ✈️ | DEV Community | Sciencx | https://www.scien.cx/2022/03/07/flutter-widget-to-create-a-group-of-buttons-fast-%e2%9c%88%ef%b8%8f/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.