AndroidView仅在Flutter中显示全屏

AndroidView displays full screen only in Flutter

我在从 Android 本机代码向 Flutter 显示 TextView(或任何类型的 view)时遇到问题。

我想要实现的是这个 -> 要有几个 Flutter Widgets,Android Native View (widget) 在它们之间。在一个屏幕的 Flutter 应用程序中,我需要显示来自本机 Android 代码的视图,但是当我在 Scaffold。当在 ListView 中添加时,就像下面的例子一样,我收到一个错误。

我需要这个 Android 本机 view 就像我在 Flutter 中使用 Text Widget,在其他一些小部件之间。

遵循此文档:https://flutter.dev/docs/development/platform-integration/platform-views

  @override
  Widget build(BuildContext context) {
    // This is used in the platform side to register the view.
    final String viewType = 'hybrid-view-type';
    // Pass parameters to the platform side.
    final Map<String, dynamic> creationParams = <String, dynamic>{};

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            Flexible(
              child: ListView(
                physics: const AlwaysScrollableScrollPhysics(),
                scrollDirection: Axis.vertical,
                shrinkWrap: true,
                children: [
                  PlatformViewLink(
                    viewType: viewType,
                    surfaceFactory: (BuildContext context, PlatformViewController controller) {
                      return AndroidViewSurface(
                        controller: controller,
                        gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{},
                        hitTestBehavior: PlatformViewHitTestBehavior.opaque,
                      );
                    },
                    onCreatePlatformView: (PlatformViewCreationParams params) {
                      return PlatformViewsService.initSurfaceAndroidView(
                        id: params.id,
                        viewType: viewType,
                        layoutDirection: TextDirection.ltr,
                        creationParams: creationParams,
                        creationParamsCodec: StandardMessageCodec(),
                      )
                        ..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
                        ..create();
    },),],),),],),),);
  }

错误:

======== Exception caught by rendering library =====================================================
The following assertion was thrown during performLayout():
'package:flutter/src/rendering/object.dart': Failed assertion: line 1706 pos 12: '!_debugDoingThisLayout': is not true.


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=2_bug.md

The relevant error-causing widget was: 
  ListView file:///C:/Repo/Test_Repo/flutter-hybrid-composition-demo/lib/main.dart:85:22
When the exception was thrown, this was the stack: 
#2      RenderObject.layout (package:flutter/src/rendering/object.dart:1706:12)
#3      RenderSliverList.performLayout (package:flutter/src/rendering/sliver_list.dart:213:28)
#4      RenderObject.layout (package:flutter/src/rendering/object.dart:1779:7)
#5      RenderSliverEdgeInsetsPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:137:12)
#6      RenderSliverPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:371:11)
...
The following RenderObject was being processed when the exception was fired: RenderSliverList#65918 relayoutBoundary=up14
...  needs compositing
...  parentData: paintOffset=Offset(0.0, 0.0) (can use size)
...  constraints: SliverConstraints(AxisDirection.down, GrowthDirection.forward, ScrollDirection.reverse, scrollOffset: 965.8, remainingPaintExtent: 603.4, crossAxisExtent: 411.4, crossAxisDirection: AxisDirection.right, viewportMainAxisExtent: 603.4, remainingCacheExtent: 1103.4, cacheOrigin: -250.0)
...  geometry: SliverGeometry(scrollExtent: Infinity, paintExtent: 603.4, maxPaintExtent: Infinity, hasVisualOverflow: true, cacheExtent: 853.4)
...    scrollExtent: Infinity
...    paintExtent: 603.4
...    maxPaintExtent: Infinity
...    hasVisualOverflow: true
...    cacheExtent: 853.4
...  currently live children: 0 to 0
RenderObject: RenderSliverList#65918 relayoutBoundary=up14
  needs compositing
  parentData: paintOffset=Offset(0.0, 0.0) (can use size)
  constraints: SliverConstraints(AxisDirection.down, GrowthDirection.forward, ScrollDirection.reverse, scrollOffset: 965.8, remainingPaintExtent: 603.4, crossAxisExtent: 411.4, crossAxisDirection: AxisDirection.right, viewportMainAxisExtent: 603.4, remainingCacheExtent: 1103.4, cacheOrigin: -250.0)
  geometry: SliverGeometry(scrollExtent: Infinity, paintExtent: 603.4, maxPaintExtent: Infinity, hasVisualOverflow: true, cacheExtent: 853.4)
    scrollExtent: Infinity
    paintExtent: 603.4
    maxPaintExtent: Infinity
    hasVisualOverflow: true
    cacheExtent: 853.4
  currently live children: 0 to 0
...  child with index 0: RenderIndexedSemantics#e0d09 relayoutBoundary=up15
...    needs compositing
...    parentData: index=0; layoutOffset=0.0 (can use size)
...    constraints: BoxConstraints(w=411.4, 0.0<=h<=Infinity)
...    semantic boundary
...    size: Size(411.4, Infinity)
...    index: 0
...    child: RenderRepaintBoundary#a66fc relayoutBoundary=up16
...      needs compositing
...      parentData: <none> (can use size)
...      constraints: BoxConstraints(w=411.4, 0.0<=h<=Infinity)
...      layer: OffsetLayer#23514
...        engine layer: OffsetEngineLayer#cf5b5
...        offset: Offset(0.0, -964.7)
...      size: Size(411.4, Infinity)
...      metrics: 92.0% useful (2 bad vs 23 good)
...      diagnosis: this is an outstandingly useful repaint boundary and should definitely be kept
...      child: RenderSemanticsAnnotations#77a02 relayoutBoundary=up17
...        needs compositing
...        parentData: <none> (can use size)
...        constraints: BoxConstraints(w=411.4, 0.0<=h<=Infinity)
...        size: Size(411.4, Infinity)
...        child: PlatformViewRenderBox#728cc
...          needs compositing
...          parentData: <none> (can use size)
...          constraints: BoxConstraints(w=411.4, 0.0<=h<=Infinity)
...          layer: OffsetLayer#e89f7
...            engine layer: OffsetEngineLayer#ee38b
...            offset: Offset(0.0, 0.0)
...          semantic boundary
...          size: Size(411.4, Infinity)

找到方法了。

显然我可以像这样为 PlatformViewLink 使用包装器 Widget

  Row(
    mainAxisAlignment: MainAxisAlignment.center,
    children: <Widget>[
      Container(
        width: 300, 
        height: 250, 
        child: PlatformViewLink(
          ...
          ...
        ),
      ],
    ),