SliverGeometry 无效:“maxPaintExtent”小于 Flutter 中的“paintExtent”

问题描述 投票:0回答:3

我的整个应用程序在模拟器和 Moto-G5S 中完美运行,但我发现在 Samsung Galaxy A6+ 的 SliverGrid View 中加载数据时出错。 我的错误是“maxPaintExtent小于paintExtent,maxPaintExtent是177.5,但是paintExtent是612.0。根据定义,条子不能绘制超过它可以绘制的最大值!”

我尝试了不同的 SliverGrid gridDelegate,例如 maxCrossAxisExtent: 300.0、mainAxisSpacing: 5.0、crossAxisSpacing: 5.0、childAspectRatio: 1.0 但我仍然没有修复它。 我如何设置 maxPaintExtent? 预先感谢。

系列.dart

class SeriesScreen extends StatefulWidget {
  SeriesScreen({Key key}) : super(key: key);

  @override
  _SeriesScreenState createState() => new _SeriesScreenState();
}

class _SeriesScreenState extends State<SeriesScreen> {
  bool isHomeDataLoading;

  @override
  void initState() {
    super.initState();
    isHomeDataLoading = false;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: ComComp.getAppBar(COLORS.SERIES_THEME_COLOR, STRING_NAME.SERIES_MOVIE),
      body: CustomScrollView(
        shrinkWrap: false,
        slivers: <Widget>[
          makeHeader(STRING_NAME.MOVIE),
          Container(
            child: FutureBuilder(
                future: Services.getMovies(),
                initialData: [],
                builder: (context, snapshot) {
                  //if (snapshot.connectionState == ConnectionState.done) {
                    return ComComp.sliverGridMovies(snapshot, movieGridClicked);
                  //} else {
                  //  ComComp.circularProgress(COLORS.SERIES_THEME_COLOR);
                 // }
                }),
          ),

          makeHeader(STRING_NAME.SERIES),
          Container(
            child: FutureBuilder(
                future: Services.getSeries(),
                initialData: [],
                builder: (context, snapshot) {
                  //if (snapshot.connectionState == ConnectionState.done) {
                    return ComComp.sliverGridSeries(snapshot, seriesGridClicked);
                  //} else {
                  //  ComComp.circularProgress(COLORS.SERIES_THEME_COLOR);
                  //}
                }),
          ),
        ],
      ),
    );
  }

  setLoading(bool loading) {
    setState(() {
      isHomeDataLoading = loading;
    });
  }

  fetch() {setLoading(true);}
}

SliverPersistentHeader makeHeader(String headerText) {
  return SliverPersistentHeader(
    pinned: true,
    delegate: _SliverAppBarDelegate(
      minHeight: 60.0,
      maxHeight: 130.0,
      child: Container(
        //color: COLORS.COMPLY,
          color: COLORS.CRAFT_THEME_COLOR,
          child: Center(
            child: Text(headerText,
              style: TextStyle(
                  fontFamily: 'Chewy', fontWeight: FontWeight.w500,
                  color: Colors.white, fontSize: 32, letterSpacing: 2.0),
            ),
          )
      ),
    ),
  );
}



class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  _SliverAppBarDelegate({
    @required this.minHeight,
    @required this.maxHeight,
    @required this.child,
  });

  final double minHeight;
  final double maxHeight;
  final Widget child;
  @override
  double get minExtent => minHeight;
  @override
  double get maxExtent => math.max(maxHeight, minHeight);
  @override
  Widget build(
      BuildContext context,
      double shrinkOffset,
      bool overlapsContent)
  {
    return SizedBox.expand(child: child);
  }
  @override
  bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
    return maxHeight != oldDelegate.maxHeight;
  }
}

movieGridClicked(BuildContext context, CellModel model) {
  // Grid Click
      Navigator.push(context,
          MaterialPageRoute(
              builder: (context) => VideoPlayerScreen(model.title, model.url)));
  //SnackBar Msg:
  //Scaffold.of(context).showSnackBar(new SnackBar(content: Text(model.title),));
}

seriesGridClicked(BuildContext context, SeriesModel model) {
  // Grid Click
      Navigator.push(context,
          MaterialPageRoute(
              builder: (context) => EpisodeScreen(model.id.toString(), model.title)));
  //SnackBar Msg:
  //Scaffold.of(context).showSnackBar(new SnackBar(content: Text(model.title),));
}

comcomp.dart

static SliverGrid sliverGridMovies (AsyncSnapshot snapshot, Function gridClicked){
    var values = snapshot.data;

    return SliverGrid(
      delegate: SliverChildBuilderDelegate(
            (BuildContext context, int index) {
          return GestureDetector(
            child: Cell(COLORS.SERIES_THEME_COLOR, values[index]),
            onTap: () => gridClicked(context, values[index]),
          );
        },
        childCount: values == null ? 0 : values.length,
      ),
      gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
        maxCrossAxisExtent: 300.0,
        mainAxisSpacing: 5.0,
        crossAxisSpacing: 5.0,
        childAspectRatio: 1.0,
      ),
    );
  }

错误:

I/flutter (21918): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═══
I/flutter (21918): The following assertion was thrown during performLayout():
I/flutter (21918): SliverGeometry is not valid: The "maxPaintExtent" is less than the "paintExtent".
I/flutter (21918): The maxPaintExtent is 177.5, but the paintExtent is 612.0. By definition, a sliver can't paint more
I/flutter (21918): than the maximum that it can paint!
I/flutter (21918): The RenderSliver that returned the offending geometry was:
I/flutter (21918):   RenderSliverGrid#7ddfe relayoutBoundary=up1 NEEDS-LAYOUT NEEDS-PAINT
I/flutter (21918):   creator: SliverGrid ← FutureBuilder<List<dynamic>> ← Container ← Viewport ←
I/flutter (21918):   IgnorePointer-[GlobalKey#8bde6] ← Semantics ← Listener ← _GestureSemantics ←
I/flutter (21918):   RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#55565] ← Listener ← _ScrollableScope
I/flutter (21918):   ← _ScrollSemantics-[GlobalKey#062a8] ← ⋯
I/flutter (21918):   parentData: paintOffset=Offset(0.0, 0.0) (can use size)
I/flutter (21918):   constraints: SliverConstraints(AxisDirection.down, GrowthDirection.forward,
I/flutter (21918):   ScrollDirection.forward, scrollOffset: 379.7, remainingPaintExtent: 612.0, overlap: 60.0,
I/flutter (21918):   crossAxisExtent: 360.0, crossAxisDirection: AxisDirection.right, viewportMainAxisExtent: 612.0,
I/flutter (21918):   remainingCacheExtent: 1112.0 cacheOrigin: -250.0 )
I/flutter (21918):   geometry: SliverGeometry(scrollExtent: 177.5, paintExtent: 612.0, maxPaintExtent: 177.5,
I/flutter (21918):   hasVisualOverflow: true, cacheExtent: 1112.0)
I/flutter (21918):   currently live children: 0 to 1

I/flutter (21918)════════════════════════════════════════════════════
I/flutter (21918): Another exception was thrown: SliverGeometry is not valid: The "maxPaintExtent" is less than the "paintExtent".
I/chatty  (21918): uid=10250(com.globaltoons.globaltoons_mobile) 1.ui identical 1 line
I/flutter (21918): Another exception was thrown: SliverGeometry is not valid: The "maxPaintExtent" is less than the "paintExtent".
D/ViewRootImpl@93b5099[MainActivity](21918): ViewPostIme pointer 0
D/ViewRootImpl@93b5099[MainActivity](21918): ViewPostIme pointer 1
D/ViewRootImpl@93b5099[MainActivity](21918): ViewPostIme pointer 0
flutter
3个回答
0
投票

我也有同样的问题。

我更新了flutter和dart版本,重启了ide。

然后在“工具”选项卡下单击“flutter”,然后单击“flutter clean”,然后单击“flutter Upgrade”

这因某种原因解决了问题


0
投票

我在重构我的小部件代码时遇到了同样的错误 当我的代码是这样的时候就发生了

SliverPersistentHeader(
  pinned: true,
  delegate: CustomDelegate(
    height: 100,
    color: Colors.grey,
    child: headerWidget, // Simple container with text widget
  ),
)

用Column包裹headerWidget,错误消失了

SliverPersistentHeader(
  pinned: true,
  delegate: CustomDelegate(
    height: 100,
    color: Colors.grey,
    child: Column( // ----> Wrap headerWidget with Column
      children: [
        // Simple container with text widget
        headerWidget,
      ],
    ),
  ),
)

您可以根据自己的情况尝试一下

调试愉快


-1
投票
  • GridView / CustomScrollView 随机断言失败,具体取决于 childAspectRatio 值和屏幕分辨率

如何解决

解决方案1

    return new CustomScrollView(
        slivers: <Widget>[
            new SliverPadding(
                padding: const EdgeInsets.all( 20.0 ),
                sliver: SliverGrid.count(
                    crossAxisCount:   2,
                    crossAxisSpacing: 10.0,
                    childAspectRatio: 4.8,
                    children:         items,
                ),
            ),
        ],
    );

解决方案2

return new GridView.count(
            crossAxisSpacing: 10.0,
            childAspectRatio: 4.8,
            crossAxisCount:   2,
            children:         items,
        );

解决方案3

此问题似乎取决于屏幕尺寸,因为它仅出现在某些模拟设备上,而不出现在其他设备上。

由于问题来自 sliver.dart,我无法从外部做太多事情:

    if (maxPaintExtent < paintExtent) {
            verify(false,
              'The "maxPaintExtent" is less than the "paintExtent".\n' +
              _debugCompareFloats('maxPaintExtent', maxPaintExtent, 'paintExtent', paintExtent) +
              'By definition, a sliver can\'t paint more than the maximum that it can paint!'
            );
          }

© www.soinside.com 2019 - 2024. All rights reserved.