| 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import 'basic.dart'; |
| 6 | import 'framework.dart'; |
| 7 | import 'layout_builder.dart'; |
| 8 | import 'media_query.dart'; |
| 9 | |
| 10 | /// Signature for a function that builds a widget given an [Orientation]. |
| 11 | /// |
| 12 | /// Used by [OrientationBuilder.builder]. |
| 13 | typedef OrientationWidgetBuilder = Widget Function(BuildContext context, Orientation orientation); |
| 14 | |
| 15 | /// Builds a widget tree that can depend on the parent widget's orientation |
| 16 | /// (distinct from the device orientation). |
| 17 | /// |
| 18 | /// See also: |
| 19 | /// |
| 20 | /// * [LayoutBuilder], which exposes the complete constraints, not just the |
| 21 | /// orientation. |
| 22 | /// * [CustomSingleChildLayout], which positions its child during layout. |
| 23 | /// * [CustomMultiChildLayout], with which you can define the precise layout |
| 24 | /// of a list of children during the layout phase. |
| 25 | /// * [MediaQueryData.orientation], which exposes whether the device is in |
| 26 | /// landscape or portrait mode. |
| 27 | class OrientationBuilder extends StatelessWidget { |
| 28 | /// Creates an orientation builder. |
| 29 | const OrientationBuilder({super.key, required this.builder}); |
| 30 | |
| 31 | /// Builds the widgets below this widget given this widget's orientation. |
| 32 | /// |
| 33 | /// A widget's orientation is a factor of its width relative to its |
| 34 | /// height. For example, a [Column] widget will have a landscape orientation |
| 35 | /// if its width exceeds its height, even though it displays its children in |
| 36 | /// a vertical array. |
| 37 | final OrientationWidgetBuilder builder; |
| 38 | |
| 39 | Widget _buildWithConstraints(BuildContext context, BoxConstraints constraints) { |
| 40 | // If the constraints are fully unbounded (i.e., maxWidth and maxHeight are |
| 41 | // both infinite), we prefer Orientation.portrait because its more common to |
| 42 | // scroll vertically then horizontally. |
| 43 | final Orientation orientation = constraints.maxWidth > constraints.maxHeight |
| 44 | ? Orientation.landscape |
| 45 | : Orientation.portrait; |
| 46 | return builder(context, orientation); |
| 47 | } |
| 48 | |
| 49 | @override |
| 50 | Widget build(BuildContext context) { |
| 51 | return LayoutBuilder(builder: _buildWithConstraints); |
| 52 | } |
| 53 | } |
| 54 | |