mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
We need the key to be a String even though we claimed we could support any Object. Also, clean up some style nits including shortening the |root| getters on OneChildRenderNodeWrappers. Fixes https://github.com/domokit/sky_sdk/issues/26. TBR=ianh@google.com Review URL: https://codereview.chromium.org/1173293005.
75 lines
1.9 KiB
Dart
75 lines
1.9 KiB
Dart
// 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 'dart:sky' as sky;
|
|
|
|
import 'animated_component.dart';
|
|
import 'basic.dart';
|
|
import '../framework/animation/animated_value.dart';
|
|
import '../framework/animation/curves.dart';
|
|
|
|
typedef void ValueChanged(value);
|
|
|
|
const double _kCheckDuration = 200.0;
|
|
|
|
abstract class Toggleable extends AnimatedComponent {
|
|
|
|
Toggleable({
|
|
String key,
|
|
this.value,
|
|
this.onChanged
|
|
}) : super(key: key) {
|
|
toggleAnimation = new AnimatedValue(value ? 1.0 : 0.0);
|
|
}
|
|
|
|
bool value;
|
|
AnimatedValue toggleAnimation;
|
|
ValueChanged onChanged;
|
|
|
|
void syncFields(Toggleable source) {
|
|
onChanged = source.onChanged;
|
|
if (value != source.value) {
|
|
value = source.value;
|
|
double targetValue = value ? 1.0 : 0.0;
|
|
double difference = (toggleAnimation.value - targetValue).abs();
|
|
if (difference > 0) {
|
|
toggleAnimation.stop();
|
|
double t = difference * duration;
|
|
Curve curve = targetValue > toggleAnimation.value ? curveUp : curveDown;
|
|
toggleAnimation.animateTo(targetValue, t, curve: curve);
|
|
}
|
|
}
|
|
super.syncFields(source);
|
|
}
|
|
|
|
void _handleClick(sky.Event e) {
|
|
onChanged(!value);
|
|
}
|
|
|
|
// Override these methods to draw yourself
|
|
void customPaintCallback(sky.Canvas canvas, Size size) {
|
|
assert(false);
|
|
}
|
|
Size get size => Size.zero;
|
|
EdgeDims get margin => const EdgeDims.symmetric(horizontal: 5.0);
|
|
double get duration => 200.0;
|
|
Curve get curveUp => easeIn;
|
|
Curve get curveDown => easeOut;
|
|
|
|
UINode build() {
|
|
return new EventListenerNode(
|
|
new Container(
|
|
margin: margin,
|
|
width: size.width,
|
|
height: size.height,
|
|
child: new CustomPaint(
|
|
token: toggleAnimation.value,
|
|
callback: customPaintCallback
|
|
)
|
|
),
|
|
onGestureTap: _handleClick
|
|
);
|
|
}
|
|
}
|