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

问题描述 投票:0回答:1

我正在尝试在海关查看中创建一张可以拒绝的卡片列表。卡片正在渲染,但是当我刷卡以解除它们时,它们不会从列表中删除。下面是代码。请帮忙。

  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,
        ),
      ),
    ]
);
dart flutter custom-scrolling
1个回答
1
投票

你的尝试基本上是正确的 - 我已经简化了列表创建并将其替换为下面的示例代码 - 你要找的是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);
}

List Item dismissed

快乐的编码!

© www.soinside.com 2019 - 2024. All rights reserved.