Commit de448c20 authored by Dragoș Tiselice's avatar Dragoș Tiselice Committed by GitHub

Added LayoutChangedNotifier. (#5304)

Added a simple widget that automatically dispatches a
LayoutChangedNotification when its child changes layout.
parent fd119f4f
......@@ -656,5 +656,4 @@ class _InkHighlight extends InkFeature implements InkHighlight {
_paintHighlight(canvas, rect.shift(originOffset), paint);
}
}
}
// 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:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
/// Indicates that the size of one of the descendants of the object receiving
/// this notification has changed, and that therefore any assumptions about that
/// layout are no longer valid.
///
/// See [LayoutChangedNotification].
class SizeChangedLayoutNotificaion extends LayoutChangedNotification {}
/// A widget that automatically dispatches a [SizeChangedLayoutNotifier] when
/// the layout of its child changes.
///
/// Useful especially when having some complex, layout-changing animation within
/// [Material] that is also interactive.
class SizeChangedLayoutNotifier extends SingleChildRenderObjectWidget {
/// Creates a [SizeChangedLayoutNotifier] that dispatches layout changed
/// notifications when [child] changes layout.
SizeChangedLayoutNotifier({
Key key,
Widget child
}) : super(key: key, child: child);
@override
_RenderSizeChangedWithCallback createRenderObject(BuildContext context) {
return new _RenderSizeChangedWithCallback(
onLayoutChangedCallback: () {
new SizeChangedLayoutNotificaion().dispatch(context);
}
);
}
}
class _RenderSizeChangedWithCallback extends RenderProxyBox {
_RenderSizeChangedWithCallback({
RenderBox child,
this.onLayoutChangedCallback
}) : super(child);
VoidCallback onLayoutChangedCallback;
Size _oldSize;
@override
void performLayout() {
super.performLayout();
if (onLayoutChangedCallback != null && size != _oldSize)
onLayoutChangedCallback();
_oldSize = size;
}
}
......@@ -50,6 +50,7 @@ export 'src/widgets/scrollable_grid.dart';
export 'src/widgets/scrollable_list.dart';
export 'src/widgets/scrollable.dart';
export 'src/widgets/semantics_debugger.dart';
export 'src/widgets/size_changed_layout_notifier.dart';
export 'src/widgets/status_transitions.dart';
export 'src/widgets/table.dart';
export 'src/widgets/text_selection.dart';
......
// 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 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
class NotifyMaterial extends StatelessWidget {
@override
Widget build(BuildContext context) {
new LayoutChangedNotification().dispatch(context);
return new Container();
}
}
void main() {
testWidgets('SizeChangedLayoutNotification test', (WidgetTester tester) async {
bool notified = false;
await tester.pumpWidget(
new NotificationListener<LayoutChangedNotification>(
onNotification: (LayoutChangedNotification notification) {
notified = true;
return true;
},
child: new SizeChangedLayoutNotifier(
child: new SizedBox(
width: 100.0,
height: 100.0
)
)
)
);
await tester.pumpWidget(
new NotificationListener<LayoutChangedNotification>(
onNotification: (LayoutChangedNotification notification) {
notified = true;
return true;
},
child: new SizeChangedLayoutNotifier(
child: new SizedBox(
width: 200.0,
height: 100.0
)
)
)
);
expect(notified, isTrue);
});
}
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