Skip to content

Commit

Permalink
Add Shimmer as Placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
up2code committed Mar 24, 2024
1 parent 1a72abf commit 6e613a3
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 12 deletions.
10 changes: 8 additions & 2 deletions lib/src/common_widgets/async_value_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:vocadb_app/src/common_widgets/error_message_widget.dart';

class AsyncValueWidget<T> extends StatelessWidget {
const AsyncValueWidget({super.key, required this.value, required this.data});
const AsyncValueWidget({
super.key,
required this.value,
required this.data,
this.loadingWidget = const Center(child: CircularProgressIndicator()),
});
final AsyncValue<T> value;
final Widget Function(T) data;
final Widget loadingWidget;

@override
Widget build(BuildContext context) {
Expand All @@ -15,7 +21,7 @@ class AsyncValueWidget<T> extends StatelessWidget {
return Center(child: ErrorMessageWidget(e.toString()));
},
loading: () {
return const Center(child: CircularProgressIndicator());
return loadingWidget;
},
);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/common_widgets/custom_network_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CustomNetworkImage extends StatelessWidget {
final String imageUrl;

/// A placeholder widget to display when image loading.
final Widget placeholder;
// final Widget placeholder;

/// A widget to display error when fail to load image.
final Widget errorWidget;
Expand All @@ -21,7 +21,7 @@ class CustomNetworkImage extends StatelessWidget {
const CustomNetworkImage(
this.imageUrl, {
super.key,
this.placeholder = const Center(child: CircularProgressIndicator()),
// this.placeholder,
this.errorWidget = const Icon(Icons.error),
this.fit = BoxFit.cover,
this.width = 140,
Expand All @@ -35,7 +35,7 @@ class CustomNetworkImage extends StatelessWidget {
imageUrl: imageUrl,
width: width,
height: height,
placeholder: (context, url) => placeholder,
placeholder: (context, url) => Container(color: Colors.grey),
errorWidget: (context, url, error) => errorWidget,
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
import 'package:flutter/material.dart';
import 'package:shimmer/shimmer.dart';

/// A widget display placeholder for album
class AlbumCardPlaceholder extends StatelessWidget {
const AlbumCardPlaceholder({super.key});

@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.symmetric(horizontal: 8.0),
child: const Text('Placeholder'),
margin: EdgeInsets.symmetric(horizontal: 8.0),
child: Shimmer.fromColors(
baseColor: Colors.grey.shade300,
highlightColor: Colors.grey.shade100,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: 130,
height: 120,
color: Colors.white,
),
const SizedBox(
height: 6,
),
Container(
width: 130,
height: 8,
color: Colors.white,
),
const SizedBox(
height: 6,
),
Container(
width: 80,
height: 8,
color: Colors.white,
),
],
),
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:vocadb_app/src/common_widgets/async_value_widget.dart';
import 'package:vocadb_app/src/features/songs/data/song_repository.dart';
import 'package:vocadb_app/src/features/songs/domain/song.dart';
import 'package:vocadb_app/src/features/songs/presentation/songs_list/song_placeholder_list_view.dart';
import 'package:vocadb_app/src/features/songs/presentation/songs_list/songs_list_view.dart';

class TopSongsDailyListView extends ConsumerWidget {
Expand All @@ -13,8 +14,10 @@ class TopSongsDailyListView extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final value = ref.watch(songsDailyProvider);
// return SongPlaceholderListView(scrollDirection: Axis.vertical,);
return AsyncValueWidget(
value: value,
loadingWidget: const SongPlaceholderListView(scrollDirection: Axis.vertical,),
data: (data) => SongListView(
songs: data,
displayOrderNumber: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:vocadb_app/src/common_widgets/async_value_widget.dart';
import 'package:vocadb_app/src/features/songs/data/song_repository.dart';
import 'package:vocadb_app/src/features/songs/domain/song.dart';
import 'package:vocadb_app/src/features/songs/presentation/songs_list/song_placeholder_list_view.dart';
import 'package:vocadb_app/src/features/songs/presentation/songs_list/songs_list_view.dart';

class TopSongsMonthlyListView extends ConsumerWidget {
Expand All @@ -14,6 +15,7 @@ class TopSongsMonthlyListView extends ConsumerWidget {
final value = ref.watch(songsMonthlyProvider);
return AsyncValueWidget(
value: value,
loadingWidget: const SongPlaceholderListView(scrollDirection: Axis.vertical,),
data: (data) => SongListView(
songs: data,
displayOrderNumber: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:vocadb_app/src/common_widgets/async_value_widget.dart';
import 'package:vocadb_app/src/features/songs/data/song_repository.dart';
import 'package:vocadb_app/src/features/songs/domain/song.dart';
import 'package:vocadb_app/src/features/songs/presentation/songs_list/song_placeholder_list_view.dart';
import 'package:vocadb_app/src/features/songs/presentation/songs_list/songs_list_view.dart';

class TopSongsOverallListView extends ConsumerWidget {
Expand All @@ -15,6 +16,7 @@ class TopSongsOverallListView extends ConsumerWidget {
final value = ref.watch(songsOverallProvider);
return AsyncValueWidget(
value: value,
loadingWidget: const SongPlaceholderListView(scrollDirection: Axis.vertical,),
data: (data) => SongListView(
songs: data,
displayOrderNumber: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:vocadb_app/src/common_widgets/async_value_widget.dart';
import 'package:vocadb_app/src/features/songs/data/song_repository.dart';
import 'package:vocadb_app/src/features/songs/domain/song.dart';
import 'package:vocadb_app/src/features/songs/presentation/songs_list/song_placeholder_list_view.dart';
import 'package:vocadb_app/src/features/songs/presentation/songs_list/songs_list_view.dart';

class TopSongsWeeklyListView extends ConsumerWidget {
Expand All @@ -15,6 +16,7 @@ class TopSongsWeeklyListView extends ConsumerWidget {
final value = ref.watch(songsWeeklyProvider);
return AsyncValueWidget(
value: value,
loadingWidget: const SongPlaceholderListView(scrollDirection: Axis.vertical,),
data: (data) => SongListView(
songs: data,
displayOrderNumber: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
import 'package:flutter/material.dart';
import 'package:shimmer/shimmer.dart';

/// A widget display placeholder for song
class SongCardPlaceholder extends StatelessWidget {
const SongCardPlaceholder({super.key});

@override
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.symmetric(horizontal: 8.0),
child: const Center(
child: CircularProgressIndicator(),
margin: EdgeInsets.symmetric(horizontal: 8.0),
child: Shimmer.fromColors(
baseColor: Colors.grey.shade300,
highlightColor: Colors.grey.shade100,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: 130,
height: 80,
color: Colors.white,
),
const SizedBox(
height: 6,
),
Container(
width: 130,
height: 8,
color: Colors.white,
),
const SizedBox(
height: 6,
),
Container(
width: 80,
height: 8,
color: Colors.white,
),
],
),
),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:flutter/material.dart';
import 'package:shimmer/shimmer.dart';

class SongTilePlaceholder extends StatelessWidget {
const SongTilePlaceholder({super.key});

@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.symmetric(horizontal: 8.0),
child: Shimmer.fromColors(
baseColor: Colors.grey.shade300,
highlightColor: Colors.grey.shade100,
child: SizedBox(
height: 100,
child: Row(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 120,
height: 80,
color: Colors.white,
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Padding(
padding: const EdgeInsets.fromLTRB(0.0, 2.0, 0.0, 2.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
width: 100,
height: 16,
color: Colors.white,
),
const SizedBox(
height: 8,
),
Container(
width: 200,
height: 16,
color: Colors.white,
),
const SizedBox(
height: 8,
),
Container(
width: 40,
height: 16,
color: Colors.white,
),
],
),
),
),)
],
),
),
),
);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:vocadb_app/src/features/songs/presentation/song_card/song_card_placeholder.dart';
import 'package:vocadb_app/src/features/songs/presentation/song_tile/song_tile_placeholder.dart';

class SongPlaceholderListView extends StatelessWidget {
const SongPlaceholderListView(
Expand All @@ -13,6 +14,15 @@ class SongPlaceholderListView extends StatelessWidget {

@override
Widget build(BuildContext context) {

if(scrollDirection == Axis.vertical) {
return ListView.builder(
itemCount: size,
scrollDirection: Axis.vertical,
itemBuilder: (context, index) => const SongTilePlaceholder(),
);
}

return SizedBox(
height: 180,
child: ListView.builder(
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.3"
shimmer:
dependency: "direct main"
description:
name: shimmer
sha256: "5f88c883a22e9f9f299e5ba0e4f7e6054857224976a5d9f839d4ebdc94a14ac9"
url: "https://pub.dev"
source: hosted
version: "3.0.0"
sky_engine:
dependency: transitive
description: flutter
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dependencies:
hive_flutter: 1.1.0
youtube_player_flutter: 8.1.2
fluttertoast: 8.1.1
shimmer: ^3.0.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 6e613a3

Please sign in to comment.