Using Stack and FractionallySizedBox

Did you know?

Flutter offers many ways to create custom layouts that can’t be expressed with Row and Column. πŸ‘

For example, here's how to overlay multiple widgets inside a Stack and constrain their size and position with Positioned and FractionallySizedBox. πŸ‘‡

Using Stack and FractionallySizedBox

Example Code

const n = 4; // * Aspect ratio = 1 yields a square return AspectRatio( aspectRatio: 1, // * Stack the squares on top of each other child: Stack( // * Generate n squares children: List.generate(n, (index) { // * Material colors have shades from 100 to 900 final color = Colors.indigo[(index + 2) * 100]!; // * Fill the entire surface return Positioned.fill( // * Size child to a fraction of the available space child: FractionallySizedBox( // * Pick width and height between 0 and 1 widthFactor: 1 - index / n, heightFactor: 1 - index / n, // * Choose the alignment of the child alignment: Alignment.topRight, // * Just a colored box child: ColoredBox(color: color), ), ); }), ), );

Happy coding!

Want More?

Invest in yourself with my high-quality Flutter courses.

Flutter In Production

Flutter In Production

Learn about flavors, environments, error monitoring, analytics, release management, CI/CD, and finally ship your Flutter apps to the stores. πŸš€

Flutter Foundations Course

Flutter Foundations Course

Learn about State Management, App Architecture, Navigation, Testing, and much more by building a Flutter eCommerce app on iOS, Android, and web.

Flutter & Firebase Masterclass

Flutter & Firebase Masterclass

Learn about Firebase Auth, Cloud Firestore, Cloud Functions, Stripe payments, and much more by building a full-stack eCommerce app with Flutter & Firebase.

The Complete Dart Developer Guide

The Complete Dart Developer Guide

Learn Dart Programming in depth. Includes: basic to advanced topics, exercises, and projects. Last updated to Dart 2.15.

Flutter Animations Masterclass

Flutter Animations Masterclass

Master Flutter animations and build a completely custom habit tracking application.