i selected multiple files inside listview now i want to copy them. And then want to create input filed to make folder and zip files there. List view builder
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,
),
),
),
)
),
);
},
),
function to copy files
void copy() {
setState(() {
myMultiSelectController.set(files?.length ?? 0);
});
}
CodePudding user response:
Solution ^^: get the selected files by using controller.isSelected inside the for loop
for(int i=0;i<multiSelectList.length;i )
if(controller.isSelected(i)){
files.add(multiSelectList[i].path);
}
Example
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}';
}
}