In certain cases, it can be handy to update the design of an app when the userrotates their screen from portrait mode to landscape mode. For example, we maywant to show one item after the next in portrait mode, yet put those same itemsside-by-side in landscape mode.

In Flutter, we can build different layouts depending on a givenOrientation.In this example, we’ll build a list that displays 2 columns in portrait mode and3 columns in landscape mode.

Directions

  • Build a GridView with 2 columns
  • Use an OrientationBuilder to change the number of columns

1. Build a GridView with 2 columns

First, we’ll need a list of items to work with. Rather than using a normal list,we’ll want a list that displays items in a Grid. For now, we’ll create a gridwith 2 columns.

  1. GridView.count(
  2. // A list with 2 columns
  3. crossAxisCount: 2,
  4. // ...
  5. );

To learn more about working with GridViews, please see theCreating a grid list recipe.

2. Use an OrientationBuilder to change the number of columns

In order to determine the current Orientation, we can use theOrientationBuilderWidget. The OrientationBuilder calculates the current Orientation bycomparing the width and height available to the parent widget, and rebuildswhen the size of the parent changes.

Using the Orientation, we can build a list that displays 2 columns in portraitmode, or 3 columns in landscape mode.

  1. OrientationBuilder(
  2. builder: (context, orientation) {
  3. return GridView.count(
  4. // Create a grid with 2 columns in portrait mode, or 3 columns in
  5. // landscape mode.
  6. crossAxisCount: orientation == Orientation.portrait ? 2 : 3,
  7. );
  8. },
  9. );

Note: If you’re interested in the orientation of the screen, rather thanthe amount of space available to the parent, please useMediaQuery.of(context).orientation instead of an OrientationBuilder Widget.

Complete example

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6. @override
  7. Widget build(BuildContext context) {
  8. final appTitle = 'Orientation Demo';
  9. return MaterialApp(
  10. title: appTitle,
  11. home: OrientationList(
  12. title: appTitle,
  13. ),
  14. );
  15. }
  16. }
  17. class OrientationList extends StatelessWidget {
  18. final String title;
  19. OrientationList({Key key, this.title}) : super(key: key);
  20. @override
  21. Widget build(BuildContext context) {
  22. return Scaffold(
  23. appBar: AppBar(title: Text(title)),
  24. body: OrientationBuilder(
  25. builder: (context, orientation) {
  26. return GridView.count(
  27. // Create a grid with 2 columns in portrait mode, or 3 columns in
  28. // landscape mode.
  29. crossAxisCount: orientation == Orientation.portrait ? 2 : 3,
  30. // Generate 100 Widgets that display their index in the List
  31. children: List.generate(100, (index) {
  32. return Center(
  33. child: Text(
  34. 'Item $index',
  35. style: Theme.of(context).textTheme.headline,
  36. ),
  37. );
  38. }),
  39. );
  40. },
  41. ),
  42. );
  43. }
  44. }

Orientation Demo