简体   繁体   English

如何从 Flutter 中的 ToggleButtons 中删除空间并使其滚动

[英]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在 ToggleButtons-Example 中,图标之间没有太多空间: https://api.flutter.dev/flutter/material/ToggleButtons-class.html

在此处输入图像描述

When I use the code provided, I get that当我使用提供的代码时,我明白了

在此处输入图像描述

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 fe on buttons on the left and on the right of the toggle buttons and "scroll/move" by one icon in a direction)?是否可以滚动切换按钮 - 甚至“翻页”它们(单击切换按钮左侧和右侧的按钮 fe 并在一个方向上“滚动/移动”一个图标)?

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. Bogdan Orzea 怎么说,在 Flutter 最新版本(版本 1.9.1)中,无法更改 ToggleButtons 子项的填充。 Probably in the next Flutter release it will be possible.可能在下一个 Flutter 版本中将成为可能。 If you can't wait until the next release, you can update Flutter to version 1.12.13+hotfix.3 (beta).如果等不及下个版本,可以将 Flutter 更新到版本 1.12.13+hotfix.3(测试版)。 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:在这个 beta 版本中,ToggleButtons 的子项将是方形的,如 ToggleButtons-Example 中所示,您可以使用 Padding Widget 更改填充,如下面的代码:

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 fe on buttons on the left and on the right of the toggle buttons and "scroll/move" by one icon in a direction)?是否可以滚动切换按钮 - 甚至“翻页”它们(单击切换按钮左侧和右侧的按钮上的 fe 并在一个方向上“滚动/移动”一个图标)?

Wrap your ToggleButton inside the SingleChildScrollView widget:将您的 ToggleButton 包装在 SingleChildScrollView 小部件中:

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

You can wrap any widget with a SingleChildScrollView like this:您可以使用SingleChildScrollView包装任何小部件,如下所示:

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

Probably the next Flutter release will include a PR that adds constraints parameter to ToggleButtons widget ( https://github.com/flutter/flutter/pull/39857 ).可能下一个 Flutter 版本将包含一个 PR,它将约束参数添加到ToggleButtons小部件( https://github.com/flutter/flutter/pull/39857 )。 Until then, you can use the SingleChildScrollView method.在此之前,您可以使用 SingleChildScrollView 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM