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



我在listview中选择了多个文件,现在我想复制它们。然后要创建输入文件,在那里创建文件夹和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);
});

}

解决方案^^:使用控制器获取选中的文件。isSelected在for循环内

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}';
}
}

最新更新