skip to Main Content

I’m brand new to Dart and Flutter. I’m trying to overflow some Widget from their parent in a Stack widget. I ended up with something that looks like what I want, but with warnings: A RenderConstraintsTransformBox overflowed by 50 pixels on the left. and A RenderConstraintsTransformBox overflowed by 50 pixels on the right.

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    double size = MediaQuery.of(context).size.width;

    return Scaffold(
      body: Center(
        child: SizedBox(
          width: size,
          height: size,
          child: Stack(
            children: <Widget>[
              Container(
                color: Colors.green,
              ),
              Container(
                alignment: Alignment.center,
                child: buildElement(),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget buildElement() {
    return SizedBox(
      height: 50,
      width: 50,
      child: Stack(
        children: <Widget>[
          Container(
            alignment: Alignment.center,
            color: Colors.red,
          ),
          Container(
            alignment: Alignment.topLeft,
            child: UnconstrainedBox(
              alignment: Alignment.topLeft,
              child: FractionalTranslation(
                translation: const Offset(-0.5, -0.5),
                child: buildSomeComponent(),
              ),
            ),
          ),
          Container(
            alignment: Alignment.bottomRight,
            child: UnconstrainedBox(
              alignment: Alignment.bottomRight,
              child: FractionalTranslation(
                translation: const Offset(0.5, 0.5),
                child: buildSomeComponent(),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget buildSomeComponent() {
    return Container(
      color: Colors.cyan,
      child: const Text(
        '[::::::::::::::::::::]',
        overflow: TextOverflow.visible,
        softWrap: false,
        textAlign: TextAlign.center,
      ),
    );
  }
}

It looks looks this:
enter image description here

This is the expected result, except the warnings 🥲

The wanted result as shown is the red box being the "main" content, and the cyan boxes are kind-of tooltips.

I tried with some OverflowBox placed at different levels (except under UnconstrainedBox which messes up with the app) or Flexible around Text, but it has no effect on the caught exceptions.

Many thanks in advance for your help!

2

Answers


  1. I think it is overflowing because you are trying to put Container from buildSomeComponent() and the width is bigger than its parent SizedBox() of width 50. How about setting Stack as parent and set the size of the red box in the Container instead?

    Widget buildElement() {
      return Stack(
          children: <Widget>[
            Container(
              height: 50,
              width: 50,
              alignment: Alignment.center,
              color: Colors.red,
            ),
            Container(
              alignment: Alignment.topLeft,
              child: UnconstrainedBox(
                alignment: Alignment.topLeft,
                child: FractionalTranslation(
                  translation: const Offset(-0.5, -0.5),
                  child: buildSomeComponent(),
                ),
              ),
            ),
            Container(
              alignment: Alignment.bottomRight,
              child: UnconstrainedBox(
                alignment: Alignment.bottomRight,
                child: FractionalTranslation(
                  translation: const Offset(0.5, 0.5),
                  child: buildSomeComponent(),
                ),
              ),
            ),
          ],
        ),
      );
    }
    
    Login or Signup to reply.
  2. You can remove UnconstrainedBox, it will be visible (overflow text/part) by default on Stack.

      Widget buildElement() {
        return SizedBox(
          height: 50,
          width: 50,
          child: Stack(
            children: <Widget>[
              Container(
                alignment: Alignment.center,
                color: Colors.red,
              ),
              Container(
                alignment: Alignment.topLeft,
                child: FractionalTranslation(
                  translation: const Offset(-0.5, -0.5),
                  child: buildSomeComponent(),
                ),
              ),
              Container(
                alignment: Alignment.bottomRight,
                child: FractionalTranslation(
                  translation: const Offset(0.5, 0.5),
                  child: buildSomeComponent(),
                ),
              ),
            ],
          ),
        );
      }
    

    You can find more about Stack, Align and Positioned widgets.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search