Flutter: Wrap container around Stack with Positioned children

2.6k Views Asked by At

I want to center a Stack widget that includes Positioned children. The size of the Stack is not known before rendering.

Unfortunately, the Stack seems to have an unlimited size (as seen, when wrapping a container around it).

import 'package:flutter/material.dart';

class StackTest extends StatelessWidget {
  const StackTest({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: Container(
              decoration: BoxDecoration(
                border: Border.all(
                  color: Colors.yellow,
                  width: 5.0,
                ),
              ),
              child: buildStack()),
        ),
      ),
    );
  }

  Widget buildStack() {
    return Stack(
      clipBehavior: Clip.none,
      children: [
        Container(),
        Positioned(
          top: 0,
          left: 0,
          child: Container(
            height: 50,
            width: 50,
            color: Colors.red,
          ),
        ),
        Positioned(
          top: 50,
          left: 50,
          child: Container(
            height: 50,
            width: 50,
            color: Colors.blue,
          ),
        ),
      ],
    );
  }
}

Final Question: How can I wrap a container around a stack including positioned children and center it?

Here are some similar questions, that weren't helpful for me:

Flutter - Positioned Widget in Stack causing

Stack makes infinite height if only have Positioned children

2

There are 2 best solutions below

0
On BEST ANSWER

Stack needs a reference point. So when there are only Positioned items in it, it doesn't know where to start and how big it is.

See RenderStack documentation: [...] If there are no non-positioned children, the stack becomes as large as possible [...]

This problem was also described here.

Solution:

  1. One way is to place a non-positioned item in the stack, which will become the reference point of the stack (incl. the positioned items). This could be a container with a known size.
  2. Another option is to set the Positioned with regard to global values, like the screen size (see Priyansu Choudhury answer)
2
On

A custom solution:

main.dart

import 'package:flutter/material.dart';

void main() {
  runApp(
      const MaterialApp(debugShowCheckedModeBanner: false, home: StackTest()));
}

class StackTest extends StatelessWidget {
  const StackTest({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: buildStack(context),
    );
  }

  Widget buildStack(BuildContext context) {
    double containerSize = 50;
    double borderSize = 5;
    return Stack(
      clipBehavior: Clip.none,
      children: [
        Positioned(
          top: MediaQuery.of(context).size.height / 2 -
              (containerSize + borderSize),
          left: MediaQuery.of(context).size.width / 2 -
              (containerSize + borderSize),
          child: Container(
            height: 2 * containerSize + 2 * borderSize,
            width: 2 * containerSize + 2 * borderSize,
            decoration: BoxDecoration(
              border: Border.all(
                color: Colors.yellow,
                width: borderSize,
              ),
            ),
          ),
        ),
        Positioned(
          top: MediaQuery.of(context).size.height / 2 - containerSize,
          left: MediaQuery.of(context).size.width / 2 - containerSize,
          child: Center(
            child: Container(
              height: containerSize,
              width: containerSize,
              color: Colors.red,
            ),
          ),
        ),
        Positioned(
          top: MediaQuery.of(context).size.height / 2,
          left: MediaQuery.of(context).size.width / 2,
          child: Center(
            child: Container(
              height: containerSize,
              width: containerSize,
              color: Colors.blue,
            ),
          ),
        ),
      ],
    );
  }
}