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({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 =
44 constraints.maxWidth > constraints.maxHeight ? Orientation.landscape : Orientation.portrait;
45 return builder(context, orientation);
46 }
47
48 @override
49 Widget build(BuildContext context) {
50 return LayoutBuilder(builder: _buildWithConstraints);
51 }
52}
53

Provided by KDAB

Privacy Policy
Learn more about Flutter for embedded and desktop on industrialflutter.com