The Align widget in Flutter is used to position a single child within itself and can be thought of as a more flexible version of Center. It allows you to align a child widget at any position within its parent, specifying alignment using an Alignment widget.
Here is an example of using Align and some of its attributes:
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('Align Widget Example'),
),
body: Align(
alignment: Alignment(0.5, 0.5), // Align to the center of the parent
child: Container(
width: 100.0,
height: 100.0,
color: Colors.blue,
child: Text(
'Aligned Text',
style: TextStyle(color: Colors.white),
),
),
),
),
);
}
}
Common Attributes:
alignment (Alignment): Specifies the position within the parent where the child should be aligned. You can use the Alignment class to define an X and Y coordinate, where (0.0, 0.0) is the top-left corner, (1.0, 1.0) is the bottom-right corner, and (0.5, 0.5) is the center.
Align( alignment: Alignment(0.5, 0.5), // Center child: // Your child widget here )
widthFactor and heightFactor: These attributes allow you to scale the size of the child widget relative to the size of the Align widget.
Align( alignment: Alignment.center, widthFactor: 2.0, heightFactor: 2.0, child: // Your child widget here )
child: The child widget that you want to position within the Align widget.
Align( alignment: Alignment.bottomRight, child: Container( width: 50.0, height: 50.0, color: Colors.blue, child: // Your content here ), )
width and height: If you don't want to use widthFactor and heightFactor, you can set the absolute width and height of the child.
Align( alignment: Alignment.center, width: 100.0, height: 100.0, child: // Your child widget here )
The Align widget is useful when you need more control over the positioning of a child widget within its parent. The alignment property, in particular, is powerful and allows for precise placement.