|
| 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 'dart:ui' show FlutterView; |
| 6 | + |
| 7 | +import 'framework.dart'; |
| 8 | +import 'lookup_boundary.dart'; |
| 9 | + |
| 10 | +/// Injects a [FlutterView] into the tree and makes it available to descendants |
| 11 | +/// within the same [LookupBoundary] via [View.of] and [View.maybeOf]. |
| 12 | +/// |
| 13 | +/// In a future version of Flutter, the functionality of this widget will be |
| 14 | +/// extended to actually bootstrap the render tree that is going to be rendered |
| 15 | +/// into the provided [view]. This will enable rendering content into multiple |
| 16 | +/// [FlutterView]s from a single widget tree. |
| 17 | +/// |
| 18 | +/// Each [FlutterView] can be associated with at most one [View] widget in the |
| 19 | +/// widget tree. Two or more [View] widgets configured with the same |
| 20 | +/// [FlutterView] must never exist within the same widget tree at the same time. |
| 21 | +/// Internally, this limitation is enforced by a [GlobalObjectKey] that derives |
| 22 | +/// its identity from the [view] provided to this widget. |
| 23 | +class View extends InheritedWidget { |
| 24 | + /// Injects the provided [view] into the widget tree. |
| 25 | + View({required this.view, required super.child}) : super(key: GlobalObjectKey(view)); |
| 26 | + |
| 27 | + /// The [FlutterView] to be injected into the tree. |
| 28 | + final FlutterView view; |
| 29 | + |
| 30 | + @override |
| 31 | + bool updateShouldNotify(View oldWidget) => view != oldWidget.view; |
| 32 | + |
| 33 | + /// Returns the [FlutterView] that the provided `context` will render into. |
| 34 | + /// |
| 35 | + /// Returns null if the `context` is not associated with a [FlutterView]. |
| 36 | + /// |
| 37 | + /// The method creates a dependency on the `context`, which will be informed |
| 38 | + /// when the identity of the [FlutterView] changes (i.e. the `context` is |
| 39 | + /// moved to render into a different [FlutterView] then before). The context |
| 40 | + /// will not be informed when the properties on the [FlutterView] itself |
| 41 | + /// change their values. To access the property values of a [FlutterView] it |
| 42 | + /// is best practise to use [MediaQuery.maybeOf] instead, which will ensure |
| 43 | + /// that the `context` is informed when the view properties change. |
| 44 | + /// |
| 45 | + /// See also: |
| 46 | + /// |
| 47 | + /// * [View.of], which throws instead of returning null if no [FlutterView] |
| 48 | + /// is found. |
| 49 | + static FlutterView? maybeOf(BuildContext context) { |
| 50 | + return LookupBoundary.dependOnInheritedWidgetOfExactType<View>(context)?.view; |
| 51 | + } |
| 52 | + |
| 53 | + /// Returns the [FlutterView] that the provided `context` will render into. |
| 54 | + /// |
| 55 | + /// Throws if the `context` is not associated with a [FlutterView]. |
| 56 | + /// |
| 57 | + /// The method creates a dependency on the `context`, which will be informed |
| 58 | + /// when the identity of the [FlutterView] changes (i.e. the `context` is |
| 59 | + /// moved to render into a different [FlutterView] then before). The context |
| 60 | + /// will not be informed when the properties on the [FlutterView] itself |
| 61 | + /// change their values. To access the property values of a [FlutterView] it |
| 62 | + /// is best practise to use [MediaQuery.of] instead, which will ensure that |
| 63 | + /// the `context` is informed when the view properties change. |
| 64 | + /// |
| 65 | + /// See also: |
| 66 | + /// |
| 67 | + /// * [View.maybeOf], which throws instead of returning null if no |
| 68 | + /// [FlutterView] is found. |
| 69 | + static FlutterView of(BuildContext context) { |
| 70 | + final FlutterView? result = maybeOf(context); |
| 71 | + assert(() { |
| 72 | + if (result == null) { |
| 73 | + final bool hiddenByBoundary = LookupBoundary.debugIsHidingAncestorWidgetOfExactType<View>(context); |
| 74 | + final List<DiagnosticsNode> information = <DiagnosticsNode>[ |
| 75 | + if (hiddenByBoundary) ...<DiagnosticsNode>[ |
| 76 | + ErrorSummary('View.of() was called with a context that does not have access to a View widget.'), |
| 77 | + ErrorDescription('The context provided to View.of() does have a View widget ancestor, but it is hidden by a LookupBoundary.'), |
| 78 | + ] else ...<DiagnosticsNode>[ |
| 79 | + ErrorSummary('View.of() was called with a context that does not contain a View widget.'), |
| 80 | + ErrorDescription('No View widget ancestor could be found starting from the context that was passed to View.of().'), |
| 81 | + ], |
| 82 | + ErrorDescription( |
| 83 | + 'The context used was:\n' |
| 84 | + ' $context', |
| 85 | + ), |
| 86 | + ErrorHint('This usually means that the provided context is not associated with a View.'), |
| 87 | + ]; |
| 88 | + throw FlutterError.fromParts(information); |
| 89 | + } |
| 90 | + return true; |
| 91 | + }()); |
| 92 | + return result!; |
| 93 | + } |
| 94 | +} |
0 commit comments