Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrollable.ensureVisible(context) broken due to incorrect childScrollOffset() #59

Open
spkersten opened this issue Mar 29, 2021 · 2 comments · May be fixed by #60
Open

Scrollable.ensureVisible(context) broken due to incorrect childScrollOffset() #59

spkersten opened this issue Mar 29, 2021 · 2 comments · May be fixed by #60

Comments

@spkersten
Copy link

spkersten commented Mar 29, 2021

When using Scrollable.ensureVisible(context) on a child of SliverStickyHeader with overlapsContent: true, the Scrollable scrolls to the wrong position.

The problem appears to be in childScrollOffset (https://github.com/letsar/flutter_sticky_header/blob/master/lib/src/rendering/sliver_sticky_header.dart#L346) which returns _headerExtent even with overlapping headers. I think that should be headerLogicalExtent (= overlapsContent ? 0.0 : _headerExtent).

@letsar
Copy link
Owner

letsar commented Jul 13, 2022

Is it still the case?

@rohansohonee1
Copy link

We are facing an issue where the header body items are getting scrolled when expanding the header.
The issue starts after version 0.6.3 i.e starting from commit (version 0.6.4 through latest 0.7.0)

Reproducible code
import 'package:flutter/material.dart';
import 'package:flutter_sticky_header/flutter_sticky_header.dart';
import 'package:sliver_tools/sliver_tools.dart';

void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _showHeaderBody = false;

  final _duration = Duration(milliseconds: 200);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text("Flutter Sticky Header Issue")),
        body: CustomScrollView(
          slivers: [
            MultiSliver(
              pushPinnedChildren: true,
              children: [
                Builder(
                  builder: (ctx) => SliverStickyHeader(
                    header: _buildHeader(ctx, 'Header', _showHeaderBody,
                        callback: (showHeader) =>
                            setState(() => _showHeaderBody = showHeader)),
                    sliver: SliverToBoxAdapter(
                      child: _buildBody(_showHeaderBody),
                    ),
                  ),
                ),
                Builder(
                  builder: (ctx) => SliverStickyHeader(
                    header: _buildHeader(ctx, 'Header 2', true),
                    sliver: SliverToBoxAdapter(child: _buildBody(true)),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Future<void> _showHeaderOnScreen(BuildContext ctx) async {
    await Future.delayed(_duration);
    WidgetsBinding.instance.addPostFrameCallback((_) {
      Scrollable.ensureVisible(ctx, duration: _duration);
    });
  }

  Widget _buildHeader(
    BuildContext ctx,
    String header,
    bool showHeader, {
    void Function(bool)? callback,
  }) {
    return ListTile(
      title: Text(header),
      onTap: () async {
        final expand = !showHeader;
        if (expand) await _showHeaderOnScreen(ctx);
        callback?.call(expand);
      },
    );
  }

  Widget _buildBody(bool expanded) {
    return AnimatedCrossFade(
      firstChild: Container(height: 0.0),
      secondChild: Column(
        children: List.generate(50, (index) => Text(index.toString())),
      ),
      crossFadeState:
          expanded ? CrossFadeState.showSecond : CrossFadeState.showFirst,
      duration: _duration,
    );
  }
}

Video with issue

issue.mov

Video without issue using version 0.6.3

no.issue.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants