Flutter Centered SizedBox won't center

I want to center a SizedBox that I have created, so I wrapped it with a Center widget, like so:

import 'package:flutter/material.dart';

void main() {
  runApp(
    const MaterialApp(
      title: 'My app',
      home: SafeArea(
        child: MyScaffold(),
      ),
    ),
  );
}

class MyScaffold extends StatelessWidget {
  const MyScaffold({super.key});
  @override
  Widget build(BuildContext context) {
    return Material(
      child: Column(
        children: [
          TextInputWidget(),
        ],
      ),
    );
  }
}

class TextInputWidget extends StatelessWidget {
  const TextInputWidget({super.key});
  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Center(
          child: SizedBox(
            width: 200.0,
            child: TextField(
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                hintText: 'Enter a search term',
              ),
            ),
          ),
        ),
      ],
    );
  }
}

But unfortunately, it does not work, as you can see on the picture below, the SizedBox is still on the left side on the window:

enter image description here

I have been looking for an answer, but did not find one yet.

Any solution for this?

Thanks

>Solution :

According to the Center documentation:

This widget will be as big as possible if its dimensions are constrained and widthFactor and heightFactor are null. If a dimension is unconstrained and the corresponding size factor is null then the widget will match its child’s size in that dimension.

In a Row, children are unconstrained on the primary axis. To make a child widget such as the Center take up all the available space, wrap it in an Expanded widget.

(The Row API documentation has a detailed explanation of its layout mechanics. It’s a very nice read.)

class TextInputWidget extends StatelessWidget {
  const TextInputWidget({super.key});
  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: Center(
            child: SizedBox(
              width: 200.0,
              child: TextField(
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  hintText: 'Enter a search term',
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}

Alternatively, you could set the Row alignment using mainAxisAlignment instead.

class TextInputWidget extends StatelessWidget {
  const TextInputWidget({super.key});
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        SizedBox(
          width: 200.0,
          child: TextField(
            decoration: InputDecoration(
              border: OutlineInputBorder(),
              hintText: 'Enter a search term',
            ),
          ),
        ),
      ],
    );
  }
}

Leave a Reply