text stringlengths 1 372 |
|---|
<code_end> |
<topic_end> |
<topic_start> |
fade in images with a placeholder |
when displaying images using the default image widget, |
you might notice they simply pop onto the screen as they’re loaded. |
this might feel visually jarring to your users. |
instead, wouldn’t it be nice to display a placeholder at first, |
and images would fade in as they’re loaded? use the |
FadeInImage widget for exactly this purpose. |
FadeInImage works with images of any type: in-memory, local assets, |
or images from the internet. |
<topic_end> |
<topic_start> |
In-Memory |
in this example, use the transparent_image |
package for a simple transparent placeholder. |
<code_start> |
FadeInImage.memoryNetwork( |
placeholder: kTransparentImage, |
image: 'https://picsum.photos/250?image=9', |
), |
<code_end> |
<topic_end> |
<topic_start> |
complete example |
<code_start> |
import 'package:flutter/material.dart'; |
import 'package:transparent_image/transparent_image.dart'; |
void main() { |
runApp(const MyApp()); |
} |
class MyApp extends StatelessWidget { |
const MyApp({super.key}); |
@override |
widget build(BuildContext context) { |
const title = 'fade in images'; |
return MaterialApp( |
title: title, |
home: scaffold( |
appBar: AppBar( |
title: const text(title), |
), |
body: stack( |
children: <widget>[ |
const center(child: CircularProgressIndicator()), |
center( |
child: FadeInImage.memoryNetwork( |
placeholder: kTransparentImage, |
image: 'https://picsum.photos/250?image=9', |
), |
), |
], |
), |
), |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
from asset bundle |
you can also consider using local assets for placeholders. |
first, add the asset to the project’s pubspec.yaml file |
(for more details, see adding assets and images): |
then, use the FadeInImage.assetNetwork() constructor: |
<code_start> |
FadeInImage.assetNetwork( |
placeholder: 'assets/loading.gif', |
image: 'https://picsum.photos/250?image=9', |
), |
<code_end> |
<topic_end> |
<topic_start> |
complete example |
<code_start> |
import 'package:flutter/material.dart'; |
void main() { |
runApp(const MyApp()); |
} |
class MyApp extends StatelessWidget { |
const MyApp({super.key}); |
@override |
widget build(BuildContext context) { |
const title = 'fade in images'; |
return MaterialApp( |
title: title, |
home: scaffold( |
appBar: AppBar( |
title: const text(title), |
), |
body: center( |
child: FadeInImage.assetNetwork( |
placeholder: 'assets/loading.gif', |
image: 'https://picsum.photos/250?image=9', |
), |
), |
), |
); |
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.