0

How would one make a centered content container like this in Flutter?:

.container {
    margin-left: auto;
    margin-right: auto;
    width: 100%;
    max-width: 600px;
    background-color: red;
    height: 100vh;
}
<div class="container">
    Hello inside container
</div>

I have tried this, but it just overflows instead of fitting to the width of the screen once the application is smaller than the maxWidth.

import "package:flutter/material.dart";

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Theme.of(context).colorScheme.background,
        body: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: double.infinity,
              color: Colors.red,
              constraints: const BoxConstraints(maxWidth: 800),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: const [
                  Text(
                    "Hello inside container",
                    style: TextStyle(fontSize: 40),
                  ),
                ],
              ),
            ),
          ],
        ));
  }
}
Robiot
  • 112
  • 1
  • 2
  • 9

2 Answers2

0

You can use MediaQuery.of(context).size.width as width. It gives you the full width of screen. Also Limit max width of Container in Flutter have a look at this.

aoiTenshi
  • 547
  • 1
  • 6
  • 20
0

Try below code wrap your Column inside Expanded or Flexible

 Row(
    mainAxisAlignment: MainAxisAlignment.center,
    children: [
      Expanded(
        child: Container(
          width: double.infinity,
          color: Colors.red,
          constraints: const BoxConstraints(maxWidth: 800),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: const [
              Text(
                "Hello inside container",
                style: TextStyle(fontSize: 40),
              ),
            ],
          ),
        ),
      ),
    ],
  ),
Ravindra S. Patil
  • 11,757
  • 3
  • 13
  • 40