From 058620007bd9fb633084cf7a5266d3c59df5810d Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Thu, 15 Sep 2016 13:13:51 -0700 Subject: [PATCH] Extract a Listenable base class from Animation and ChangeNotifier (#5889) Having this base class lets classes like CustomPainter and DataTableSource be more agnostic as to what's generating the repaints. --- packages/flutter/lib/foundation.dart | 1 + .../flutter/lib/src/animation/animation.dart | 6 +++++- .../lib/src/foundation/change_notifier.dart | 9 ++++++--- .../lib/src/foundation/listenable.dart | 19 +++++++++++++++++++ .../lib/src/material/data_table_source.dart | 2 +- .../flutter/lib/src/material/time_picker.dart | 6 ++++++ packages/flutter/lib/src/rendering/flow.dart | 8 ++++---- .../flutter/lib/src/rendering/proxy_box.dart | 8 ++++---- 8 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 packages/flutter/lib/src/foundation/listenable.dart diff --git a/packages/flutter/lib/foundation.dart b/packages/flutter/lib/foundation.dart index 21cb3f2ab8..a006e38ff7 100644 --- a/packages/flutter/lib/foundation.dart +++ b/packages/flutter/lib/foundation.dart @@ -14,6 +14,7 @@ export 'src/foundation/basic_types.dart'; export 'src/foundation/binding.dart'; export 'src/foundation/change_notifier.dart'; export 'src/foundation/licenses.dart'; +export 'src/foundation/listenable.dart'; export 'src/foundation/platform.dart'; export 'src/foundation/print.dart'; export 'src/foundation/synchronous_future.dart'; diff --git a/packages/flutter/lib/src/animation/animation.dart b/packages/flutter/lib/src/animation/animation.dart index 3336b90696..7dc7a3999f 100644 --- a/packages/flutter/lib/src/animation/animation.dart +++ b/packages/flutter/lib/src/animation/animation.dart @@ -4,6 +4,8 @@ import 'dart:ui' show VoidCallback; +import 'package:flutter/foundation.dart'; + /// The status of an animation enum AnimationStatus { /// The animation is stopped at the beginning @@ -36,7 +38,7 @@ typedef void AnimationStatusListener(AnimationStatus status); /// /// To create a new animation that you can run forward and backward, consider /// using [AnimationController]. -abstract class Animation { +abstract class Animation extends Listenable { /// Abstract const constructor. This constructor enables subclasses to provide /// const constructors so that they can be used in const expressions. const Animation(); @@ -46,11 +48,13 @@ abstract class Animation { /// Calls the listener every time the value of the animation changes. /// /// Listeners can be removed with [removeListener]. + @override void addListener(VoidCallback listener); /// Stop calling the listener every time the value of the animation changes. /// /// Listeners can be added with [addListener]. + @override void removeListener(VoidCallback listener); /// Calls listener every time the status of the animation changes. diff --git a/packages/flutter/lib/src/foundation/change_notifier.dart b/packages/flutter/lib/src/foundation/change_notifier.dart index e20df8eb99..5ce4d372e9 100644 --- a/packages/flutter/lib/src/foundation/change_notifier.dart +++ b/packages/flutter/lib/src/foundation/change_notifier.dart @@ -6,13 +6,15 @@ import 'package:meta/meta.dart'; import 'assertions.dart'; import 'basic_types.dart'; +import 'listenable.dart'; -/// Abstract class that can be extended or mixed in that provides -/// a change notification API using [VoidCallback] for notifications. -abstract class ChangeNotifier { +/// A class that can be extended or mixed in that provides a change notification +/// API using [VoidCallback] for notifications. +class ChangeNotifier extends Listenable { List _listeners; /// Register a closure to be called when the object changes. + @override void addListener(VoidCallback listener) { _listeners ??= []; _listeners.add(listener); @@ -20,6 +22,7 @@ abstract class ChangeNotifier { /// Remove a previously registered closure from the list of closures that are /// notified when the object changes. + @override void removeListener(VoidCallback listener) { _listeners?.remove(listener); } diff --git a/packages/flutter/lib/src/foundation/listenable.dart b/packages/flutter/lib/src/foundation/listenable.dart new file mode 100644 index 0000000000..ad193d322c --- /dev/null +++ b/packages/flutter/lib/src/foundation/listenable.dart @@ -0,0 +1,19 @@ +// Copyright 2016 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 'basic_types.dart'; + +/// An object that maintains a list of listeners. +abstract class Listenable { + /// Abstract const constructor. This constructor enables subclasses to provide + /// const constructors so that they can be used in const expressions. + const Listenable(); + + /// Register a closure to be called when the object notifies its listeners. + void addListener(VoidCallback listener); + + /// Remove a previously registered closure from the list of closures that the + /// object notifies. + void removeListener(VoidCallback listener); +} diff --git a/packages/flutter/lib/src/material/data_table_source.dart b/packages/flutter/lib/src/material/data_table_source.dart index ed5cb4dc89..4ebc554278 100644 --- a/packages/flutter/lib/src/material/data_table_source.dart +++ b/packages/flutter/lib/src/material/data_table_source.dart @@ -59,4 +59,4 @@ abstract class DataTableSource extends ChangeNotifier { /// /// If the selected row count changes, call [notifyListeners]. int get selectedRowCount; -} \ No newline at end of file +} diff --git a/packages/flutter/lib/src/material/time_picker.dart b/packages/flutter/lib/src/material/time_picker.dart index 143b3a36af..ee87fd09fd 100644 --- a/packages/flutter/lib/src/material/time_picker.dart +++ b/packages/flutter/lib/src/material/time_picker.dart @@ -426,6 +426,12 @@ class _DialState extends State<_Dial> { _animateTo(_getThetaForTime(config.selectedTime)); } + @override + void dispose() { + _thetaController.dispose(); + super.dispose(); + } + Tween _thetaTween; Animation _theta; AnimationController _thetaController; diff --git a/packages/flutter/lib/src/rendering/flow.dart b/packages/flutter/lib/src/rendering/flow.dart index a043185121..6534971348 100644 --- a/packages/flutter/lib/src/rendering/flow.dart +++ b/packages/flutter/lib/src/rendering/flow.dart @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:flutter/animation.dart'; +import 'package:flutter/foundation.dart'; import 'package:vector_math/vector_math_64.dart'; import 'box.dart'; @@ -54,10 +54,10 @@ abstract class FlowPaintingContext { /// * [Flow] /// * [RenderFlow] abstract class FlowDelegate { - /// The flow will repaint whenever the [repaint] animation ticks. - const FlowDelegate({ Animation repaint }) : _repaint = repaint; + /// The flow will repaint whenever [repaint] notifies its listeners. + const FlowDelegate({ Listenable repaint }) : _repaint = repaint; - final Animation _repaint; + final Listenable _repaint; /// Override to control the size of the container for the children. /// diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index 94c3cadd0b..a6633689f4 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -4,7 +4,7 @@ import 'dart:ui' as ui show ImageFilter; -import 'package:flutter/animation.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; import 'package:meta/meta.dart'; import 'package:vector_math/vector_math_64.dart'; @@ -1490,10 +1490,10 @@ class RenderFractionalTranslation extends RenderProxyBox { abstract class CustomPainter { /// Creates a custom painter. /// - /// The painter will repaint whenever the [repaint] animation ticks. - const CustomPainter({ Animation repaint }) : _repaint = repaint; + /// The painter will repaint whenever [repaint] notifies its listeners. + const CustomPainter({ Listenable repaint }) : _repaint = repaint; - final Animation _repaint; + final Listenable _repaint; /// Called whenever the object needs to paint. The given [Canvas] has its /// coordinate space configured such that the origin is at the top left of the