This patch contains a prototype of a new widget framework. In this framework, Components can be reused in the tree as many times as the author desires. Also, StatefulComponent is split into two pieces, a ComponentConfiguration and a ComponentState. The ComponentConfiguration is created by the author and can be reused as many times as desired. When mounted into the tree, the ComponentConfiguration creates a ComponentState to hold the state for the component. The state remains in the tree and cannot be reused.
45 lines
954 B
Dart
45 lines
954 B
Dart
import 'package:sky/src/fn3/framework.dart';
|
|
|
|
class TestComponent extends Component {
|
|
TestComponent({ this.child });
|
|
final Widget child;
|
|
Widget build() => child;
|
|
}
|
|
|
|
class WidgetTester {
|
|
ComponentElement _rootElement;
|
|
|
|
void walkElements(ElementVisitor visitor) {
|
|
void walk(Element element) {
|
|
visitor(element);
|
|
element.visitChildren(walk);
|
|
}
|
|
|
|
_rootElement.visitChildren(walk);
|
|
}
|
|
|
|
Element findElement(bool predicate(Element widget)) {
|
|
try {
|
|
walkElements((Element widget) {
|
|
if (predicate(widget))
|
|
throw widget;
|
|
});
|
|
} catch (e) {
|
|
if (e is Element)
|
|
return e;
|
|
rethrow;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
void pumpFrame(Widget widget) {
|
|
if (_rootElement == null) {
|
|
_rootElement = new ComponentElement(new TestComponent(child: widget));
|
|
_rootElement.mount(null);
|
|
} else {
|
|
_rootElement.update(new TestComponent(child: widget), null);
|
|
}
|
|
}
|
|
|
|
}
|