Add Mimic to fn3
This commit is contained in:
parent
24171915aa
commit
93f1ba5dac
@ -298,6 +298,8 @@ abstract class State<T extends StatefulComponent> {
|
||||
/// The context in which this object will be built
|
||||
BuildContext get context => _element;
|
||||
|
||||
bool get mounted => _element != null;
|
||||
|
||||
/// Called when this object is inserted into the tree. Override this function
|
||||
/// to perform initialization that depends on the location at which this
|
||||
/// object was inserted into the tree or on the widget configuration object.
|
||||
|
87
packages/flutter/lib/src/fn3/mimic.dart
Normal file
87
packages/flutter/lib/src/fn3/mimic.dart
Normal file
@ -0,0 +1,87 @@
|
||||
// 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:sky/rendering.dart';
|
||||
import 'package:sky/src/fn3/basic.dart';
|
||||
import 'package:sky/src/fn3/framework.dart';
|
||||
|
||||
class MimicableKey {
|
||||
MimicableKey._(this._state);
|
||||
|
||||
final MimicableState _state;
|
||||
|
||||
Rect get globalBounds => _state._globalBounds;
|
||||
|
||||
void stopMimic() {
|
||||
_state._stopMimic();
|
||||
}
|
||||
}
|
||||
|
||||
class Mimic extends StatelessComponent {
|
||||
Mimic({ Key key, this.original }) : super(key: key);
|
||||
|
||||
final MimicableKey original;
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
if (original != null && original._state._beingMimicked)
|
||||
return original._state.config.child;
|
||||
return new Container();
|
||||
}
|
||||
}
|
||||
|
||||
class Mimicable extends StatefulComponent {
|
||||
Mimicable({ Key key, this.child }) : super(key: key);
|
||||
|
||||
final Widget child;
|
||||
|
||||
MimicableState createState() => new MimicableState();
|
||||
}
|
||||
|
||||
class MimicableState extends State<Mimicable> {
|
||||
Size _size;
|
||||
bool _beingMimicked = false;
|
||||
|
||||
MimicableKey startMimic() {
|
||||
assert(!_beingMimicked);
|
||||
assert(_size != null);
|
||||
setState(() {
|
||||
_beingMimicked = true;
|
||||
});
|
||||
return new MimicableKey._(this);
|
||||
}
|
||||
|
||||
void _stopMimic() {
|
||||
assert(!_beingMimicked);
|
||||
if (!mounted) {
|
||||
_beingMimicked = false;
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_beingMimicked = false;
|
||||
});
|
||||
}
|
||||
|
||||
Rect get _globalBounds {
|
||||
RenderBox box = context.findRenderObject();
|
||||
return box.localToGlobal(Point.origin) & box.size;
|
||||
}
|
||||
|
||||
void _handleSizeChanged(Size size) {
|
||||
setState(() {
|
||||
_size = size;
|
||||
});
|
||||
}
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
if (_beingMimicked) {
|
||||
return new ConstrainedBox(
|
||||
constraints: new BoxConstraints.tight(_size)
|
||||
);
|
||||
}
|
||||
return new SizeObserver(
|
||||
callback: _handleSizeChanged,
|
||||
child: config.child
|
||||
);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user