primary_scroll_controller.dart 2.18 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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/foundation.dart';

import 'framework.dart';
import 'scroll_controller.dart';

10 11 12 13 14 15 16 17 18
/// Associates a [ScrollController] with a subtree.
///
/// When a [ScrollView] has [ScrollView.primary] set to true and is not given
/// an explicit [ScrollController], the [ScrollView] uses [of] to find the
/// [ScrollController] associated with its subtree.
///
/// This mechanism can be used to provide default behavior for scroll views in a
/// subtree. For example, the [Scaffold] uses this mechanism to implement the
/// scroll-to-top gesture on iOS.
19
class PrimaryScrollController extends InheritedWidget {
20
  /// Creates a widget that associates a [ScrollController] with a subtree.
21
  const PrimaryScrollController({
22 23
    Key key,
    @required this.controller,
24
    @required Widget child,
25 26
  }) : assert(controller != null),
       super(key: key, child: child);
27

28
  /// Creates a subtree without an associated [ScrollController].
29 30
  const PrimaryScrollController.none({
    Key key,
31
    @required Widget child,
32 33 34
  }) : controller = null,
       super(key: key, child: child);

35
  /// The [ScrollController] associated with the subtree.
36 37 38 39 40
  ///
  /// See also:
  ///
  ///  * [ScrollView.controller], which discusses the purpose of specifying a
  ///    scroll controller.
41 42
  final ScrollController controller;

43 44 45 46 47
  /// Returns the [ScrollController] most closely associated with the given
  /// context.
  ///
  /// Returns null if there is no [ScrollController] associated with the given
  /// context.
48
  static ScrollController of(BuildContext context) {
49
    final PrimaryScrollController result = context.inheritFromWidgetOfExactType(PrimaryScrollController);
50 51 52 53
    return result?.controller;
  }

  @override
54
  bool updateShouldNotify(PrimaryScrollController oldWidget) => controller != oldWidget.controller;
55 56

  @override
57 58
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
59
    properties.add(DiagnosticsProperty<ScrollController>('controller', controller, ifNull: 'no controller', showName: false));
60 61
  }
}