InheritedTheme class

An InheritedWidget that defines visual properties like colors and text styles, which the child's subtree depends on.

The wrap method is used by captureAll to construct a widget that will wrap a child in all of the inherited themes which are present in a build context but are not present in the context that the returned widget is eventually built in.

A widget that's shown in a different context from the one it's built in, like the contents of a new route or an overlay, will be able to depend on inherited widget ancestors of the context it's built in.

This example demonstrates how InheritedTheme.captureAll() can be used to wrap the contents of a new route with the inherited themes that are present when the route is built - but are not present when route is actually shown.

If the same code is run without InheritedTheme.captureAll(), the new route's Text widget will inherit the "something must be wrong" fallback text style, rather than the default text style defined in MyApp. <dartdoc-html>dc7f94d437ffc851e6dd7fa137b5791237e81223</dartdoc-html> This example demonstrates howInheritedTheme.captureAll()` can be used to wrap the contents of a new route with the inherited themes that are present when the route is built - but are not present when route is actually shown.

If the same code is run without `InheritedTheme.captureAll(), the new route's Text widget will inherit the "something must be wrong" fallback text style, rather than the default text style defined in MyApp.

import 'package:flutter/material.dart';

// ...

void main() {
  runApp(MyApp());
}

// ...

class MyAppBody extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.of(context).push(
          MaterialPageRoute(
            builder: (BuildContext _) {
              // InheritedTheme.captureAll() saves references to themes that
              // are found above the context provided to this widget's build
              // method, notably the DefaultTextStyle defined in MyApp. The
              // context passed to the MaterialPageRoute's builder is not used,
              // because its ancestors are above MyApp's home.
              return InheritedTheme.captureAll(context, Container(
                alignment: Alignment.center,
                color: Theme.of(context).colorScheme.surface,
                child: Text('Hello World'),
              ));
            },
          ),
        );
      },
      child: Center(child: Text('Tap Here')),
    );
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        // Override the DefaultTextStyle defined by the Scaffold.
        // Descendant widgets will inherit this big blue text style.
        body: DefaultTextStyle(
          style: TextStyle(fontSize: 48, color: Colors.blue),
          child: MyAppBody(),
        ),
      ),
    );
  }
}

Inheritance
Implementers

Constructors

InheritedTheme({Key key, @required Widget child})
Abstract const constructor. This constructor enables subclasses to provide const constructors so that they can be used in const expressions.
const

Properties

child Widget
The widget below this widget in the tree. [...]
final, inherited
hashCode int
The hash code for this object. [...]
@nonVirtual, read-only, inherited
key Key
Controls how one widget replaces another widget in the tree. [...]
final, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

wrap(BuildContext context, Widget child) Widget
Return a copy of this inherited theme with the specified child. [...]
createElement() InheritedElement
Inflates this configuration to a concrete instance. [...]
inherited
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
@protected, inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node. [...]
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toDiagnosticsNode({String name, DiagnosticsTreeStyle style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel: DiagnosticLevel.info}) String
Returns a string representation of this object.
inherited
toStringDeep({String prefixLineOne: '', String prefixOtherLines, DiagnosticLevel minLevel: DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner: ', ', DiagnosticLevel minLevel: DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited
updateShouldNotify(covariant InheritedWidget oldWidget) bool
Whether the framework should notify widgets that inherit from this widget. [...]
@protected, inherited

Operators

operator ==(Object other) bool
The equality operator. [...]
@nonVirtual, inherited

Static Methods

captureAll(BuildContext context, Widget child) Widget
Returns a widget that will wrap child in all of the inherited themes which are visible from context.