Commit c3b3b71b authored by Hixie's avatar Hixie

Rename ComponentState and use initState().

ComponentState becomes State, for brevity.
Instead of overriding its constructor, override initState().
This makes writing States much simpler.
parent 7dd26a7a
...@@ -10,12 +10,10 @@ class BigSwitch extends StatefulComponent { ...@@ -10,12 +10,10 @@ class BigSwitch extends StatefulComponent {
final double scale; final double scale;
BigSwitchState createState() => new BigSwitchState(this); BigSwitchState createState() => new BigSwitchState();
} }
class BigSwitchState extends ComponentState<BigSwitch> { class BigSwitchState extends State<BigSwitch> {
BigSwitchState(BigSwitch config) : super(config);
bool _value = false; bool _value = false;
void _handleOnChanged(bool value) { void _handleOnChanged(bool value) {
......
...@@ -8,11 +8,12 @@ import 'package:sky/material.dart'; ...@@ -8,11 +8,12 @@ import 'package:sky/material.dart';
void main() => runApp(new DatePickerDemo()); void main() => runApp(new DatePickerDemo());
class DatePickerDemo extends StatefulComponent { class DatePickerDemo extends StatefulComponent {
DatePickerDemoState createState() => new DatePickerDemoState(this); DatePickerDemoState createState() => new DatePickerDemoState();
} }
class DatePickerDemoState extends ComponentState<DatePickerDemo> { class DatePickerDemoState extends State<DatePickerDemo> {
DatePickerDemoState(DatePickerDemo config) : super(config) { void initState(BuildContext context) {
super.initState(context);
DateTime now = new DateTime.now(); DateTime now = new DateTime.now();
_dateTime = new DateTime(now.year, now.month, now.day); _dateTime = new DateTime(now.year, now.month, now.day);
} }
......
...@@ -17,12 +17,10 @@ class DragData { ...@@ -17,12 +17,10 @@ class DragData {
} }
class ExampleDragTarget extends StatefulComponent { class ExampleDragTarget extends StatefulComponent {
ExampleDragTargetState createState() => new ExampleDragTargetState(this); ExampleDragTargetState createState() => new ExampleDragTargetState();
} }
class ExampleDragTargetState extends ComponentState<ExampleDragTarget> { class ExampleDragTargetState extends State<ExampleDragTarget> {
ExampleDragTargetState(ExampleDragTarget config) : super(config);
String _text = 'ready'; String _text = 'ready';
void _handleAccept(DragData data) { void _handleAccept(DragData data) {
...@@ -71,9 +69,7 @@ class DragAndDropApp extends StatefulComponent { ...@@ -71,9 +69,7 @@ class DragAndDropApp extends StatefulComponent {
DragAndDropAppState createState() => new DragAndDropAppState(this); DragAndDropAppState createState() => new DragAndDropAppState(this);
} }
class DragAndDropAppState extends ComponentState<DragAndDropApp> { class DragAndDropAppState extends State<DragAndDropApp> {
DragAndDropAppState(DragAndDropApp config) : super(config);
DragController _dragController; DragController _dragController;
Offset _displacement = Offset.zero; Offset _displacement = Offset.zero;
......
...@@ -61,12 +61,10 @@ List<Route> routes = [ ...@@ -61,12 +61,10 @@ List<Route> routes = [
]; ];
class NavigationExampleApp extends StatefulComponent { class NavigationExampleApp extends StatefulComponent {
NavigationExampleAppState createState() => new NavigationExampleAppState(this); NavigationExampleAppState createState() => new NavigationExampleAppState();
} }
class NavigationExampleAppState extends ComponentState<NavigationExampleApp> { class NavigationExampleAppState extends State<NavigationExampleApp> {
NavigationExampleAppState(NavigationExampleApp config) : super(config);
NavigatorHistory _history = new NavigatorHistory(routes); NavigatorHistory _history = new NavigatorHistory(routes);
void onBack() { void onBack() {
......
...@@ -20,8 +20,9 @@ class PageableListApp extends StatefulComponent { ...@@ -20,8 +20,9 @@ class PageableListApp extends StatefulComponent {
PageableListAppState createState() => new PageableListAppState(this); PageableListAppState createState() => new PageableListAppState(this);
} }
class PageableListAppState extends ComponentState<PageableListApp> { class PageableListAppState extends State<PageableListApp> {
PageableListAppState(PageableListApp config) : super(config) { void initState(BuildContext context) {
super.initState(context);
List<Size> cardSizes = [ List<Size> cardSizes = [
[100.0, 300.0], [300.0, 100.0], [200.0, 400.0], [400.0, 400.0], [300.0, 400.0] [100.0, 300.0], [300.0, 100.0], [200.0, 400.0], [400.0, 400.0], [300.0, 400.0]
] ]
......
...@@ -10,8 +10,9 @@ class ProgressIndicatorApp extends StatefulComponent { ...@@ -10,8 +10,9 @@ class ProgressIndicatorApp extends StatefulComponent {
ProgressIndicatorAppState createState() => new ProgressIndicatorAppState(this); ProgressIndicatorAppState createState() => new ProgressIndicatorAppState(this);
} }
class ProgressIndicatorAppState extends ComponentState<ProgressIndicatorApp> { class ProgressIndicatorAppState extends State<ProgressIndicatorApp> {
ProgressIndicatorAppState(ProgressIndicatorApp config) : super(config) { void initState(BuildContext context) {
super.initState(context);
valueAnimation = new ValueAnimation<double>() valueAnimation = new ValueAnimation<double>()
..duration = const Duration(milliseconds: 1500) ..duration = const Duration(milliseconds: 1500)
..variable = new AnimatedValue<double>( ..variable = new AnimatedValue<double>(
......
...@@ -10,8 +10,9 @@ class ScaleApp extends StatefulComponent { ...@@ -10,8 +10,9 @@ class ScaleApp extends StatefulComponent {
ScaleAppState createState() => new ScaleAppState(this); ScaleAppState createState() => new ScaleAppState(this);
} }
class ScaleAppState extends ComponentState<ScaleApp> { class ScaleAppState extends State<ScaleApp> {
ScaleAppState(ScaleApp config) : super(config) { void initState(BuildContext context) {
super.initState(context);
_offset = Offset.zero; _offset = Offset.zero;
_zoom = 1.0; _zoom = 1.0;
} }
......
...@@ -12,8 +12,9 @@ abstract class AnimatedComponent extends StatefulComponent { ...@@ -12,8 +12,9 @@ abstract class AnimatedComponent extends StatefulComponent {
final Direction direction; final Direction direction;
} }
abstract class AnimatedComponentState<T extends AnimatedComponent> extends ComponentState<T> { abstract class AnimatedState<T extends AnimatedComponent> extends State<T> {
AnimatedComponentState(T config) : super(config) { void initState(BuildContext context) {
super.initState(context);
_performance = new AnimationPerformance(duration: config.duration); _performance = new AnimationPerformance(duration: config.duration);
performance.addStatusListener(_handleAnimationStatusChanged); performance.addStatusListener(_handleAnimationStatusChanged);
if (buildDependsOnPerformance) { if (buildDependsOnPerformance) {
......
...@@ -724,11 +724,12 @@ class ImageListener extends StatefulComponent { ...@@ -724,11 +724,12 @@ class ImageListener extends StatefulComponent {
final ImageFit fit; final ImageFit fit;
final ImageRepeat repeat; final ImageRepeat repeat;
ImageListenerState createState() => new ImageListenerState(this); ImageListenerState createState() => new ImageListenerState();
} }
class ImageListenerState extends ComponentState<ImageListener> { class ImageListenerState extends State<ImageListener> {
ImageListenerState(ImageListener config) : super(config) { void initState(BuildContext context) {
super.initState(context);
config.image.addListener(_handleImageChanged); config.image.addListener(_handleImageChanged);
} }
...@@ -742,6 +743,7 @@ class ImageListenerState extends ComponentState<ImageListener> { ...@@ -742,6 +743,7 @@ class ImageListenerState extends ComponentState<ImageListener> {
void dispose() { void dispose() {
config.image.removeListener(_handleImageChanged); config.image.removeListener(_handleImageChanged);
super.dispose();
} }
void didUpdateConfig(ImageListener oldConfig) { void didUpdateConfig(ImageListener oldConfig) {
......
...@@ -5,9 +5,7 @@ ...@@ -5,9 +5,7 @@
import 'package:sky/src/fn3/basic.dart'; import 'package:sky/src/fn3/basic.dart';
import 'package:sky/src/fn3/framework.dart'; import 'package:sky/src/fn3/framework.dart';
abstract class ButtonState<T extends StatefulComponent> extends ComponentState<T> { abstract class ButtonState<T extends StatefulComponent> extends State<T> {
ButtonState(T config) : super(config);
bool highlight = false; bool highlight = false;
void _handlePointerDown(_) { void _handlePointerDown(_) {
......
...@@ -39,12 +39,10 @@ class DatePicker extends StatefulComponent { ...@@ -39,12 +39,10 @@ class DatePicker extends StatefulComponent {
final DateTime firstDate; final DateTime firstDate;
final DateTime lastDate; final DateTime lastDate;
DatePickerState createState() => new DatePickerState(this); DatePickerState createState() => new DatePickerState();
} }
class DatePickerState extends ComponentState<DatePicker> { class DatePickerState extends State<DatePicker> {
DatePickerState(DatePicker config) : super(config);
DatePickerMode _mode = DatePickerMode.day; DatePickerMode _mode = DatePickerMode.day;
void _handleModeChanged(DatePickerMode mode) { void _handleModeChanged(DatePickerMode mode) {
...@@ -292,11 +290,12 @@ class MonthPicker extends ScrollableWidgetList { ...@@ -292,11 +290,12 @@ class MonthPicker extends ScrollableWidgetList {
final DateTime firstDate; final DateTime firstDate;
final DateTime lastDate; final DateTime lastDate;
MonthPickerState createState() => new MonthPickerState(this); MonthPickerState createState() => new MonthPickerState();
} }
class MonthPickerState extends ScrollableWidgetListState<MonthPicker> { class MonthPickerState extends ScrollableWidgetListState<MonthPicker> {
MonthPickerState(MonthPicker config) : super(config) { void initState(BuildContext context) {
super.initState(context);
_updateCurrentDate(); _updateCurrentDate();
} }
...@@ -342,6 +341,7 @@ class MonthPickerState extends ScrollableWidgetListState<MonthPicker> { ...@@ -342,6 +341,7 @@ class MonthPickerState extends ScrollableWidgetListState<MonthPicker> {
void dispose() { void dispose() {
if (_timer != null) if (_timer != null)
_timer.cancel(); _timer.cancel();
super.dispose();
} }
} }
...@@ -363,12 +363,10 @@ class YearPicker extends ScrollableWidgetList { ...@@ -363,12 +363,10 @@ class YearPicker extends ScrollableWidgetList {
final DateTime firstDate; final DateTime firstDate;
final DateTime lastDate; final DateTime lastDate;
YearPickerState createState() => new YearPickerState(this); YearPickerState createState() => new YearPickerState();
} }
class YearPickerState extends ScrollableWidgetListState<YearPicker> { class YearPickerState extends ScrollableWidgetListState<YearPicker> {
YearPickerState(YearPicker config) : super(config);
int get itemCount => config.lastDate.year - config.firstDate.year + 1; int get itemCount => config.lastDate.year - config.firstDate.year + 1;
List<Widget> buildItems(BuildContext context, int start, int count) { List<Widget> buildItems(BuildContext context, int start, int count) {
......
...@@ -44,11 +44,12 @@ class Dismissable extends StatefulComponent { ...@@ -44,11 +44,12 @@ class Dismissable extends StatefulComponent {
DismissedCallback onDismissed; DismissedCallback onDismissed;
DismissDirection direction; DismissDirection direction;
DismissableState createState() => new DismissableState(this); DismissableState createState() => new DismissableState();
} }
class DismissableState extends ComponentState<Dismissable> { class DismissableState extends State<Dismissable> {
DismissableState(Dismissable config) : super(config) { void initState(BuildContext context) {
super.initState(context);
_fadePerformance = new AnimationPerformance(duration: _kCardDismissFadeout); _fadePerformance = new AnimationPerformance(duration: _kCardDismissFadeout);
_fadePerformance.addStatusListener((AnimationStatus status) { _fadePerformance.addStatusListener((AnimationStatus status) {
if (status == AnimationStatus.completed) if (status == AnimationStatus.completed)
......
...@@ -25,12 +25,10 @@ class DragTarget<T> extends StatefulComponent { ...@@ -25,12 +25,10 @@ class DragTarget<T> extends StatefulComponent {
final DragTargetWillAccept<T> onWillAccept; final DragTargetWillAccept<T> onWillAccept;
final DragTargetAccept<T> onAccept; final DragTargetAccept<T> onAccept;
DragTargetState<T> createState() => new DragTargetState<T>(this); DragTargetState<T> createState() => new DragTargetState<T>();
} }
class DragTargetState<T> extends ComponentState<DragTarget<T>> { class DragTargetState<T> extends State<DragTarget<T>> {
DragTargetState(DragTarget<T> config) : super(config);
final List<T> _candidateData = new List<T>(); final List<T> _candidateData = new List<T>();
final List<dynamic> _rejectedData = new List<dynamic>(); final List<dynamic> _rejectedData = new List<dynamic>();
......
...@@ -53,11 +53,12 @@ class Drawer extends StatefulComponent { ...@@ -53,11 +53,12 @@ class Drawer extends StatefulComponent {
final DrawerDismissedCallback onDismissed; final DrawerDismissedCallback onDismissed;
final NavigatorState navigator; final NavigatorState navigator;
DrawerState createState() => new DrawerState(this); DrawerState createState() => new DrawerState();
} }
class DrawerState extends ComponentState<Drawer> { class DrawerState extends State<Drawer> {
DrawerState(Drawer config) : super(config) { void initState(BuildContext context) {
super.initState(context);
_performance = new AnimationPerformance(duration: _kBaseSettleDuration); _performance = new AnimationPerformance(duration: _kBaseSettleDuration);
_performance.addStatusListener((AnimationStatus status) { _performance.addStatusListener((AnimationStatus status) {
if (status == AnimationStatus.dismissed) if (status == AnimationStatus.dismissed)
......
...@@ -24,12 +24,10 @@ class DrawerItem extends StatefulComponent { ...@@ -24,12 +24,10 @@ class DrawerItem extends StatefulComponent {
final GestureTapListener onPressed; final GestureTapListener onPressed;
final bool selected; final bool selected;
DrawerItemState createState() => new DrawerItemState(this); DrawerItemState createState() => new DrawerItemState();
} }
class DrawerItemState extends ButtonState<DrawerItem> { class DrawerItemState extends ButtonState<DrawerItem> {
DrawerItemState(DrawerItem config) : super(config);
TextStyle _getTextStyle(ThemeData themeData) { TextStyle _getTextStyle(ThemeData themeData) {
TextStyle result = themeData.text.body2; TextStyle result = themeData.text.body2;
if (config.selected) if (config.selected)
......
...@@ -19,12 +19,10 @@ class FlatButton extends MaterialButton { ...@@ -19,12 +19,10 @@ class FlatButton extends MaterialButton {
enabled: enabled, enabled: enabled,
onPressed: onPressed); onPressed: onPressed);
FlatButtonState createState() => new FlatButtonState(this); FlatButtonState createState() => new FlatButtonState();
} }
class FlatButtonState extends MaterialButtonState<FlatButton> { class FlatButtonState extends MaterialButtonState<FlatButton> {
FlatButtonState(FlatButton config) : super(config);
Color getColor(BuildContext context) { Color getColor(BuildContext context) {
if (!config.enabled || !highlight) if (!config.enabled || !highlight)
return null; return null;
......
...@@ -28,12 +28,10 @@ class FloatingActionButton extends StatefulComponent { ...@@ -28,12 +28,10 @@ class FloatingActionButton extends StatefulComponent {
final Color backgroundColor; final Color backgroundColor;
final GestureTapListener onPressed; final GestureTapListener onPressed;
FloatingActionButtonState createState() => new FloatingActionButtonState(this); FloatingActionButtonState createState() => new FloatingActionButtonState();
} }
class FloatingActionButtonState extends ButtonState<FloatingActionButton> { class FloatingActionButtonState extends ButtonState<FloatingActionButton> {
FloatingActionButtonState(FloatingActionButton config) : super(config);
Widget buildContent(BuildContext context) { Widget buildContent(BuildContext context) {
IconThemeColor iconThemeColor = IconThemeColor.white; IconThemeColor iconThemeColor = IconThemeColor.white;
Color materialColor = config.backgroundColor; Color materialColor = config.backgroundColor;
......
...@@ -75,12 +75,10 @@ class Focus extends StatefulComponent { ...@@ -75,12 +75,10 @@ class Focus extends StatefulComponent {
final bool autofocus; final bool autofocus;
final Widget child; final Widget child;
FocusState createState() => new FocusState(this); FocusState createState() => new FocusState();
} }
class FocusState extends ComponentState<Focus> { class FocusState extends State<Focus> {
FocusState(Focus config) : super(config);
GlobalKey _focusedWidget; // when null, the first component to ask if it's focused will get the focus GlobalKey _focusedWidget; // when null, the first component to ask if it's focused will get the focus
GlobalKey _currentlyRegisteredWidgetRemovalListenerKey; GlobalKey _currentlyRegisteredWidgetRemovalListenerKey;
...@@ -164,6 +162,7 @@ class FocusState extends ComponentState<Focus> { ...@@ -164,6 +162,7 @@ class FocusState extends ComponentState<Focus> {
void dispose() { void dispose() {
_updateWidgetRemovalListener(null); _updateWidgetRemovalListener(null);
_updateScopeRemovalListener(null); _updateScopeRemovalListener(null);
super.dispose();
} }
Widget build(BuildContext context) { Widget build(BuildContext context) {
......
...@@ -289,8 +289,8 @@ abstract class StatelessComponent extends Widget { ...@@ -289,8 +289,8 @@ abstract class StatelessComponent extends Widget {
} }
/// StatefulComponents provide the configuration for /// StatefulComponents provide the configuration for
/// [StatefulComponentElement]s, which wrap [ComponentState]s, which hold /// [StatefulComponentElement]s, which wrap [State]s, which hold mutable state
/// mutable state and can dynamically and spontaneously ask to be rebuilt. /// and can dynamically and spontaneously ask to be rebuilt.
abstract class StatefulComponent extends Widget { abstract class StatefulComponent extends Widget {
const StatefulComponent({ Key key }) : super(key: key); const StatefulComponent({ Key key }) : super(key: key);
...@@ -300,49 +300,71 @@ abstract class StatefulComponent extends Widget { ...@@ -300,49 +300,71 @@ abstract class StatefulComponent extends Widget {
/// Returns an instance of the state to which this StatefulComponent is /// Returns an instance of the state to which this StatefulComponent is
/// related, using this object as the configuration. Subclasses should /// related, using this object as the configuration. Subclasses should
/// override this to return a new instance of the ComponentState class /// override this to return a new instance of the State class associated with
/// associated with this StatefulComponent class, like this: /// this StatefulComponent class, like this:
/// ///
/// MyComponentState createState() => new MyComponentState(this); /// MyState createState() => new MyState(this);
ComponentState createState(); State createState();
}
enum _StateLifecycle {
created,
initialized,
ready,
defunct,
} }
/// The logic and internal state for a StatefulComponent. /// The logic and internal state for a StatefulComponent.
abstract class ComponentState<T extends StatefulComponent> { abstract class State<T extends StatefulComponent> {
ComponentState(this._config); /// The current configuration (an instance of the corresponding
/// StatefulComponent class).
T get config => _config;
T _config;
/// This is used to verify that State objects move through life in an orderly fashion.
_StateLifecycle _debugLifecycleState = _StateLifecycle.created;
/// Pointer to the owner Element object
StatefulComponentElement _element; StatefulComponentElement _element;
/// Whenever you need to change internal state for a ComponentState object, /// Called when this object is inserted into the tree. Override this function
/// make the change in a function that you pass to setState(), as in: /// to perform initialization that depends on the location at which this
/// /// object was inserted into the tree or on the widget configuration object.
/// setState(() { myState = newValue });
/// ///
/// If you just change the state directly without calling setState(), then /// If you override this, make sure your method starts with a call to
/// the component will not be scheduled for rebuilding, meaning that its /// super.initState(context).
/// rendering will not be updated. void initState(BuildContext context) {
void setState(void fn()) { assert(_debugLifecycleState == _StateLifecycle.created);
fn(); assert(() { _debugLifecycleState = _StateLifecycle.initialized; return true; });
_element.markNeedsBuild();
} }
/// The current configuration (an instance of the corresponding
/// StatefulComponent class).
T get config => _config;
T _config;
/// Called whenever the configuration changes. Override this method to update /// Called whenever the configuration changes. Override this method to update
/// additional state when the config field's value is changed. /// additional state when the config field's value is changed.
void didUpdateConfig(T oldConfig) { } void didUpdateConfig(T oldConfig) { }
/// Called when this object is inserted into the tree. Override this function /// Whenever you need to change internal state for a State object, make the
/// to perform initialization that depends on the location at which this /// change in a function that you pass to setState(), as in:
/// object was inserted into the tree. ///
void initState(BuildContext context) { } /// setState(() { myState = newValue });
///
/// If you just change the state directly without calling setState(), then the
/// component will not be scheduled for rebuilding, meaning that its rendering
/// will not be updated.
void setState(void fn()) {
assert(_debugLifecycleState == _StateLifecycle.ready);
fn();
_element.markNeedsBuild();
}
/// Called when this object is removed from the tree. Override this to clean /// Called when this object is removed from the tree. Override this to clean
/// up any resources allocated by this object. /// up any resources allocated by this object.
void dispose() { } ///
/// If you override this, make sure to end your method with a call to
/// super.dispose().
void dispose() {
assert(_debugLifecycleState == _StateLifecycle.ready);
assert(() { _debugLifecycleState = _StateLifecycle.defunct; return true; });
}
/// Returns another Widget out of which this StatefulComponent is built. /// Returns another Widget out of which this StatefulComponent is built.
/// Typically that Widget will have been configured with further children, /// Typically that Widget will have been configured with further children,
...@@ -634,8 +656,8 @@ abstract class BuildableElement<T extends Widget> extends Element<T> { ...@@ -634,8 +656,8 @@ abstract class BuildableElement<T extends Widget> extends Element<T> {
} }
/// Reinvokes the build() method of the StatelessComponent object (for /// Reinvokes the build() method of the StatelessComponent object (for
/// stateless components) or the ComponentState object (for stateful /// stateless components) or the State object (for stateful components) and
/// components) and then updates the widget tree. /// then updates the widget tree.
/// ///
/// Called automatically during mount() to generate the first build, by the /// Called automatically during mount() to generate the first build, by the
/// binding when scheduleBuild() has been called to mark this element dirty, /// binding when scheduleBuild() has been called to mark this element dirty,
...@@ -670,8 +692,8 @@ abstract class BuildableElement<T extends Widget> extends Element<T> { ...@@ -670,8 +692,8 @@ abstract class BuildableElement<T extends Widget> extends Element<T> {
static bool get _debugStateLocked => _debugStateLockLevel > 0; static bool get _debugStateLocked => _debugStateLockLevel > 0;
/// Calls the callback argument synchronously, but in a context where calls to /// Calls the callback argument synchronously, but in a context where calls to
/// ComponentState.setState() will fail. Use this when it is possible that you /// State.setState() will fail. Use this when it is possible that you will
/// will trigger code in components but want to make sure that there is no /// trigger code in components but want to make sure that there is no
/// possibility that any components will be marked dirty, for example because /// possibility that any components will be marked dirty, for example because
/// you are in the middle of layout and you are not going to be flushing the /// you are in the middle of layout and you are not going to be flushing the
/// build queue (since that could mutate the layout tree). /// build queue (since that could mutate the layout tree).
...@@ -735,14 +757,19 @@ class StatelessComponentElement<T extends StatelessComponent> extends BuildableE ...@@ -735,14 +757,19 @@ class StatelessComponentElement<T extends StatelessComponent> extends BuildableE
class StatefulComponentElement extends BuildableElement<StatefulComponent> { class StatefulComponentElement extends BuildableElement<StatefulComponent> {
StatefulComponentElement(StatefulComponent widget) StatefulComponentElement(StatefulComponent widget)
: _state = widget.createState(), super(widget) { : _state = widget.createState(), super(widget) {
assert(_state._config == widget);
assert(_state._element == null); assert(_state._element == null);
_state._element = this; _state._element = this;
assert(_state._config == null);
_state._config = widget;
assert(_state._debugLifecycleState == _StateLifecycle.created);
_state.initState(this);
assert(_state._debugLifecycleState == _StateLifecycle.initialized);
assert(() { _state._debugLifecycleState = _StateLifecycle.ready; return true; });
_builder = _state.build; _builder = _state.build;
} }
ComponentState get state => _state; State get state => _state;
ComponentState _state; State _state;
void update(StatefulComponent newWidget) { void update(StatefulComponent newWidget) {
super.update(newWidget); super.update(newWidget);
...@@ -757,6 +784,7 @@ class StatefulComponentElement extends BuildableElement<StatefulComponent> { ...@@ -757,6 +784,7 @@ class StatefulComponentElement extends BuildableElement<StatefulComponent> {
void unmount() { void unmount() {
super.unmount(); super.unmount();
_state.dispose(); _state.dispose();
assert(_state._debugLifecycleState == _StateLifecycle.defunct);
_state._element = null; _state._element = null;
_state = null; _state = null;
} }
......
...@@ -51,11 +51,12 @@ class GestureDetector extends StatefulComponent { ...@@ -51,11 +51,12 @@ class GestureDetector extends StatefulComponent {
final GestureScaleUpdateCallback onScaleUpdate; final GestureScaleUpdateCallback onScaleUpdate;
final GestureScaleEndCallback onScaleEnd; final GestureScaleEndCallback onScaleEnd;
GestureDetectorState createState() => new GestureDetectorState(this); GestureDetectorState createState() => new GestureDetectorState();
} }
class GestureDetectorState extends ComponentState<GestureDetector> { class GestureDetectorState extends State<GestureDetector> {
GestureDetectorState(GestureDetector config) : super(config) { void initState(BuildContext context) {
super.initState(context);
didUpdateConfig(null); didUpdateConfig(null);
} }
...@@ -120,6 +121,7 @@ class GestureDetectorState extends ComponentState<GestureDetector> { ...@@ -120,6 +121,7 @@ class GestureDetectorState extends ComponentState<GestureDetector> {
_horizontalDrag = _ensureDisposed(_horizontalDrag); _horizontalDrag = _ensureDisposed(_horizontalDrag);
_pan = _ensureDisposed(_pan); _pan = _ensureDisposed(_pan);
_scale = _ensureDisposed(_scale); _scale = _ensureDisposed(_scale);
super.dispose();
} }
void didUpdateConfig(GestureDetector oldConfig) { void didUpdateConfig(GestureDetector oldConfig) {
......
...@@ -52,7 +52,6 @@ class HomogeneousViewportElement extends RenderObjectElement<HomogeneousViewport ...@@ -52,7 +52,6 @@ class HomogeneousViewportElement extends RenderObjectElement<HomogeneousViewport
HomogeneousViewportElement(HomogeneousViewport widget) : super(widget); HomogeneousViewportElement(HomogeneousViewport widget) : super(widget);
List<Element> _children = const <Element>[]; List<Element> _children = const <Element>[];
bool _layoutDirty = true;
int _layoutFirstIndex; int _layoutFirstIndex;
int _layoutItemCount; int _layoutItemCount;
......
...@@ -26,8 +26,6 @@ abstract class MaterialButton extends StatefulComponent { ...@@ -26,8 +26,6 @@ abstract class MaterialButton extends StatefulComponent {
} }
abstract class MaterialButtonState<T extends MaterialButton> extends ButtonState<T> { abstract class MaterialButtonState<T extends MaterialButton> extends ButtonState<T> {
MaterialButtonState(T config) : super(config);
Color getColor(BuildContext context); Color getColor(BuildContext context);
int get level; int get level;
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:collection';
import 'package:sky/rendering.dart'; import 'package:sky/rendering.dart';
import 'package:sky/src/fn3/framework.dart'; import 'package:sky/src/fn3/framework.dart';
import 'package:sky/src/fn3/basic.dart'; import 'package:sky/src/fn3/basic.dart';
...@@ -63,9 +61,9 @@ class _ChildKey { ...@@ -63,9 +61,9 @@ class _ChildKey {
} }
class MixedViewportElement extends RenderObjectElement<MixedViewport> { class MixedViewportElement extends RenderObjectElement<MixedViewport> {
MixedViewportElement(MixedViewport config) : super(config) { MixedViewportElement(MixedViewport widget) : super(widget) {
if (config.onInvalidatorAvailable != null) if (widget.onInvalidatorAvailable != null)
config.onInvalidatorAvailable(invalidate); widget.onInvalidatorAvailable(invalidate);
} }
/// _childOffsets contains the offsets of each child from the top of the list /// _childOffsets contains the offsets of each child from the top of the list
...@@ -269,9 +267,13 @@ class MixedViewportElement extends RenderObjectElement<MixedViewport> { ...@@ -269,9 +267,13 @@ class MixedViewportElement extends RenderObjectElement<MixedViewport> {
double _getOffset(Element element, BoxConstraints innerConstraints) { double _getOffset(Element element, BoxConstraints innerConstraints) {
final RenderBox childRenderObject = element.renderObject; final RenderBox childRenderObject = element.renderObject;
switch (widget.direction) { switch (widget.direction) {
case ScrollDirection.vertical: return childRenderObject.getMaxIntrinsicHeight(innerConstraints); case ScrollDirection.vertical:
case ScrollDirection.horizontal: return childRenderObject.getMaxIntrinsicWidth(innerConstraints); return childRenderObject.getMaxIntrinsicHeight(innerConstraints);
case ScrollDirection.both: assert(false); // we don't support ScrollDirection.both, see issue 888 case ScrollDirection.horizontal:
return childRenderObject.getMaxIntrinsicWidth(innerConstraints);
case ScrollDirection.both:
assert(false); // we don't support ScrollDirection.both, see issue 888
return double.NAN;
} }
} }
......
...@@ -90,7 +90,7 @@ class RouteState extends RouteBase { ...@@ -90,7 +90,7 @@ class RouteState extends RouteBase {
Function callback; Function callback;
RouteBase route; RouteBase route;
ComponentState owner; State owner;
bool get isOpaque => false; bool get isOpaque => false;
...@@ -152,15 +152,13 @@ class Navigator extends StatefulComponent { ...@@ -152,15 +152,13 @@ class Navigator extends StatefulComponent {
final NavigatorHistory history; final NavigatorHistory history;
NavigatorState createState() => new NavigatorState(this); NavigatorState createState() => new NavigatorState();
} }
class NavigatorState extends ComponentState<Navigator> { class NavigatorState extends State<Navigator> {
NavigatorState(Navigator config) : super(config);
RouteBase get currentRoute => config.history.currentRoute; RouteBase get currentRoute => config.history.currentRoute;
void pushState(ComponentState owner, Function callback) { void pushState(State owner, Function callback) {
RouteBase route = new RouteState( RouteBase route = new RouteState(
owner: owner, owner: owner,
callback: callback, callback: callback,
......
...@@ -43,11 +43,12 @@ class PopupMenu extends StatefulComponent { ...@@ -43,11 +43,12 @@ class PopupMenu extends StatefulComponent {
final NavigatorState navigator; final NavigatorState navigator;
final WatchableAnimationPerformance performance; final WatchableAnimationPerformance performance;
PopupMenuState createState() => new PopupMenuState(this); PopupMenuState createState() => new PopupMenuState();
} }
class PopupMenuState extends ComponentState<PopupMenu> { class PopupMenuState extends State<PopupMenu> {
PopupMenuState(PopupMenu config) : super(config) { void initState(BuildContext context) {
super.initState(context);
_updateBoxPainter(); _updateBoxPainter();
config.performance.addListener(_performanceChanged); config.performance.addListener(_performanceChanged);
} }
...@@ -75,6 +76,7 @@ class PopupMenuState extends ComponentState<PopupMenu> { ...@@ -75,6 +76,7 @@ class PopupMenuState extends ComponentState<PopupMenu> {
void dispose() { void dispose() {
config.performance.removeListener(_performanceChanged); config.performance.removeListener(_performanceChanged);
super.dispose();
} }
void _performanceChanged() { void _performanceChanged() {
......
...@@ -31,11 +31,12 @@ abstract class ProgressIndicator extends StatefulComponent { ...@@ -31,11 +31,12 @@ abstract class ProgressIndicator extends StatefulComponent {
Widget _buildIndicator(BuildContext context, double performanceValue); Widget _buildIndicator(BuildContext context, double performanceValue);
ProgressIndicatorState createState() => new ProgressIndicatorState(this); ProgressIndicatorState createState() => new ProgressIndicatorState();
} }
class ProgressIndicatorState extends ComponentState<ProgressIndicator> { class ProgressIndicatorState extends State<ProgressIndicator> {
ProgressIndicatorState(ProgressIndicator config) : super(config) { void initState(BuildContext context) {
super.initState(context);
_performance = new AnimationPerformance() _performance = new AnimationPerformance()
..duration = const Duration(milliseconds: 1500) ..duration = const Duration(milliseconds: 1500)
..variable = new AnimatedValue<double>(0.0, end: 1.0, curve: ease); ..variable = new AnimatedValue<double>(0.0, end: 1.0, curve: ease);
...@@ -44,7 +45,6 @@ class ProgressIndicatorState extends ComponentState<ProgressIndicator> { ...@@ -44,7 +45,6 @@ class ProgressIndicatorState extends ComponentState<ProgressIndicator> {
_restartAnimation(); _restartAnimation();
}); });
_performance.play(); _performance.play();
} }
AnimationPerformance _performance; AnimationPerformance _performance;
......
...@@ -28,12 +28,10 @@ class Radio extends StatefulComponent { ...@@ -28,12 +28,10 @@ class Radio extends StatefulComponent {
final Object groupValue; final Object groupValue;
final RadioValueChanged onChanged; final RadioValueChanged onChanged;
RadioState createState() => new RadioState(this); RadioState createState() => new RadioState();
} }
class RadioState extends ComponentState<Radio> { class RadioState extends State<Radio> {
RadioState(Radio config) : super(config);
Color _getColor(BuildContext context) { Color _getColor(BuildContext context) {
ThemeData themeData = Theme.of(context); ThemeData themeData = Theme.of(context);
if (config.value == config.groupValue) if (config.value == config.groupValue)
......
...@@ -21,12 +21,10 @@ class RaisedButton extends MaterialButton { ...@@ -21,12 +21,10 @@ class RaisedButton extends MaterialButton {
assert(enabled != null); assert(enabled != null);
} }
RaisedButtonState createState() => new RaisedButtonState(this); RaisedButtonState createState() => new RaisedButtonState();
} }
class RaisedButtonState extends MaterialButtonState<RaisedButton> { class RaisedButtonState extends MaterialButtonState<RaisedButton> {
RaisedButtonState(RaisedButton config) : super(config);
Color getColor(BuildContext context) { Color getColor(BuildContext context) {
if (config.enabled) { if (config.enabled) {
switch (Theme.of(context).brightness) { switch (Theme.of(context).brightness) {
......
...@@ -9,8 +9,7 @@ import 'dart:sky' as sky; ...@@ -9,8 +9,7 @@ import 'dart:sky' as sky;
import 'package:newton/newton.dart'; import 'package:newton/newton.dart';
import 'package:sky/animation.dart'; import 'package:sky/animation.dart';
import 'package:sky/gestures.dart'; import 'package:sky/gestures.dart';
import 'package:sky/src/rendering/box.dart'; import 'package:sky/rendering.dart';
import 'package:sky/src/rendering/viewport.dart';
import 'package:sky/src/fn3/basic.dart'; import 'package:sky/src/fn3/basic.dart';
import 'package:sky/src/fn3/framework.dart'; import 'package:sky/src/fn3/framework.dart';
import 'package:sky/src/fn3/gesture_detector.dart'; import 'package:sky/src/fn3/gesture_detector.dart';
...@@ -39,8 +38,9 @@ abstract class Scrollable extends StatefulComponent { ...@@ -39,8 +38,9 @@ abstract class Scrollable extends StatefulComponent {
final ScrollDirection scrollDirection; final ScrollDirection scrollDirection;
} }
abstract class ScrollableState<T extends Scrollable> extends ComponentState<T> { abstract class ScrollableState<T extends Scrollable> extends State<T> {
ScrollableState(T config) : super(config) { void initState(BuildContext context) {
super.initState(context);
if (config.initialScrollOffset is double) if (config.initialScrollOffset is double)
_scrollOffset = config.initialScrollOffset; _scrollOffset = config.initialScrollOffset;
_toEndAnimation = new AnimatedSimulation(_setScrollOffset); _toEndAnimation = new AnimatedSimulation(_setScrollOffset);
...@@ -126,6 +126,7 @@ abstract class ScrollableState<T extends Scrollable> extends ComponentState<T> { ...@@ -126,6 +126,7 @@ abstract class ScrollableState<T extends Scrollable> extends ComponentState<T> {
void dispose() { void dispose() {
_stopAnimations(); _stopAnimations();
super.dispose();
} }
void _setScrollOffset(double newScrollOffset) { void _setScrollOffset(double newScrollOffset) {
...@@ -220,12 +221,10 @@ class ScrollableViewport extends Scrollable { ...@@ -220,12 +221,10 @@ class ScrollableViewport extends Scrollable {
final Widget child; final Widget child;
ScrollableViewportState createState() => new ScrollableViewportState(this); ScrollableViewportState createState() => new ScrollableViewportState();
} }
class ScrollableViewportState extends ScrollableState<ScrollableViewport> { class ScrollableViewportState extends ScrollableState<ScrollableViewport> {
ScrollableViewportState(ScrollableViewport config) : super(config);
ScrollBehavior createScrollBehavior() => new OverscrollWhenScrollableBehavior(); ScrollBehavior createScrollBehavior() => new OverscrollWhenScrollableBehavior();
OverscrollWhenScrollableBehavior get scrollBehavior => super.scrollBehavior; OverscrollWhenScrollableBehavior get scrollBehavior => super.scrollBehavior;
...@@ -320,8 +319,6 @@ abstract class ScrollableWidgetList extends Scrollable { ...@@ -320,8 +319,6 @@ abstract class ScrollableWidgetList extends Scrollable {
} }
abstract class ScrollableWidgetListState<T extends ScrollableWidgetList> extends ScrollableState<T> { abstract class ScrollableWidgetListState<T extends ScrollableWidgetList> extends ScrollableState<T> {
ScrollableWidgetListState(T config) : super(config);
/// Subclasses must implement `get itemCount` to tell ScrollableWidgetList /// Subclasses must implement `get itemCount` to tell ScrollableWidgetList
/// how many items there are in the list. /// how many items there are in the list.
int get itemCount; int get itemCount;
...@@ -458,12 +455,10 @@ class ScrollableList<T> extends ScrollableWidgetList { ...@@ -458,12 +455,10 @@ class ScrollableList<T> extends ScrollableWidgetList {
final List<T> items; final List<T> items;
final ItemBuilder<T> itemBuilder; final ItemBuilder<T> itemBuilder;
ScrollableListState<T, ScrollableList<T>> createState() => new ScrollableListState<T, ScrollableList<T>>(this); ScrollableListState<T, ScrollableList<T>> createState() => new ScrollableListState<T, ScrollableList<T>>();
} }
class ScrollableListState<T, Config extends ScrollableList<T>> extends ScrollableWidgetListState<Config> { class ScrollableListState<T, Config extends ScrollableList<T>> extends ScrollableWidgetListState<Config> {
ScrollableListState(Config config) : super(config);
ScrollBehavior createScrollBehavior() { ScrollBehavior createScrollBehavior() {
return config.itemsWrap ? new UnboundedBehavior() : super.createScrollBehavior(); return config.itemsWrap ? new UnboundedBehavior() : super.createScrollBehavior();
} }
...@@ -512,8 +507,6 @@ class PageableList<T> extends ScrollableList<T> { ...@@ -512,8 +507,6 @@ class PageableList<T> extends ScrollableList<T> {
} }
class PageableListState<T> extends ScrollableListState<T, PageableList<T>> { class PageableListState<T> extends ScrollableListState<T, PageableList<T>> {
PageableListState(PageableList<T> config) : super(config);
double _snapScrollOffset(double newScrollOffset) { double _snapScrollOffset(double newScrollOffset) {
double scaledScrollOffset = newScrollOffset / config.itemExtent; double scaledScrollOffset = newScrollOffset / config.itemExtent;
double previousScrollOffset = scaledScrollOffset.floor() * config.itemExtent; double previousScrollOffset = scaledScrollOffset.floor() * config.itemExtent;
......
...@@ -56,12 +56,10 @@ class SnackBar extends AnimatedComponent { ...@@ -56,12 +56,10 @@ class SnackBar extends AnimatedComponent {
final List<SnackBarAction> actions; final List<SnackBarAction> actions;
final SnackBarDismissedCallback onDismissed; final SnackBarDismissedCallback onDismissed;
SnackBarState createState() => new SnackBarState(this); SnackBarState createState() => new SnackBarState();
} }
class SnackBarState extends AnimatedComponentState<SnackBar> { class SnackBarState extends AnimatedState<SnackBar> {
SnackBarState(SnackBar config) : super(config);
void handleDismissed() { void handleDismissed() {
if (config.onDismissed != null) if (config.onDismissed != null)
config.onDismissed(); config.onDismissed();
......
...@@ -397,11 +397,12 @@ class TabBar extends Scrollable { ...@@ -397,11 +397,12 @@ class TabBar extends Scrollable {
final SelectedIndexChanged onChanged; final SelectedIndexChanged onChanged;
final bool isScrollable; final bool isScrollable;
TabBarState createState() => new TabBarState(this); TabBarState createState() => new TabBarState();
} }
class TabBarState extends ScrollableState<TabBar> { class TabBarState extends ScrollableState<TabBar> {
TabBarState(TabBar config) : super(config) { void initState(BuildContext context) {
super.initState(context);
_indicatorAnimation = new ValueAnimation<Rect>() _indicatorAnimation = new ValueAnimation<Rect>()
..duration = _kTabBarScroll ..duration = _kTabBarScroll
..variable = new AnimatedRect(null, curve: ease); ..variable = new AnimatedRect(null, curve: ease);
......
...@@ -23,11 +23,12 @@ abstract class TransitionComponent extends StatefulComponent { ...@@ -23,11 +23,12 @@ abstract class TransitionComponent extends StatefulComponent {
Widget build(BuildContext context); Widget build(BuildContext context);
TransitionComponentState createState() => new TransitionComponentState(this); TransitionState createState() => new TransitionState();
} }
class TransitionComponentState extends ComponentState<TransitionComponent> { class TransitionState extends State<TransitionComponent> {
TransitionComponentState(TransitionComponent config) : super(config) { void initState(BuildContext context) {
super.initState(context);
config.performance.addListener(_performanceChanged); config.performance.addListener(_performanceChanged);
} }
...@@ -40,6 +41,7 @@ class TransitionComponentState extends ComponentState<TransitionComponent> { ...@@ -40,6 +41,7 @@ class TransitionComponentState extends ComponentState<TransitionComponent> {
void dispose() { void dispose() {
config.performance.removeListener(_performanceChanged); config.performance.removeListener(_performanceChanged);
super.dispose();
} }
void _performanceChanged() { void _performanceChanged() {
......
...@@ -21,11 +21,10 @@ class FlipComponent extends StatefulComponent { ...@@ -21,11 +21,10 @@ class FlipComponent extends StatefulComponent {
final Widget left; final Widget left;
final Widget right; final Widget right;
FlipComponentState createState() => new FlipComponentState(this); FlipComponentState createState() => new FlipComponentState();
} }
class FlipComponentState extends ComponentState<FlipComponent> { class FlipComponentState extends State<FlipComponent> {
FlipComponentState(FlipComponent config): super(config);
bool _showLeft = true; bool _showLeft = true;
void flip() { void flip() {
......
...@@ -9,8 +9,7 @@ class RootComponent extends StatefulComponent { ...@@ -9,8 +9,7 @@ class RootComponent extends StatefulComponent {
RootComponentState createState() => new RootComponentState(this); RootComponentState createState() => new RootComponentState(this);
} }
class RootComponentState extends ComponentState<RootComponent> { class RootComponentState extends State<RootComponent> {
RootComponentState(RootComponent widget) : super(widget);
Widget _child = new DecoratedBox(decoration: new BoxDecoration()); Widget _child = new DecoratedBox(decoration: new BoxDecoration());
Widget get child => _child; Widget get child => _child;
void set child(Widget value) { void set child(Widget value) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment