1

在我的应用程序SliverPersistentHeader中,我有一个父级和内部我想有另一个SliverListSliverPersistentHeader我在下面有一个伪代码,我得到

应用父数据时引发以下断言。: ParentDataWidget 的使用不正确。

ParentDataWidget Expanded(flex: 1) 想要将 FlexParentData 类型的 ParentData 应用于 RenderObject,该 RenderObject 已设置为接受不兼容类型 ParentData 的 ParentData。

通常,这意味着 Expanded 小部件具有错误的祖先 RenderObjectWidget。通常,扩展小部件直接放置在 Flex 小部件内。有问题的 Expanded 当前放置在 RepaintBoundary 小部件中。

接收到不兼容父数据的 RenderObject 的所有权链为: RepaintBoundary ← NotificationListener ← GlowingOverscrollIndicator ← Scrollable ← _NestedScrollViewCustomScrollView ← Builder ← _InheritedNestedScrollView ← NestedScrollView ← Expanded ← RepaintBoundary ← </p>

════════ 渲染库捕获的异常═══════ RenderBox 未布局:RenderNestedScrollViewViewport#e9060 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 'package:flutter/src/rendering /box.dart':断言失败:第 1785 行 pos 12:'hasSize' 相关的导致错误的小部件是:
NestedScrollView file:///C:/Users/mahdi/AndroidStudioProjects/instacheeta/lib/nested.dart:41: 26═════════════════════════════════════════════════ ═════

错误,当我尝试删除Expanded小部件时,我得到另一个错误。

我该如何解决?

import 'package:flutter/material.dart';

void main()=>runApp(MySliverApp());

class MySliverApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'sample',
      home: MySilverHomeApp(),
    );
  }
}

class MySilverHomeApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        physics: const ClampingScrollPhysics(),
        slivers: <Widget>[
          SliverPersistentHeader(
            pinned: false,
            floating: true,
            delegate: CustomAutoHideAppBarDelegate(
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 12.0),
                child: Row(
                  children:const <Widget>[
                     Text('SAMPLE')
                  ],
                ),
              ),
            ),
          ),
          SliverPadding(
            padding: const EdgeInsets.all(0),
            sliver: SliverList(
              delegate: SliverChildListDelegate([
                Expanded(
                  child: NestedScrollView(
                    headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
                      return <Widget>[
                        SliverList(
                          delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
                            return Column(
                              children:const <Widget>[
                                Text('SAMPLE'),
                                Text('SAMPLE')
                              ],
                            );
                          }, childCount: 1),
                        ),
                        SliverPersistentHeader(
                          pinned: true,
                          floating: true,
                          delegate: ContestTabHeader(
                            Text('SAMPLE'),
                          ),
                        ),
                      ];
                    },
                    body: ListView.builder(itemBuilder: (context,index){
                      return Container(
                        child: Text('$index'),
                      );
                    },itemCount: 100,)),
                  ),
              ]),
            ),
          ),
        ],
      ),
    );
  }
}

class CustomAutoHideAppBarDelegate extends SliverPersistentHeaderDelegate {
  const CustomAutoHideAppBarDelegate({
    @required this.child,
  });

  final Widget child;

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    final theme = Theme.of(context);
    return SizedBox.expand(
      child: Material(
        color: Colors.white,
        elevation: 3,
        child: Padding(
          padding: EdgeInsets.only(
            top: MediaQuery.of(context).padding.top,
          ),
          child: DefaultTextStyle.merge(
            style: theme.primaryTextTheme.subtitle1,
            child: IconTheme.merge(
              data: theme.primaryIconTheme,
              child: child,
            ),
          ),
        ),
      ),
    );
  }

  @override
  double get minExtent => kToolbarHeight;

  @override
  double get maxExtent => kToolbarHeight;

  @override
  bool shouldRebuild(CustomAutoHideAppBarDelegate oldDelegate) => child != oldDelegate.child;
}
class ContestTabHeader extends SliverPersistentHeaderDelegate {
  ContestTabHeader(
      this.searchUI,
      );

  final Widget searchUI;

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    return searchUI;
  }

  @override
  double get maxExtent => 52.0;

  @override
  double get minExtent => 52.0;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return false;
  }
}
4

0 回答 0