Learn how to build Gallery Apps in flutter using various widgets.
Example 1: Simple Gallery App
Create your first gallery app with local images as the image source. No library is used.
Step 1: Create Project
Start by creating an empty Flutter Project in your favorite editor or IDE.
Step 2: Add Dependencies
No external packages are needed for this project.
Step 3: Add Images
Add some local images in the assets folder. These images will be rendered in the gallery. If you don't have the assets folder create it in the root folder.
You then need to register these images. Open your pubspec.yaml
and register them as follows:
assets:
- assets/wallpaper-1.jpeg
- assets/wallpaper-2.jpeg
- assets/wallpaper-3.jpeg
Step 4: Write Code
Create or go to main.dart
and add material
package import:
import 'package:flutter/material.dart';
Define the entry point of our app:
void main() => runApp(MyApp());
Build a Stateless MyApp
widget:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Gallery Demo',
theme: ThemeData(primarySwatch: Colors.lightGreen),
home: DisplayPage(),
);
}
}
Create the Display page:
class DisplayPage extends StatelessWidget {
Reference the list of images:
final List<String> images = [
"assets/wallpaper-1.jpeg",
"assets/wallpaper-2.jpeg",
"assets/wallpaper-3.jpeg",
];
And finally build the gallery widget:
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SizedBox.fromSize(
size: Size.fromHeight(550.0),
child: PageView.builder(
controller: PageController(viewportFraction: 0.8),
itemCount: images.length,
itemBuilder: (BuildContext context, int index) {
return new Padding(
padding: EdgeInsets.symmetric(
vertical: 16.0,
horizontal: 8.0,
),
child: Material(
elevation: 5.0,
borderRadius: BorderRadius.circular(8.0),
child: Stack(
fit: StackFit.expand,
children: [
Image.asset(
images[index],
fit: BoxFit.cover,
),
DecoratedBox(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: FractionalOffset.bottomCenter,
end: FractionalOffset.topCenter,
colors: [
Color(0x00000000).withOpacity(0.9),
Color(0xff000000).withOpacity(0.01),
],
),
),
)
],
),
),
);
},
),
)),
);
}
}
Here is the full code:
main.dart
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Gallery Demo',
theme: ThemeData(primarySwatch: Colors.lightGreen),
home: DisplayPage(),
);
}
}
class DisplayPage extends StatelessWidget {
final List<String> images = [
"assets/wallpaper-1.jpeg",
"assets/wallpaper-2.jpeg",
"assets/wallpaper-3.jpeg",
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SizedBox.fromSize(
size: Size.fromHeight(550.0),
child: PageView.builder(
controller: PageController(viewportFraction: 0.8),
itemCount: images.length,
itemBuilder: (BuildContext context, int index) {
return new Padding(
padding: EdgeInsets.symmetric(
vertical: 16.0,
horizontal: 8.0,
),
child: Material(
elevation: 5.0,
borderRadius: BorderRadius.circular(8.0),
child: Stack(
fit: StackFit.expand,
children: [
Image.asset(
images[index],
fit: BoxFit.cover,
),
DecoratedBox(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: FractionalOffset.bottomCenter,
end: FractionalOffset.topCenter,
colors: [
Color(0x00000000).withOpacity(0.9),
Color(0xff000000).withOpacity(0.01),
],
),
),
)
],
),
),
);
},
),
)),
);
}
}
Run
Copy the code into your project or download the code in the reference links below.
Reference
Find the reference links below:
Number | Link |
---|---|
1. | Download code |
2. | Follow code author |