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

Don't use one refreshController to multiple SmartRefresher,It will cause some unexpected bugs mostly in TabBarView #649

Open
Asmewill opened this issue Jun 21, 2024 · 2 comments

Comments

@Asmewill
Copy link

No description provided.

@Asmewill
Copy link
Author

这个问题,怎么解决啊

@anjarnaufals
Copy link

Hello @Asmewill
maybe you can make the children of the TabBarView, each have their own SmartRefresher and RefreshController,
hope this can help

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

  @override
  State<Example> createState() =>
      _ExampleState();
}

class _ExampleState
    extends State<Example>
    with SingleTickerProviderStateMixin {
  late final TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 2, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        bottom: TabBar(
          controller: _tabController,
          tabs: const [
            Tab(
              text: 'Tab 1',
            ),
            Tab(
              text: 'Tab 2',
            ),
          ],
        ),
      ),
      body: TabBarView(
        controller: _tabController,
        children: const [
          _TabOne(),
          _TabTwo(),
        ],
      ),
    );
  }
}

class _TabOne extends StatefulWidget {
  const _TabOne();

  @override
  State<_TabOne> createState() => _TabOneState();
}

class _TabOneState extends State<_TabOne> {
  final RefreshController _refreshController = RefreshController();

  Future<void> testRefresh() async {
    _refreshController.requestRefresh();
    await Future.delayed(Durations.extralong4);
    _refreshController.refreshCompleted();
  }

  Future<void> testLoadMore() async {
    _refreshController.requestLoading();
    await Future.delayed(Durations.extralong4);
    _refreshController.loadComplete();
  }

  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
      enablePullDown: true,
      enablePullUp: true,
      onRefresh: () async {
        testRefresh();
      },
      onLoading: () {
        testLoadMore();
      },
      header: const ClassicHeader(),
      controller: _refreshController,
      child: CustomScrollView(
        physics: const BouncingScrollPhysics(),
        slivers: [
          SliverList.builder(
            itemCount: 32,
            itemBuilder: (_, i) => ListTile(
              title: Text('list index - $i'),
            ),
          ),
        ],
      ),
    );
  }
}

class _TabTwo extends StatefulWidget {
  const _TabTwo();

  @override
  State<_TabTwo> createState() => _TabTwoState();
}

class _TabTwoState extends State<_TabTwo> {
  final RefreshController _refreshController = RefreshController();

  Future<void> testRefresh() async {
    _refreshController.requestRefresh();
    await Future.delayed(Durations.extralong4);
    _refreshController.refreshCompleted();
  }

  Future<void> testLoadMore() async {
    _refreshController.requestLoading();
    await Future.delayed(Durations.extralong4);
    _refreshController.loadComplete();
  }

  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
      enablePullDown: true,
      enablePullUp: true,
      onRefresh: () async {
        testRefresh();
      },
      onLoading: () {
        testLoadMore();
      },
      header: const ClassicHeader(),
      controller: _refreshController,
      child: ListView.builder(
        itemCount: 32,
        itemBuilder: (_, i) => ListTile(
          title: Text('list index - $i'),
        ),
      ),
    );
  }
}

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

2 participants