简体   繁体   English

在颤动中的 CustomScrollView 中使用 Dismissible Widget 的正确方法是什么?

[英]What is the correct way to use Dismissible Widget inside CustomScrollView in flutter?

I am trying to put basic DismissibleWidget inside CustomScrollView.我正在尝试将基本的 DismissibleWidget 放在 CustomScrollView 中。 Here is the code这是代码

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool change = false;
  List<String> someList;

  @override
  void initState() {
    super.initState();
    someList = List.generate((20), (index) => 'Data $index');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: Scaffold(
          body: CustomScrollView(
            slivers: <Widget>[
              SliverAppBar(
                title: Text("Dismissible in Slivers"),
              ),
              SliverList(
                delegate: SliverChildBuilderDelegate(
                    (context, index) => Dismissible(
                          key: ValueKey<String>(someList[index]),
                          child: ListTile(
                            title: Text(someList[index]),
                            onTap: () {
                              Navigator.of(context).push(MaterialPageRoute(
                                  builder: (context) =>
                                      SecondPage(someList[index])));
                            },
                          ),
                          onDismissed: (dismissDirection) {
                            someList.remove(index);
                          },
                        ),
                    childCount: someList.length),
              )
            ],
          ),
        ));
  }
}

class SecondPage extends StatelessWidget {
  final String title;

  SecondPage(this.title);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text(this.title),
      ),
    );
  }
}

But when ListTile is tapped and user is routed to SecondPage;但是当点击 ListTile 并且用户被路由到 SecondPage 时; and user navigates back it shows following errors.并且用户导航回它显示以下错误。

A dismissed Dismissible widget is still part of the tree.关闭的 Dismissible 小部件仍然是树的一部分。 Make sure to immediately remove the Dismissible widget from the application once that handler has fired.确保在该处理程序触发后立即从应用程序中删除 Dismissible 小部件。

Normally this would be solved by using ListView.builder when not using Slivers as follow.通常,当不使用 Slivers 时,这将通过使用 ListView.builder 来解决,如下所示。

ListView.builder(
itemCount: someList.length,
itemBuilder: (context, index) => Dismissible(
  key: ValueKey<String>(someList[index]),
  child: ListTile(
    title: Text(someList[index]),
    onTap: () {
      Navigator.of(context).push(MaterialPageRoute(
          builder: (context) =>
              SecondPage(someList[index])));
    },
  ),
  onDismissed: (dissmissDirection) {
    setState(() {
      someList.removeAt(index);
    });
  },
))

But I cannot find way to get rid of this error while using CustomScrollView.但是我在使用 CustomScrollView 时找不到消除这个错误的方法。

You need to use removeAt inside setState您需要在setState使用removeAt
You can copy paste run full code below您可以在下面复制粘贴运行完整代码
code snippet代码片段

 onDismissed: (dismissDirection) {
                        setState(() {
                          someList.removeAt(index);
                        });

                      }

working demo工作演示

在此处输入图片说明

full code完整代码

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';

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

    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }

    class _MyAppState extends State<MyApp> {
      bool change = false;
      List<String> someList;

      @override
      void initState() {
        super.initState();
        someList = List.generate((20), (index) => 'Data $index');
      }

      @override
      Widget build(BuildContext context) {
        return MaterialApp(
            title: 'Flutter Demo',
            theme: ThemeData(
              primarySwatch: Colors.blue,
            ),
            home: Scaffold(
              body: CustomScrollView(
                slivers: <Widget>[
                  SliverAppBar(
                    title: Text("Dismissible in Slivers"),
                  ),
                  SliverList(
                    delegate: SliverChildBuilderDelegate(
                            (context, index) => Dismissible(
                          key: ValueKey<String>(someList[index]),
                          child: ListTile(
                            title: Text(someList[index]),
                            onTap: () {
                              Navigator.of(context).push(MaterialPageRoute(
                                  builder: (context) =>
                                      SecondPage(someList[index])));
                            },
                          ),
                          onDismissed: (dismissDirection) {
                            setState(() {
                              someList.removeAt(index);
                            });

                          },
                        ),
                        childCount: someList.length),
                  )
                ],
              ),
            ));
      }
    }

    class SecondPage extends StatelessWidget {
      final String title;

      SecondPage(this.title);

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: Text(this.title),
          ),
        );
      }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Flutter中的CustomScrollView中使用可禁用的小部件? - How to use a dismissible widget inside a CustomScrollView in Flutter? Flutter:Dismissible 小部件内的 SnackBar 无法正常工作 - Flutter: SnackBar inside Dismissible widget is not working properly Flutter CustomScrollView 中的中心小部件 - Flutter Center Widget in CustomScrollView CustomScrollView 中的 Flutter SliverPersistentHeader 在到达底部小部件之前达到 maxScrollExtend - Flutter SliverPersistentHeader inside CustomScrollView reaches maxScrollExtend before reach to the bottom widget 是否可以在flutter中为SliverList内的按钮实现一个可禁用的窗口小部件 - is it Possible to Implement a Dismissible widget for a button inside a SliverList in flutter 自定义 Flutter 小部件以返回两个小部件以与 CustomScrollView / slivers 一起使用 - Custom Flutter widget to return two widgets for use with CustomScrollView / slivers Flutter 中有没有办法在 Dismissible Widget 中捕捉“开始移动”事件? - Is there a way in Flutter to catch the “start-moving” event in a Dismissible Widget? 将 pushNamed 与 Dismissible 小部件一起使用 - Use pushNamed with Dismissible Widget 如何在 flutter 中使用提供者 state 管理的可关闭小部件 - How to use dismissible widget with provider state management in flutter Flutter在CustomScrollView中使用RefreshIndicator - Flutter use RefreshIndicator in CustomScrollView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM