Open In App
Related Articles

Center widget in Flutter

Improve Article
Improve
Save Article
Save
Like Article
Like

Center widget comes built-in with flutter, it aligns its child widget to the center of the available space on the screen. The size of this widget will be as big as possible if the widthFactor and heightFactor properties are set to null and the dimensions are constrained. And in case the dimensions are not constrained and the widthFactor and HeightFactor are set to null then the Center widget takes the size of its child widget. Let’s understand this with the help of examples.

Constructor:

Syntax:
Center({Key key, 
double widthFactor, 
double heightFactor, 
Widget child})

Properties of Center Widget:

  • widthFactor: This property takes a double value as a parameter and it sets the Center widget’s width as the same as the child’s width multiplied by this factor. For example, if it is set to 3.0 then the Center widget will take three times the size of its child widget.
  • heightFactor: This property also takes in a double value as a parameter and it sets the Center widget’s height as the same as the child’s height multiplied by this factor.
  • child: This property takes in a widget as a parameter to be displayed inside the Center widget on the screen.
  • alignment: This property takes in AlignmentGeometry as the parameter value to determine how the child widget will be aligned with respect to the Center widget.

Example:

The main.dart file.

Dart




import 'package:flutter/material.dart';
 
void main() {
  runApp(MyApp());
}
 
class MyApp extends StatelessWidget {
  // This widget is
  //the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
      debugShowCheckedModeBanner: false,
    );
  }
}
 
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
 
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('GeeksforGeeks'),
        backgroundColor: Colors.green,
      ),
      body: Center(
        // heightFactor: 3,
        // widthFactor: 0.8,
        child: Container(
          color: Colors.green,
          child: Text(
            'Center Widget',
            textScaleFactor: 3,
            style: TextStyle(color: Colors.white),
          ),
        ),
      ),
    );
  }
}


Output:

When the above code is executed, the output is shown below:

center align widget

If the properties are defined as below:

heightFactor: 3,





The following design changes can be observed:

center top align widget

If the properties are defined as below:

widthFactor: 1,





The following design changes can be observed:

If the properties are defined as below:

heightFactor: 3,
widthFactor: 0.8,





The following design changes can be observed:

Explanation:

  • Create Center widget with its child as Container.
  • Set heightFactor if needed.
  • Set widthFactor if needed.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 16 Nov, 2020
Like Article
Save Article
Similar Reads