flutter 如何在listview.builder中选择多个文件后复制文件列表

ukqbszuj  于 2022-11-25  发布在  Flutter
关注(0)|答案(1)|浏览(144)

我在列表视图中选择了多个文件,现在我想复制它们。然后想创建输入字段以在那里制作文件夹和zip文件。列表视图生成器

ListView.builder(
      itemCount: files?.length ?? 0,
      itemBuilder: (context, index) {
        return InkWell(
          onTap: () {},
          child: Container(
              child: MultiSelectItem(
                isSelecting: myMultiSelectController.isSelecting,
                onSelected: () {
                  setState(() {
                    myMultiSelectController.toggle(index);
                  });
                },
                child: Card(
                  color: myMultiSelectController.isSelected(index)
                      ? Colors.blueAccent
                      : Colors.white,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(7)),
                  ),
                  child: Center(
                    child: Text(files[index].path,
                        ),

                  ),
                ),

              )

          ),
        );
      },
    ),

复制文件的函数

void copy() {

setState(() {
  myMultiSelectController.set(files?.length ?? 0);
});

}

jvlzgdj9

jvlzgdj91#

解决方案^^:在for循环中使用controller.isSelected获取选定的文件

for(int i=0;i<multiSelectList.length;i++)
                if(controller.isSelected(i)){
                  files.add(multiSelectList[i].path);
                }

范例

class _MultiSelectListDemoState extends State<MultiSelectListDemo> {

  List<FileModel> multiSelectList = [];

  MultiSelectController controller = new MultiSelectController();

  @override
  void initState() {
    super.initState();

    multiSelectList.add(FileModel(path: 'assets/1.jpeg', name:"Welcome to New York City!"));
    multiSelectList.add(FileModel(path: 'assets/2.jpeg', name:"Welcome to San francisco!"));
    multiSelectList.add(FileModel(path: 'assets/3.jpeg', name:"Welcome to Chicago!"));
    multiSelectList.add(FileModel(path: 'assets/4.jpeg', name:"Welcome to Houston!"));

    controller.disableEditingWhenNoneSelected = true;
    controller.set(multiSelectList.length);
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        var before = !controller.isSelecting;
        setState(() {
          controller.deselectAll();
        });
        return before;
      },
      child: new Scaffold(
        appBar: new AppBar(
          title: new Text('Selected ${controller.selectedIndexes.length}' ),
        ),
        body:Column(
          children: [
            Expanded(child: ListView.builder(
              itemCount: multiSelectList.length,
              itemBuilder: (context, index) {
                return InkWell(
                  onTap: () {},
                  child: MultiSelectItem(
                    isSelecting: controller.isSelecting,
                    onSelected: () {
                      setState(() {
                        controller.toggle(index);
                      });
                    },
                    child:Container(
                      height:75,
                      margin: EdgeInsets.only(left:15,right:15,top:15),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(12),
                        color: Colors.transparent,
                      ),
                      child:Card(
                        color:controller.isSelected(index)
                            ? Colors.grey.shade200:Colors.white,
                        shape: RoundedRectangleBorder(

                          borderRadius: BorderRadius.all(Radius.circular(8.0)),

                        ),
                        child:Padding(
                          padding:EdgeInsets.symmetric(vertical:10, horizontal: 12),
                          child: Row(
                            children: [
                              //contentPadding: EdgeInsets.symmetric(vertical: 12, horizontal: 16),
                              ClipRRect(
                                borderRadius: BorderRadius.circular(12),
                                child:Image.asset(multiSelectList[index].path,fit:BoxFit.cover,width:60,height:60,),
                              ),
                              SizedBox(width:20,),
                              Text(multiSelectList[index].name, style: TextStyle(fontSize:14)),
                            ],
                          ),
                        ),
                      ),
                    ),
                  ),
                );
              },
            )),
            ElevatedButton(onPressed: (){
              List<String> files = List.empty(growable: true);

              for(int i=0;i<multiSelectList.length;i++)
                if(controller.isSelected(i)){
                  files.add(multiSelectList[i].path);
                }

              print("selected files $files");

            }, child: Text("zip"))
          ],
        ),
      ),
    );
  }
}


class FileModel{
  final String path;
  final String name;

  FileModel({required this.path, required this.name});

  @override
  String toString() {
    return 'FileModel{name: $name}';
  }
}

相关问题