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
5import 'basic.dart';
6import 'framework.dart';
7import 'layout_builder.dart';
8import 'media_query.dart';
9
10/// Signature for a function that builds a widget given an [Orientation].
11///
12/// Used by [OrientationBuilder.builder].
13typedef 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.
27class OrientationBuilder extends StatelessWidget {
28 /// Creates an orientation builder.
29 const OrientationBuilder({
30 super.key,
31 required this.builder,
32 });
33
34 /// Builds the widgets below this widget given this widget's orientation.
35 ///
36 /// A widget's orientation is a factor of its width relative to its
37 /// height. For example, a [Column] widget will have a landscape orientation
38 /// if its width exceeds its height, even though it displays its children in
39 /// a vertical array.
40 final OrientationWidgetBuilder builder;
41
42 Widget _buildWithConstraints(BuildContext context, BoxConstraints constraints) {
43 // If the constraints are fully unbounded (i.e., maxWidth and maxHeight are
44 // both infinite), we prefer Orientation.portrait because its more common to
45 // scroll vertically then horizontally.
46 final Orientation orientation = constraints.maxWidth > constraints.maxHeight ? Orientation.landscape : Orientation.portrait;
47 return builder(context, orientation);
48 }
49
50 @override
51 Widget build(BuildContext context) {
52 return LayoutBuilder(builder: _buildWithConstraints);
53 }
54}
55