Flutter get assets by wildcard suffix - flutter

I have a folder of assets called stickers. The folder contains images (.png, .jpeg) and gifs (.gif). I want to show the user all his stickers.
My stickers folder:
My current code supports png files only:
children: List.generate(30, (index) {
return Padding(
padding: const EdgeInsets.all(15.0),
child: Image(
image: AssetImage(
'assets/stickers/sticker_${index + 1}.png',
),
),
);
}),
How do I list assets, based on names, ignoring the file suffix?
Or how to support wildcard assets in flutter?

you can copy the assets names with a mixed sort in a list like this :
const List assetslist = const [
'sticker_1.png',
'sticker_2.png'
'sticker_3.jpeg'
'sticker_4.png'
'sticker_5.gif'
'sticker_6.png'
];
then use it to generate an image for each of text:
children: assetslist.map((e) {
return Padding(
padding: const EdgeInsets.all(15.0),
child: Image(
image: AssetImage(
e,
),
),
);
}).toList(),

You can add assets by wildcard by adding this at the pubspec.yaml.
assets:
- assets/stickers/
But, for wildcard assets, you can't use hot reload if you update those assets inside that folder, you need completely stop the app and start again via android studio or vs code.

Related

how to set background image on flutter

This is my pubspec.yaml
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- images/img_rectangle1.jpg
I have established an assets folder and included it with my.png picture; but, I am experiencing difficulty with the widgets.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Korean vocabulary related to traits"),
centerTitle: false,
backgroundColor: const Color(0xFFEC74C8),
),
body: Row(
children: [
Expanded(
child: ListView.builder(
itemCount: koreanNameList.length,
itemBuilder: (context, index) {
KoreanItem item = koreanNameList[index];
return Draggable<KoreanItem>(
data: item,
dragAnchorStrategy: pointerDragAnchorStrategy,
feedback: KoreanNameCard(item: item),
child: KoreanNameCard(item: item));
},
)),
Expanded(
child: ListView.builder(
itemCount: englishanswers.length,
itemBuilder: (context, index) {
return buildEnglishanswerColumn(englishanswers[index]);
},
)),
],
),
);
}
Where exactly should the container that holds the background picture be placed, then?
I have tried to use
AssetImage
but it is not showing on my app.
You need to specify the assets on pubspec.yaml
Flutter uses the pubspec.yaml file, located at the root of your project, to identify assets required by an app.
Here is an example:
flutter:
assets:
- assets/images/background.png
To include all assets under a directory, specify the directory name with the / character at the end:
flutter:
assets:
- assets/
- assets/images/
sample using image asset
Container(
height: double.infinity,
width: double.infinity,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/splash/background.png"),
fit: BoxFit.cover,
),
),
child: const Center(
child: Text(
'Hello World!',
style: TextStyle(color: Colors.white),
),
),
);
Reference: Adding assets and images
For setting an image as the background, use this Container widget as the body of your Scaffold.
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/image.jpg"),
fit: BoxFit.cover,
),
),
child: child
)
Replace image.jpg with your image name.

How to download local file in Flutter

I'm making my portfolio website on flutter i want that if someone click on download cv button,it download on his device.CV is place in the assets folder.
downloadFile(url) {
AnchorElement anchorElement =
AnchorElement(href: url);
anchorElement.download = "CV";
anchorElement.click();
}
TextButton(
onPressed: () =>
downloadFile("/assets/files/Hassan_Naveed_CV.pdf"),
child: FittedBox(
child: Row(
children: [
Text(
"Download CV",
style: TextStyle(
color: Theme.of(context)
.textTheme
.bodyText1!
.color),
),
const SizedBox(
width: defaultPadding / 2,
),
const Icon(Icons.download, color: Color(0xFF8B8B8D))
],
),
)),
when i click on the download button ,it shows download failed
Try checking the path of your file against where you call it and whether you have given access to that 'files' folder in your pubspec.yml. Your code looks good to me, I think it comes from the given path.
Also check that there are no spaces or unwanted characters in the file name.

How to load 'expand' content only when I click in a Item

I have a flutter app which has a ExpandablePanel as the code shows:
ListView(
children: [
...items.map<Widget>(
(item) {
return ExpandableTheme(
data: ExpandableThemeData(
hasIcon: false
),
child: ExpandablePanel(
header: Card(
// my code
),
expanded: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: ClassWhichReturnsAContainer(item),
),
),
);
},
)
],
)
Running this it shows my items as expected but the problem is with expand section because when the screen is built it loads the ClassWhichReturnsAContainer for all the items (making a list with all the returns together). I don't want this result, I want not run anything inside expanded until I tap in some item, only when I tap in an item it begins to load and show the content inside ClassWhichReturnsAContainer . How can I do this?

Image does not load on Flutter Draw Header

I'm new to flutter-dart and I've been trying to load a image to my draw header, so far unsuccessfully. Can anyone shed some light on it? Please find my code below.
class MainDrawer extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
decoration: BoxDecoration(
//color: Theme.of(context).backgroundColor,
image: DecorationImage(
image: AssetImage('assets/images/title_fb_600.png'),
fit: BoxFit.cover,
)),
child: Text("Some Text"),
//ListTile(
// leading: Icon(Icons.monetization_on),
// title: Text(
// "TRIPLE A",
// style: TextStyle(color: Colors.white, fontSize: 20),
// ),
),
ItemDrawer("Carteira Recomendada", Icons.star, 16),
ItemDrawer("Carteira R\$10", Icons.ten_k, 16),
ItemDrawer("Minha Carteira", Icons.favorite, 16),
ItemDrawer("Notícias", Icons.new_releases, 16),
ItemDrawer("Minha Conta", Icons.account_circle, 16),
ItemDrawer("Configurações", Icons.settings, 16),
],
),
);
}
}
My pubspec.yaml....
> flutter:
>
uses-material-design: true
>
assets:
> - assets/images/title3a_fb_600.png
And folder structure...
folder structure
Your pubspec file looks good. I'm assuming you only want to include this single image as an asset.
Try using this package, and replacing the DrawerHeader class with a ProgressiveImage component here.
https://pub.dev/packages/progressive_image
This package helps you to cache the image, to prevent having to reload it again, and it also adds a blur animation while it loads this asset.
If you need to layer the drawer header (and add other information, such as an avatar icon, or email information), you can add this component into a Stack collection, then make sure that the ProgressiveImage is the first in the Stack
If you also need to set a custom height to the drawer header, then make sure to wrap this Stack with a Container and manually set the height of this element

Flutter NetworkImage takes too long to load

So i'm trying to load close to a thousand gifs on Inkwell widgets with a Hero child in a boxdecoration imagenetwork, i want to understand the best practice to handle this, as it takes long times to load, i want it to load by the first one to appear, whether it's normal or by filtering, so how do flutter devs handle this ?
So how do i support slow internet users by deciding which NetworkImage to load first
InkWell(
onTap: (){
Navigator.push(context, MaterialPageRoute(builder:(context)=>GifDetail(
gifs: gifs,
)));
},
child: Hero(
tag: gifs.url,
child: Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
height: 110,
width: 110,
decoration: BoxDecoration(
image: DecorationImage(image: NetworkImage(gifs.url))
),
),
],
),
),
),
),
You could try using Cached_network_image package. It will still take the same time to load images initially but then the images are retained on the device so will load a lot quicker the next time. There is really no way of getting around the initial download of the image.
Loading via a ListView.builder or GridView.builder will only load the images in the initial view until you scroll, so should also help.