I've been learning flutter for 2 months. I'm trying to develop a wallpaper app. I created a model and a function. But right now I can only download 1 wallpaper. How can I make this a list? I get this error when I make a list.
This is url.
String url =
'https://images.hdqwalls.com/download/the-witcher-season-2-2022-5k-u1-1080x1920.jpg';
the list i want to use
List<String> url = [
'https://images.hdqwalls.com/download/the-witcher-season-2-2022-5k-u1-1080x1920.jpg',
'https://images.hdqwalls.com/download/the-witcher-season-2-2022-5k-u1-1080x1920.jpg',
];
and function
void saveimage() async {
await GallerySaver.saveImage(url, albumName: album_name);
}
and clicking this button provides download
ElevatedButton DownloadButton(BuildContext context) {
return ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: Size(40, 40),
shape: CircleBorder(),
backgroundColor: Colors.grey.shade600.withOpacity(0.1),
),
child: Icon(Icons.download, color: Colors.white.withOpacity(0.7)),
onPressed: () {
saveimage();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
duration: Duration(seconds: 2),
content: Text('Wallpaper downloaded!'),
action: SnackBarAction(
label: '',
onPressed: () {},
),
),
);
},
);
}
The packages I use are
- gallery_saver: ^2.3.2
- async_wallpaper: ^2.0.1
I want to use it in gridview
GridView.builder(
itemCount: url.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 9 / 16,
),
itemBuilder: (BuildContext context, int index) {
return Card(
child: Padding(
padding: EdgeInsets.all(1.0),
child: FullScreenWidget(
child: Stack(fit: StackFit.expand, children: [
Image.network(url, fit: BoxFit.cover),
CodePudding user response:
void saveimage() async {
for(int i = 0; i <= url.length; i ) {
await GallerySaver.saveImage(url[I], albumName: album_name);
}
}
Above code will resolve your error
CodePudding user response:
Let consider you want to download image when you click on the card then code will be like as following.
void saveimage(String imageUrl) async {
await GallerySaver.saveImage(imageUrl, albumName: album_name);
}
GridView.builder(
itemCount: url.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 9 / 16,
),
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
onTap: (){
await saveimage(url[index]);
},
child: Card(
child: Padding(
padding: EdgeInsets.all(1.0),
child: FullScreenWidget(
child: Stack(fit: StackFit.expand, children: [
Image.network(url, fit: BoxFit.cover),]),),);),
),)