CenterWidget

The Center widget in Flutter is a simple and commonly used layout widget that allows you to center its child within the available space. It's particularly useful when you want to center a single child, such as a piece of text, an image, or another widget, within a parent container or screen.

Here's an explanation of the Center widget and how to use it:

What the Center Widget Does:

The Center widget does precisely what its name suggests: it centers its child both horizontally and vertically within the available space. It's a great way to ensure that your content is positioned in the middle of the parent container or screen, regardless of the parent's size.

When to Use the Center Widget:

You can use the Center widget in various scenarios, such as:

  1. Centering Text: When you want to center a text message on the screen or within a container.

  2. Centering Images: If you have an image or icon that needs to be positioned in the center of a container or screen.

  3. Centering Widgets: When you have any other widget (e.g., buttons, icons, custom widgets) that you want to center.

  4. Screen Loading Indicators: It's often used to center a loading indicator (e.g., a CircularProgressIndicator) while data is being fetched or processed.

How to Use the Center Widget:

Here's how you can use the Center widget in Flutter:

  1. Import the Flutter package in your Dart file:

    import 'package:flutter/material.dart';
  2. Inside your Flutter widget tree, wrap the widget you want to center with a Center widget.

    Center(
      child: Text(
        'Centered Text',
        style: TextStyle(fontSize: 24.0),
      ),
    )

    In this example, we've centered a text widget within a Center widget.

Example Use Case:

Here's an example of using the Center widget to center a text message within the body of a Flutter app:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Center Widget Example'),
        ),
        body: Center(
          child: Text(
            'This text is centered.',
            style: TextStyle(fontSize: 24.0),
          ),
        ),
      ),
    );
  }
}

In this example, the Text widget is centered both horizontally and vertically within the available space in the app's body, thanks to the Center widget.

By using the Center widget, you can ensure that your content is consistently centered, regardless of the screen size or the parent container's dimensions.

Video: Youtube

Last updated