Layout inconsistencies between platforms when using Wrap, Positioned, Chip

huangapple go评论70阅读模式
英文:

Layout inconsistencies between platforms when using Wrap, Positioned, Chip

问题

我正在为我的应用程序开发一个"徽章"小部件,发现在某些平台上布局正如预期,而在其他平台上则不是。例如,这是 Chrome 的屏幕截图,徽章的布局是我想要的:

但在 iOS 模拟器中,布局不正确:

如您所见,星星不再在芯片内对齐,而"行"之间的间距增加了。

我创建了一个简单的Flutter应用程序,您可以使用它来重现这个问题。README提供了更多有关这个问题的详细信息:

英文:

I am developing a widget for "Badges" for my app and have discovered that it lays out as intended on some platforms but not on others. For example, here is a screen shot for Chrome where the badge layout is what I want:

Layout inconsistencies between platforms when using Wrap, Positioned, Chip

But the layout is not correct in the iOS simulator:

Layout inconsistencies between platforms when using Wrap, Positioned, Chip

As you can see, the stars are no longer aligned within the Chip and the spacing between "rows" has increased.

I have created a simple Flutter app which you can use to reproduce this problem. The README provides more details on the issue:

<https://github.com/philipmjohnson/fluttermonarchlayout>

Any suggestions on how to proceed with this issue would be highly appreciated!

答案1

得分: 0

label参数在芯片小部件中接受一个小部件。您可以使用一个Row小部件将您的Text小部件包装起来,然后将点放在您的Text之前。

示例:

class BadgeIcon extends StatelessWidget {
  const BadgeIcon({Key? key, required this.name, required this.level})
      : super(key: key);
  final String name;
  final int level;

  @override
  Widget build(BuildContext context) {
    const Color selectedColor = Colors.black;
    BadgeDot dot = const BadgeDot(color: selectedColor);

    List<Widget> dots = [];
    if (level == 1) {
      dots = [dot];
    } else if (level == 2) {
      dots = [dot, dot];
    } else if (level == 3) {
      dots = [dot, dot, dot];
    }

    return Padding(
      padding: const EdgeInsets.all(3.0),
      child: Chip(
        label: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: dots,
            ),
            const SizedBox(width: 4),
            Text(name),
          ],
        ),
      ),
    );
  }
}
英文:

The label parameter in the Chip widget accepts a Widget. You can wrap your Text widget with a Row and place the dots before your Text.

Example:

class BadgeIcon extends StatelessWidget {
  const BadgeIcon({Key? key, required this.name, required this.level})
      : super(key: key);
  final String name;
  final int level;

  @override
  Widget build(BuildContext context) {
    const Color selectedColor = Colors.black;
    BadgeDot dot = const BadgeDot(color: selectedColor);

    List&lt;Widget&gt; dots = [];
    if (level == 1) {
      dots = [dot];
    } else if (level == 2) {
      dots = [dot, dot];
    } else if (level == 3) {
      dots = [dot, dot, dot];
    }

    return Padding(
      padding: const EdgeInsets.all(3.0),
      child: Chip(
        label: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: dots,
            ),
            const SizedBox(width: 4),
            Text(name),
          ],
        ),
      ),
    );
  }
}

答案2

得分: 0

我收到了一些建议,并在此处总结了我决定采用的方法和解决方案:

https://github.com/philipmjohnson/fluttermonarchlayout/tree/main#solutions

英文:

I received a few suggestions and have summarized the approaches and the solution I decided upon here:

https://github.com/philipmjohnson/fluttermonarchlayout/tree/main#solutions

huangapple
  • 本文由 发表于 2023年6月1日 05:23:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定