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

并不能完全解决多Tab滚动联动问题 #40

Closed
CaiJingLong opened this issue Jul 10, 2020 · 3 comments
Closed

并不能完全解决多Tab滚动联动问题 #40

CaiJingLong opened this issue Jul 10, 2020 · 3 comments

Comments

@CaiJingLong
Copy link
Member

CaiJingLong commented Jul 10, 2020

只有当完全滚动至第二个TabView后, 才能"解除"多Tab的滚动联动问题

具体可以看附件内录制的视频

Kapture 2020-07-10 at 18.53.08.mp4.zip

插件版本号 1.0.1

flutter: 1.17.3

代码:

import 'package:flutter/material.dart' hide NestedScrollView;
import 'package:extended_nested_scroll_view/extended_nested_scroll_view.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  TabController tc;
  @override
  void initState() {
    super.initState();
    tc = TabController(length: 4, vsync: this);
    tc.addListener(() {
      index = tc.index;
    });
  }

  int index;

  @override
  void dispose() {
    tc.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 4,
      child: Scaffold(
        body: NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return [
              SliverAppBar(
                pinned: true,
                floating: true,
                forceElevated: innerBoxIsScrolled,
                expandedHeight: 210.0,
                flexibleSpace: FlexibleSpaceBar(
                  title: Text('你好'),
                  background: Container(
                    child: Text('我是内容'),
                  ),
                ),
              ),
            ];
          },
          innerScrollPositionKeyBuilder: () {
            return Key(index.toString());
          },
          body: TabBarView(
            controller: tc,
            children: [
              _buildList(tc, 0),
              _buildList(tc, 1),
              _buildList(tc, 2),
              _buildList(tc, 3),
            ],
          ),
        ),
      ),
    );
  }

  _buildList(TabController tc, int index) {
    return Content(index: index);
  }
}

class Content extends StatefulWidget {
  final int index;

  const Content({
    Key key,
    this.index,
  }) : super(key: key);

  @override
  ContentState createState() => ContentState();
}

class ContentState extends State<Content> with AutomaticKeepAliveClientMixin {
  Key key;

  @override
  void initState() {
    key = Key(widget.index.toString());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return NotificationListener<ScrollNotification>(
      onNotification: (notification) {
        return true;
      },
      child: NestedScrollViewInnerScrollPositionKeyWidget(
        key,
        ListView.builder(
          itemBuilder: (context, index) {
            return ListTile(
              title: Text(index.toString()),
            );
          },
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
@zmtzawqlp
Copy link
Member

1.17.0 需要再研究

@zplblue
Copy link

zplblue commented Nov 12, 2020

所以有对后续的版本提供支持吗?

@clong1995
Copy link

同样遇到了这个问题

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

No branches or pull requests

4 participants