Flutter implementing nested SliverPersistentHeader

596 views Asked by At

In my Application i have a SliverPersistentHeader in parent and inside that i want to have another SliverList with SliverPersistentHeader, i have a pseudo code in below and i get

The following assertion was thrown while applying parent data.: Incorrect use of ParentDataWidget.

The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type FlexParentData to a RenderObject, which has been set up to accept ParentData of incompatible type ParentData.

Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically, Expanded widgets are placed directly inside Flex widgets. The offending Expanded is currently placed inside a RepaintBoundary widget.

The ownership chain for the RenderObject that received the incompatible parent data was: RepaintBoundary ← NotificationListener ← GlowingOverscrollIndicator ← Scrollable ← _NestedScrollViewCustomScrollView ← Builder ← _InheritedNestedScrollView ← NestedScrollView ← Expanded ← RepaintBoundary ← ⋯

════════ Exception caught by rendering library ═══════ RenderBox was not laid out: RenderNestedScrollViewViewport#e9060 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 'package:flutter/src/rendering/box.dart': Failed assertion: line 1785 pos 12: 'hasSize' The relevant error-causing widget was:
NestedScrollView file:///C:/Users/mahdi/AndroidStudioProjects/instacheeta/lib/nested.dart:41:26 ══════════════════════════════════════════════════════

error, and when i try to remove Expanded widget i get another error.

how can i solve that?

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;
  }
}
0

There are 0 answers