Flutter 横幅不适合

标签 flutter banner

我对横幅小部件有疑问。为了演示它,我制作了一些演示代码。我想要的是在容器的右上角有一个横幅,但它很难看,因为它溢出了它的 child (请参见附图)。

enter image description here

这是我的代码:

class TestPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Banner(
          message: "hello",
          location: BannerLocation.topEnd,
          color: Colors.red,
          child: Container(
            margin: const EdgeInsets.all(10.0),
            color: Colors.yellow,
            height: 100,
            child: Center(child: Text("Hello, banner!"),),
          ),
        ),
      ),
    );
  }
}

我试着玩边距,但即使边距设置为 0,我仍然有这种行为。

如何避免这种“横幅溢出”?

非常感谢!

最佳答案

只需将 ClipRect 添加到 Op 的代码中

return Scaffold(
      body: Center(
        child: ClipRect(
          child: Banner(
            message: "hello",
            location: BannerLocation.topEnd,
            color: Colors.red,
            child: Container(
              margin: const EdgeInsets.all(10.0),
              color: Colors.yellow,
              height: 100,
              child: Center(child: Text("Hello, banner!"),),
            ),
          ),
        ),
      ),
    );

enter image description here

翻转容器和横幅

return Scaffold(
      body: Center(
        child: Container(
          margin: const EdgeInsets.all(10.0),

          height: 100,
          color: Colors.yellow,
          child: Banner(
            message: "hello",
            location: BannerLocation.topEnd,
            color: Colors.red,
            child: Container(


              child: Center(child: Text("Hello, banner!"),),
            ),
          ),
        ),
      ),

enter image description here

添加 ClipRect 以反转 Container 和 Banner

return Scaffold(
      body: Center(
        child: ClipRect(
        child: Container(
          margin: const EdgeInsets.all(10.0),

          height: 100,
          color: Colors.yellow,

            child: Banner(
              message: "hello",
              location: BannerLocation.topEnd,
              color: Colors.red,
              child: Container(


                child: Center(child: Text("Hello, banner!"),),
              ),
            ),
          ),
        ),
      ),
    );

enter image description here

https://docs.flutter.io/flutter/widgets/ClipRect-class.html

既然您花时间发布了示例代码和图片,我决定返回您的帮助。

关于Flutter 横幅不适合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55480316/

相关文章:

android - 将Admob添加到ScrollView -> AdBanner消失

android - 使用 Flutter 接收共享文件 Intent

flutter - 如何更改 TextField 中字符串输入的颜色?

authentication - 如何在Flutter中使用流转换器根据另一个输入字段的值验证输入

datetime - 如何在 Dart 和 Flutter 中将时间四舍五入到最近的四分之一小时?

ios - 将用户从一个应用程序内重定向到另一个应用程序

android - 使所有 fragment 位于横幅广告上方

android - Flutter,我可以为命名参数传递一个 Map

swift - 屏幕顶部的 Admob 横幅

ios - Flurry 广告 iOS 横幅被取消