Search code examples
user-interfacefluttertogglebutton

How to remove space from ToggleButtons in Flutter and make it scroll


In the ToggleButtons-Example there is not much space between the icons: https://api.flutter.dev/flutter/material/ToggleButtons-class.html

enter image description here

When I use the code provided, I get that

enter image description here

How can I remove the space on the left and on the right?

And is it possible scroll the toggleButtons - or even to "page" them (clicking f. e. on buttons on the left and on the right of the toggle buttons and "scroll/move" by one icon in a direction)?


Solution

  • How can I remove the space on the left and on the right?

    How Bogdan Orzea said, in Flutter last release (version 1.9.1) isn't possible to change the padding of the ToggleButtons's children. Probably in the next Flutter release it will be possible. If you can't wait until the next release, you can update Flutter to version 1.12.13+hotfix.3 (beta). In this beta version the ToggleButtons's children will be square like is shown in ToggleButtons-Example, and you can change the padding using Padding Widget, like the code below:

    ToggleButtons(
      children: <Widget>[
        Padding(
          padding: EdgeInsets.only(left: 40.0, right: 40.0),
          child: Text('Option 1')
        ),
        Padding(
          padding: EdgeInsets.only(left: 40.0, right: 40.0),
          child: Text('Option 2')
        ),
        Padding(
          padding: EdgeInsets.only(left: 40.0, right: 40.0),
          child: Text('Option 3')
        ),
        Padding(
          padding: EdgeInsets.only(left: 40.0, right: 40.0),
          child: Text('Option 4')
        )
      ],
    )
    

    And is it possible scroll the toggleButtons - or even to "page" them (clicking f. e. on buttons on the left and on the right of the toggle buttons and "scroll/move" by one icon in a direction)?

    Wrap your ToggleButton inside the SingleChildScrollView widget:

    SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: ToggleButtons(
        children: (...)
      )
    )