繁体   English   中英

如何在Flutter中的CustomScrollView中使用可禁用的小部件?

[英]How to use a dismissible widget inside a CustomScrollView in Flutter?

我正在尝试在customcrollview中创建可弃用卡的列表。 卡正在渲染,但是当我滑动卡以将其关闭时,它们不会从列表中删除。 下面是代码。 请帮忙。

  CustomScrollView customScroll = new CustomScrollView(
    slivers: <Widget>[
      new SliverAppBar(
        backgroundColor: Colors.black,
        automaticallyImplyLeading: false,
        expandedHeight: 90.0,
        title: new Text("Test"),
      ),
      new SliverFixedExtentList(
        itemExtent: 128.0,
        delegate: new SliverChildBuilderDelegate(
              (BuildContext context, int index) {
              return new Dismissible(key: new ObjectKey(objects[index]),
              child: widget.widgetAdapter(objects[index]),
              onDismissed: (DismissDirection direction) {
                setState(() {
                  this.objects.removeAt(index);
                  this.reIndex();
                });
                direction == DismissDirection.endToStart ? print(
                    "favourite") : print("remove");
              },
              background: new Container(
                  color: const Color.fromRGBO(183, 28, 28, 0.8),
                  child: const ListTile(
                      leading: const Icon(
                          Icons.delete, color: Colors.white, size: 36.0)
                  )
              ),
              secondaryBackground: new Container(
                  color: const Color.fromRGBO(0, 96, 100, 0.8),
                  child: const ListTile(
                      trailing: const Icon(
                          Icons.favorite, color: Colors.white, size: 36.0)
                  )
              ),
            );
            },
           childCount: objects.length,
        ),
      ),
    ]
);

您的尝试基本上是正确的-我简化了列表的创建并将其替换为下面的示例代码-您正在寻找的是dmiss函数@第35行;

import 'package:flutter/material.dart';

class TestDismissCSV extends StatefulWidget {
  @override
  _TestDismissCSVState createState() => new _TestDismissCSVState();
}

class _TestDismissCSVState extends State<TestDismissCSV> {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: "Dismiss in Cust Scroll V",
      theme: new ThemeData(brightness: Brightness.dark),
      home: new Scaffold(
        body: dmiss(context),
      ),
    );
  }

  List<TheListClass> _theList;

  Widget dmiss(context) {
    return new CustomScrollView(slivers: <Widget>[
      new SliverAppBar(
        backgroundColor: Colors.black,
        automaticallyImplyLeading: false,
        expandedHeight: 90.0,
        title: new Text("Test"),
      ),
      new SliverFixedExtentList(
        itemExtent: 128.0,
        delegate: new SliverChildBuilderDelegate(
          (BuildContext context, int index) {
            return new Dismissible(
              key: new ObjectKey(_theList[index]),
              child: new Material(child: new Text(_theList[index].title)),
              onDismissed: (DismissDirection direction) {
                setState(() {
                  this._theList.removeAt(index);
                  //this.reIndex();
                });
                direction == DismissDirection.endToStart
                    ? print("favourite")
                    : print("remove");
              },
              background: new Container(
                  color: const Color.fromRGBO(183, 28, 28, 0.8),
                  child: const ListTile(
                      leading: const Icon(Icons.delete,
                          color: Colors.white, size: 36.0))),
              secondaryBackground: new Container(
                  color: const Color.fromRGBO(0, 96, 100, 0.8),
                  child: const ListTile(
                      trailing: const Icon(Icons.favorite,
                          color: Colors.white, size: 36.0))),
            );
          },
          childCount: _theList.length,
        ),
      ),
    ]);
  }

  @override
  void initState() {
    super.initState();
    _theList = new List<TheListClass>();

    for (var i = 0; i < 100; i++) {
      _theList.add(new TheListClass('List Item ' + i.toString()));
    }
  }

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

class TheListClass {
  String title;

  TheListClass(this.title);
}

清单项目被驳回

编码愉快!

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM