Don't size Android views to (0,0) (#20956)

Virtual displays must have a non zero size. This was causing a crash:
https://github.com/flutter/flutter/issues/20456
This commit is contained in:
amirh 2018-08-23 12:29:29 -07:00 committed by GitHub
parent 3fcc1069e9
commit 1ba16d55f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 2 deletions

View File

@ -100,7 +100,10 @@ class RenderAndroidView extends RenderBox {
Size _currentAndroidViewSize;
Future<Null> _sizePlatformView() async {
if (_state == _PlatformViewState.resizing) {
// Android virtual displays cannot have a zero size.
// Trying to size it to 0 crashes the app, which was happening when starting the app
// with a locked screen (see: https://github.com/flutter/flutter/issues/20456).
if (_state == _PlatformViewState.resizing || size.isEmpty) {
return;
}

View File

@ -427,7 +427,8 @@ class AndroidViewController {
/// Sizes the Android View.
///
/// `size` is the view's new size in logical pixel, and must not be null.
/// `size` is the view's new size in logical pixel, it must not be null and must
/// be bigger than zero.
///
/// The first time a size is set triggers the creation of the Android view.
Future<void> setSize(Size size) async {
@ -435,6 +436,7 @@ class AndroidViewController {
throw new FlutterError('trying to size a disposed Android View. View id: $id');
assert(size != null);
assert(!size.isEmpty);
if (_state == _AndroidViewState.waitingForSize)
return _create(size);

View File

@ -37,6 +37,26 @@ void main() {
);
});
testWidgets('Zero sized Android view is not created', (WidgetTester tester) async {
final FakePlatformViewsController viewsController = new FakePlatformViewsController(TargetPlatform.android);
viewsController.registerViewType('webview');
await tester.pumpWidget(
const Center(
child: SizedBox(
width: 0.0,
height: 0.0,
child: AndroidView(viewType: 'webview', layoutDirection: TextDirection.ltr),
),
),
);
expect(
viewsController.views,
isEmpty,
);
});
testWidgets('Resize Android view', (WidgetTester tester) async {
final int currentViewId = platformViewsRegistry.getNextPlatformViewId();
final FakePlatformViewsController viewsController = new FakePlatformViewsController(TargetPlatform.android);