— Flutter, Widget, Align — 2 min read
The Align
widget is a powerful tool in Flutter that allows you to control the alignment and positioning of child widgets within a parent widget. It provides a simple and intuitive way to position widgets based on different alignment options. In this article, we will explore the Align widget and its usage with some examples.
To use the Align widget, you need to have Flutter installed and set up in your development environment. If you haven't done so already, head over to the Flutter website and follow the installation instructions for your operating system.
Once you have Flutter installed, you can create a new Flutter project or open an existing one. To create a new Flutter project, use the following command:
1flutter create align_example2cd align_example
Now, let's dive into the Align widget and see how it works. If you can't be bothered setting up a new project you could try using DartPad instead.
The Align widget has two primary properties: alignment
and child
. The alignment
property determines how the child widget should be positioned within the parent widget, while the child
property defines the widget that will be aligned.
Here's an example of using the Align widget to center a child widget horizontally and vertically within its parent:
1import 'package:flutter/material.dart';2
3void main() => runApp(AlignExample());4
5class AlignExample extends StatelessWidget {6 @override7 Widget build(BuildContext context) {8 return MaterialApp(9 home: Scaffold(10 appBar: AppBar(11 title: Text('Align Example'),12 ),13 body: Center(14 child: Align(15 alignment: Alignment.center,16 child: Container(17 width: 200,18 height: 200,19 color: Colors.blue,20 child: Text(21 'Centered Widget',22 style: TextStyle(23 color: Colors.white,24 fontSize: 24,25 ),26 ),27 ),28 ),29 ),30 ),31 );32 }33}
In this example, we've created an Align widget with alignment: Alignment.center
and a child widget that is a Container with a blue background color and white text. The Align widget ensures that the child widget is centered both horizontally and vertically within its parent.
The Align widget provides various alignment options through the alignment
property. You can use predefined alignment constants from the Alignment
class or define your custom alignment using Alignment(x, y)
, where x
and y
are values between -1.0 and 1.0.
Let's take a look at an example that demonstrates how to use custom alignments:
1import 'package:flutter/material.dart';2
3void main() => runApp(CustomAlignmentsExample());4
5class CustomAlignmentsExample extends StatelessWidget {6 @override7 Widget build(BuildContext context) {8 return MaterialApp(9 home: Scaffold(10 appBar: AppBar(11 title: Text('Custom Alignments Example'),12 ),13 body: Align(14 alignment: Alignment(-0.5, 0.8),15 child: Container(16 width: 150,17 height: 150,18 color: Colors.green,19 child: Text(20 'Custom Alignment',21 style: TextStyle(22 color: Colors.white,23 fontSize: 20,24 ),25 ),26 ),27 ),28 ),29 );30 }31}
In this example, we've set alignment: Alignment(-0.5, 0.8)
, which positions the child widget at -0.5 times the width and 0.8 times the height of the parent widget. This results in a custom alignment that is slightly offset from the center towards the bottom-right corner.
Another way to define custom alignments is by using FractionalOffset
values. The FractionalOffset
class allows you to specify alignments based on fractions of the parent widget's size.
Here's an example that demonstrates the usage of FractionalOffset
:
1import 'package:flutter/material.dart';2
3void main() => runApp(FractionalOffsetExample());4
5class FractionalOffsetExample extends StatelessWidget {6 @override7 Widget build(BuildContext context) {8 return MaterialApp(9 home: Scaffold(10 appBar: AppBar(11 title: Text('FractionalOffset Example'),12 ),13 body: Align(14 alignment: FractionalOffset(0.2, 0.6),15 child: Container(16 width: 120,17 height: 120,18 color: Colors.orange,19 child: Text(20 'Fractional Offset',21 style: TextStyle(22 color: Colors.white,23 fontSize: 16,24 ),25 ),26 ),27 ),28 ),29 );30 }31}
In this example, we've set alignment: FractionalOffset(0.2, 0.6)
, which aligns the child widget at 20% of the parent's width and 60% of the parent's height. The resulting alignment is slightly offset towards the top-right corner.
The Align widget in Flutter provides a flexible and convenient way to align and position child widgets within a parent widget. By adjusting the alignment property, you can control the positioning of widgets with ease. In this article, we explored the basic usage of the Align widget, as well as how to use custom alignments and FractionalOffsets.
Now that you have a good understanding of the Align widget, feel free to experiment and explore its capabilities in your own Flutter projects. Happy coding!