primary_scroll_controller.dart 2.21 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9
// 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
    Key? key,
23
    required ScrollController 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
  const PrimaryScrollController.none({
30 31
    Key? key,
    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
  final ScrollController? controller;
42

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 49
  static ScrollController? of(BuildContext context) {
    final PrimaryScrollController? result = context.dependOnInheritedWidgetOfExactType<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
  }
}