簡體   English   中英

如何在 Flutter 中的 ListView 上添加可關閉的小部件?

[英]How do I add a Dismissible Widget on my ListView in Flutter?

我有一個列表視圖,我想向其添加一個可關閉的小部件。 我希望列表視圖的項目在單擊時消失。 我還想在列表的所有元素都結束后刪除整個列表的標題。 是否可以使用 Dismissible 小部件或任何其他此類小部件使 ListTile 消失?

這是我的列表視圖的代碼:

    SliverToBoxAdapter(
        child: Padding(
      padding: const EdgeInsets.only(left: 12.0),
      child: Container(
        child: Text(
          'New User Tasks',
          style: TextStyle(
            fontSize: 26.0,
            fontWeight: FontWeight.w600,
            fontFamily: "Netflix",
            color: Colors.orange,
          ),
        ),
      ),
    )),

    SliverFixedExtentList(
      itemExtent: 80.0,
      delegate: SliverChildBuilderDelegate(
        (BuildContext context, int index) {
          Divider(
            color: Colors.orange,
            height: 7.0,
          );
          ChatModel _model = ChatModel.dummyData[index];
          return Container(
            alignment: Alignment.center,
            color: Colors.transparent,
            child: GestureDetector(
              onTap: () {
                if (index == 0) {
                  print('Hello');
                }
                if (index == 1) {
                  print('Helyyylo');

                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => SettingsView(),
                    ),
                  );
                }
                if (index == 2) {
                  print('Heooooo');
                }
              },
              child: Column(
                children: <Widget>[
                  Divider(
                    height: 4.0,
                  ),
                  ListTile(
                    leading: ClipRRect(
                      borderRadius: BorderRadius.circular(12.0),
                      child: Container(
                        // height:80,
                        // width:30,

                        child: Image.asset(_model.imagePath),
                      ),
                    ),
                    title: Row(
                      children: <Widget>[
                        Text(
                          _model.name,
                          style: TextStyle(
                            fontSize: 17.0,
                            fontWeight: FontWeight.w600,
                            fontFamily: "Netflix",
                            color: Colors.orange,
                          ),
                        ),
                        SizedBox(
                          width: 16.0,
                        ),
                      ],
                    ),
                    subtitle: Text(
                      _model.message,
                      style: TextStyle(
                        fontSize: 15.0,
                        fontFamily: "Netflix",
                      ),
                    ),
                  ),
                ],
              ),
            ),
          );
        },
        childCount: 3,
      ),
    ),

在哪里:

class ChatModel {
  final String imagePath;
  final String name;
  final String datetime;
  final String message;

  ChatModel({this.imagePath, this.name, this.datetime, this.message});

  static final List<ChatModel> dummyData = [
    ChatModel(
      imagePath: 'assets/app/star1.jpg',
      name: "Rate Us",
      datetime: "20:18",
      message: "I love the app",
    ),
    ChatModel(
      imagePath: 'assets/app/test.jpg',
      name: "Invite Code",
      datetime: "19:22",
      message: "I love that idea, it's great!",
    ),
    ChatModel(
      imagePath: 'assets/app/share1.png',
      name: "First Poll Reward",
      datetime: "14:34",
      message: "I wasn't aware of that. Let me check",
    ),
}

刪除點擊的項目。

onTap: () {
 setState((){
   ChatModel.dummyData.removeAt(index);
 });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM