Align Widget in Flutter

Align Widget is the widget that is used to align its child within itself and optionally sizes itself based on the child’s size. Align Widget is quite flexible and can change its size according to the size of its child.

Constructor of Align class:

Syntax:
Align({Key key, 
AlignmentGeometry alignment: Alignment.center,
double widthFactor, 
double heightFactor, 
Widget child})


Properties of Align Widget:

  • alignment:  It sets the alignment.
  • child:  The child widget in the tree.
  • hasCode: The hashcode for the object.
  • heightFactor: It sets its height to the child’s height multiplied by this heightFactor.
  • key: It is used to controlling how one widget replaces the other one.
  • runtimeType: It is a representation of runtime type.
  • widthFactor: It sets its width to the child’s width multiplied by this widthFactor.

Example 1: Aligning the text at the center of the container.

The main.dart file:



Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

import 'package:flutter/material.dart';
  
void main() => runApp(MyApp());
  
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'TextSpan',
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      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 Align Widget'),
            backgroundColor: Colors.green),
        body: Center(
            child: Container(
          height: 120.0,
          width: 120.0,
          color: Colors.blue[50],
          child: Align(
            alignment: Alignment.center,
            child: Text(
              "Geeky Text",
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
            ),
          ),
        )));
  }
}

chevron_right


Output:

align center

Example 2: Aligning the Image at the top right of the container.

The main.dart file:

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

import 'package:flutter/material.dart';
  
void main() => runApp(MyApp());
  
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'TextSpan',
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      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 Align Widget'),
            backgroundColor: Colors.green),
        body: Center(
            child: Container(
          height: 240.0,
          width: 240.0,
          color: Colors.green,
          child: Align(
              alignment: Alignment.topRight,
              child: Image.network(
                width: 100,
              )),
        )));
  }
}

chevron_right


Output:

align left




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.