Center widget in Flutter - GeeksforGeeks

16 Nov, 2020
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.
Syntax:
Center({Key key, 
double widthFactor, 
double heightFactor, 
Widget child})


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:
If the properties are defined as below:
heightFactor: 3,






The following design changes can be observed:
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:
Create Center widget with its child as Container.
Set heightFactor if needed.
Set widthFactor if needed.
ankit_kumar_
android
Flutter
Flutter-widgets
Dart
Flutter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Flutter - DropDownButton Widget
Flutter - Custom Bottom Navigation Bar
Flutter - Checkbox Widget
Flutter - BoxShadow Widget
ListView Class in Flutter
Flutter - DropDownButton Widget
Flutter - Custom Bottom Navigation Bar
Flutter Tutorial
Flutter - Checkbox Widget
Flutter - BoxShadow Widget