I am developing a local package for my flutter project, it's basically a UI library which includes a lot of image assets.
I don't want to load the assets in the package from my main project.
I want to load the assets in the package itself.
Here's what I did,
asset location: package_name/lib/assets
pubspec.yaml: packages/package_name/assets/bg.png
dart file: packages/package_name/assets/bg.png
CodePudding user response:
Here are the step by step instructions to add image in Flutter:
Step 1: At the root of your project, create a new folder called assets.
Step 2: Inside the root folder, create another folder called images. You can give any name to this folder such as pictures, graphics, etc.
Step 3: Add your images inside the assets/images folder.
Step 4: Provide the image path in pubspec.yaml file as below.
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
assets: <-- SEE HERE
- assets/images/lion.png
Step 5: Display image using the Image.asset() widget
CodePudding user response:
u can use tihs idea
var files = Directory("${Directory.current.path.toString()}/lib/assets").listSync(); // this to create list of all file in the path
@override
void initState() {
// TODO: implement initState
print(files.toList()[0].path);// to see the alemint that u have and u //can creat list of all item and reach to them like this above
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
child: files.isNotEmpty ? Image.file(File('${files.toList() [0].path}'),fit: BoxFit.fill,) : Text(""),
),
),
);
}
do not forget add
import 'dart:io';