Flutter 横幅不适合

Tom*_*Tom 4 banner flutter

我有横幅小部件的问题。为了演示它,我制作了一些演示代码。我想要的是在容器的右上角有一个横幅,但它很丑陋,因为它溢出了它的孩子(请参阅附图)。

在此处输入图片说明

这是我的代码:

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!"),),
          ),
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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

非常感谢!

use*_*442 16

只需将 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!"),),
            ),
          ),
        ),
      ),
    );
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

反转容器和横幅

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!"),),
            ),
          ),
        ),
      ),
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

将 ClipRect 添加到反转容器和横幅

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!"),),
              ),
            ),
          ),
        ),
      ),
    );
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

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

由于您花时间发布了示例代码和图像,因此我决定回报您的帮助。


die*_*per 5

包裹您的Banner内部ClipRect小部件并删除边距:

            ClipRect(
                      child: Banner(
                        message: "hello",
                        location: BannerLocation.topEnd,
                        color: Colors.red,
                        child: Container(
                          color: Colors.yellow,
                          height: 100,
                          child: Center(
                            child: Text("Hello, banner!"),
                          ),
                        ),
                      ),
                    ),
Run Code Online (Sandbox Code Playgroud)