I remember when I started using Flutter, there were a lot of use cases in terms of alignment, where I didn’t know exactly how to approach them. I think alignment of widgets is one of the most important parts in Flutter UI (as it is in web development) and Flutter made it terribly easy to do so, given that you know the right widgets/approach to use.
In this series, I won’t go very deep into the technical details of each widget that I describe but rather will give practical examples with use cases.
How to align single widgets in Flutter
For this use case, we can use the Align widget which lets us align our widget in 9 places in the parent widget:
topLeft
topCenter
topRight
centerLeft
center
centerRight
bottomLeft
bottomCenter
bottomRight
We just need to define what alignment we want and what widget we want to align. In this case, we choose for Alignment.topCenter and we are aligning a Container with a Text as the child.
Or we could try Alignment.centerLeft, which will look like this:
Alignment.centerLeft
You can try other variants of alignment and see what happens. I think you get the idea. For Alignment.center, there is a shortcut available and that will be a bridge to our next use case.
How to center a widget horizontally and vertically in Flutter
There is a very useful widget called Center that takes a child widget and aligns it in the center of the parent widget. This works exactly the same as the Align widget with Alignment.center parameter.
Sometimes one wants to build a completely fullscreen experience with or without an image background. There are 2 ways to do this:
Without using an AppBar and use SafeArea to keep our UI elements inside the ‘safe’ area on our page. The downside of this method is that we cannot use actions in our AppBar and also back button and navigation are not done automatically for us (in both cases we would need to create custom ones). This method is simple and works well for example for a landing page, but not so much for a detail page where actions and navigation are very common.
Make AppBar transparent. This method requires a bit more complex widget tree but is ideal if we want to keep our AppBar and the behavior that comes with it.
I’m going to explain both methods using a fullscreen background image.
I always use Unsplash for free to use images, because it’s an awesome service. You should definitely give it a try.
Create a full-screen page without AppBar
What we do is that we won’t add an app bar to our Scaffold and make SafeArea the root of our body, to avoid any intrusion by the Operating System. We then remove the bottom and top margin of SafeArea to make it truly fullscreen. Finally, we will use BoxDecoration and NetworkImage to show a fullscreen image.
@override
Widget build(BuildContext context) {
return Scaffold(
// we won't use appBar here
// appBar: AppBar(
// title: Text("Full screen page"),
// backgroundColor: Colors.purple,
// ),
// might need to set this flag,
//resizeToAvoidBottomInset: false,
body: SafeArea(
bottom: false,
top: false,
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage('https://images.unsplash.com/photo-1517030330234-94c4fb948ebc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1275&q=80'),
fit: BoxFit.cover,
),
),
child: Center(
child: Text('mrflutter.com',
style: TextStyle(
color: Colors.white,
fontSize: 30,
),
),
),
),
),
);
}
This is how it will look on the device:
Fullscreen page with background image
Create a full-screen page with transparent AppBar
With this method, we will make AppBar part of the body and use Stack and Positioned to put AppBar on top of the rest of the body. This way we can actually make the color of our AppBar transparent, as there’s a widget “under” it. Also, to make it look flat, we remove the elevation from the AppBar.
As far as I know, these are the 2 way nicest ways of achieving fullscreen pages with or without transparent AppBar. You can see which method suits your needs better.
In my previous post, I went through 2 types of FloatingActionButtons and explained how to position them on a page. As promised in this post I’m going to show you how to create a so-called Speed Dial widget using FABs and animation. It’s actually quite easy to do. I’m going to show you step-by-step how to achieve this.
To be able to re-use our Speed Dial widget and don’t pollute our page we should create a StatefulWidget. I have explained Stateless and StatefulWidgets in one of my previous posts. But to recap, we need a StatefulWidget, because we need to change our state. In this case, we need to show and hide FABs and keep track of our animation.
class AnimatedFab extends StatefulWidget {
@override
State<StatefulWidget> createState() => AnimatedFabState();
}
class AnimatedFabState extends State<AnimatedFab>
with SingleTickerProviderStateMixin {
@override
void initState() {
// We will initialize our animation here
super.initState();
}
@override
void dispose() {
// Here we will dispose of our AnimationController
super.dispose();
}
@override
Widget build(BuildContext context) {
// This is where our FABs will be rendered
return null;
}
}
Now that we have our StatefulWidget ready, we will start defining our animation and our FABs. We need the following things to setup:
AnimationController: as the name says, to control our animation
Animation<Color>: this is needed if we want to animate the color of the FAB too
Animation<double>: this is the progress needed to animate the icon. This is useful to animate icon and change it when our Speed Dial menu is open.
Curve: we need this class to define how our animation changes over time
isOpened: a flag to keep track of whether or not our menu is open
Notice that AnimatedIcon is a very useful widget that works similar to a normal icon, but shows the animation based on the progress. The list of available animated icons is unfortunately limited. Let’s see how it looks now.
The main FAB is now nicely animated. What we need to do now is to add our menu items (other FABs and according to Google you should not add more than 6 of them, 7 including the menu FAB itself).
We will use translation to open the menu upwards and keep in mind the distance between the icons. So let make some changes to the code.
It would be nice to make the widget completely re-usable by adding params to it. For example, it would be nice to be able to define what menu items we want to add to the Speed Dial menu instead of having them hard-coded. It would, however, be a better idea if I explain that later as a separate subject.