debug.dart 1.83 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/widgets.dart';

import 'material.dart';

9
/// Asserts that the given context has a [Material] ancestor.
10
///
11 12 13
/// Used by many material design widgets to make sure that they are
/// only used in contexts where they can print ink onto some material.
///
14
/// To call this function, use the following pattern, typically in the
15
/// relevant Widget's build method:
16 17 18 19 20 21
///
/// ```dart
/// assert(debugCheckHasMaterial(context));
/// ```
///
/// Does nothing if asserts are disabled. Always returns true.
22 23
bool debugCheckHasMaterial(BuildContext context) {
  assert(() {
Hixie's avatar
Hixie committed
24
    if (context.widget is! Material && context.ancestorWidgetOfExactType(Material) == null) {
25
      final Element element = context;
26
      throw new FlutterError(
27
        'No Material widget found.\n'
28
        '${context.widget.runtimeType} widgets require a Material widget ancestor.\n'
29 30 31 32 33
        'In material design, most widgets are conceptually "printed" on a sheet of material. In Flutter\'s material library, '
        'that material is represented by the Material widget. It is the Material widget that renders ink splashes, for instance. '
        'Because of this, many material library widgets require that there be a Material widget in the tree above them.\n'
        'To introduce a Material widget, you can either directly include one, or use a widget that contains Material itself, '
        'such as a Card, Dialog, Drawer, or Scaffold.\n'
34
        'The specific widget that could not find a Material ancestor was:\n'
35
        '  ${context.widget}\n'
36
        'The ownership chain for the affected widget is:\n'
37
        '  ${element.debugGetCreatorChain(10)}'
Hixie's avatar
Hixie committed
38 39 40
      );
    }
    return true;
41
  }());
42 43
  return true;
}