【问题标题】:AndroidView displays full screen only in FlutterAndroidView 仅在 Flutter 中显示全屏
【发布时间】:2021-07-28 12:07:16
【问题描述】:

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

我想要实现的是这个 -> 有几个 Flutter Widgets,Android Native View (widget) 介于两者之间。在一个屏幕中的 Flutter App 中,我需要显示来自 Native Android 代码的视图,但是当我作为 Scaffold 中的唯一元素添加时,我从 Android Native 接收到 Flutter 的视图会全屏显示。当添加到ListView 中时,就像示例一样,我收到错误消息。

我需要这个 Android Native 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)

【问题讨论】:

    标签: android flutter view native


    【解决方案1】:

    找到了方法。

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

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

    【讨论】:

      猜你喜欢
      • 2019-05-31
      • 2018-03-20
      • 1970-01-01
      • 2022-12-11
      • 2023-04-10
      • 2018-09-25
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多