Skip to content

Commit

Permalink
feat: add pull to refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
winminhtetz committed Feb 27, 2024
1 parent 8a099e1 commit 549e539
Show file tree
Hide file tree
Showing 22 changed files with 286 additions and 137 deletions.
5 changes: 4 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle.dark,
SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
statusBarIconBrightness: Brightness.dark,
),
);
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
Expand Down
2 changes: 0 additions & 2 deletions lib/services/ios _icons/providers/ios_icon_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ List<IosIcon> iosIcon(IosIconRef ref, String q) {
final iosIcons = ref.watch(iosIconServiceProvider);

iosIcons.whenData((value) => icons = value);

if (query.isEmpty) {
return icons;
}

icons = icons.where((e) => e.iconName.contains(query)).toList();
return icons;
}
30 changes: 15 additions & 15 deletions lib/services/ios _icons/providers/ios_icon_provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions lib/services/ios _icons/services/ios_icon_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import 'package:http/http.dart' as http;

part 'ios_icon_service.g.dart';

@Riverpod(keepAlive: true)

@riverpod
class IosIconService extends _$IosIconService {
@override
Future<List<IosIcon>> build() async {
var r = await http.get(Uri.parse(Env.api));
Future<List<IosIcon>> build() async {
var r = await http.get(Uri.parse(Env.api));
return iosIconFromJson(r.body);
}
}
}
6 changes: 3 additions & 3 deletions lib/services/ios _icons/services/ios_icon_service.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/src/global/theme/app_color.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ import 'package:flutter/material.dart';
const Color coconut = Color(0xffFDFDFD);
const Color silver = Color(0xffC0C0C0);
const Color bombay = Color(0xffB1B5BB);
const Color porcelain = Color(0xffECEEF0);
const Color mineShaft = Color(0xff383838);

5 changes: 5 additions & 0 deletions lib/src/global/theme/text_style.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:ios_icon_finder/src/global/theme/app_color.dart';

const TextStyle section = TextStyle(
fontSize: 37,
Expand All @@ -11,3 +12,7 @@ const TextStyle header = TextStyle(
const TextStyle body = TextStyle(
fontSize: 14,
);
const TextStyle bodySmall = TextStyle(
fontSize: 10,
color: mineShaft,
);
7 changes: 7 additions & 0 deletions lib/src/global/util/show_snackbar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:flutter/material.dart';

extension ShowSnackBar on BuildContext {
void showSnackBar(String message) {
ScaffoldMessenger.of(this).showSnackBar(SnackBar(content: Text(message)));
}
}
14 changes: 2 additions & 12 deletions lib/src/pages/home/components/body_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,12 @@ import 'package:ios_icon_finder/services/ios%20_icons/models/ios_icon_model.dart
import 'package:ios_icon_finder/src/pages/home/widgets/icon_list.dart';

class BodyComponent extends StatelessWidget {
const BodyComponent({super.key, required this.icons, required this.onRefresh});
const BodyComponent({super.key, required this.icons});

final List<IosIcon> icons;
final Future<void> onRefresh;

@override
Widget build(BuildContext context) {
return Expanded(
child: IconList(
icons: icons,
onRefresh: onRefresh,
));
// return switch (iosIcons) {
// AsyncData(:final value) => Expanded(child: IconList(icons: value)),
// AsyncError(:final error) => Text('$error'),
// _ => CircularProgressIndicator(),
// };
return Expanded(child: IconList(icons: icons));
}
}
9 changes: 7 additions & 2 deletions lib/src/pages/home/components/title_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ import 'package:ios_icon_finder/src/pages/home/widgets/app_title.dart';
import 'package:ios_icon_finder/src/pages/home/widgets/icon_search_bar.dart';

class TitleComponent extends StatelessWidget {
const TitleComponent({super.key, required this.onSearch});
const TitleComponent({
super.key,
required this.onSearch,
required this.onRefresh,
});

final ValueChanged<String> onSearch;
final VoidCallback onRefresh;

@override
Widget build(BuildContext context) {
return SeparatedColumn(
separatorBuilder: () => SizedBox(height: 8),
crossAxisAlignment: CrossAxisAlignment.start,
children: [
AppTitle(),
AppTitle(onRefresh: onRefresh),
IconSearchBar(onChanged: onSearch),
],
);
Expand Down
12 changes: 7 additions & 5 deletions lib/src/pages/home/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ class HomePage extends HookConsumerWidget {
child: Column(
children: [
TitleComponent(
onSearch: (value) => query.value = value,
onSearch: (input) => onSearch(input, query),
onRefresh: () => ref.refresh(iosIconServiceProvider.future),
),
switch (data) {
AsyncData() => BodyComponent(
icons: iosIcons,
onRefresh: ref.refresh(iosIconServiceProvider.future),
),
AsyncData() => BodyComponent(icons: iosIcons),
AsyncError() => ErrorUI(
onPressed: () => ref.refresh(iosIconServiceProvider.future),
),
Expand All @@ -43,4 +41,8 @@ class HomePage extends HookConsumerWidget {
),
);
}

void onSearch(String value, ValueNotifier<String> query) {
query.value = value;
}
}
25 changes: 17 additions & 8 deletions lib/src/pages/home/widgets/app_title.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ios_icon_finder/src/global/theme/app_color.dart';
import 'package:ios_icon_finder/src/global/theme/text_style.dart';

class AppTitle extends StatelessWidget {
const AppTitle({super.key});
const AppTitle({super.key, required this.onRefresh});

final VoidCallback onRefresh;

@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('CupertinoIcons Finder', style: header),
Text(
'Please search the name of icon',
style: body.copyWith(color: silver),
)
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('CupertinoIcons Finder', style: header),
Text(
'Made with ❤️ by BadazzHarry',
style: body.copyWith(color: silver),
)
],
),
IconButton(onPressed: onRefresh, icon: Icon(CupertinoIcons.refresh)),
],
);
}
Expand Down
24 changes: 17 additions & 7 deletions lib/src/pages/home/widgets/error_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@ class ErrorUI extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Expanded(
child: SeparatedColumn(
separatorBuilder: () => SizedBox(height: 8),
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('An Error occured!', style: body.copyWith(color: Colors.red)),
TextButton(onPressed: onPressed, child: Text('Refresh'))
],
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: SeparatedColumn(
separatorBuilder: () => SizedBox(height: 8),
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Unable to connect to the internet',
style: body.copyWith(color: Colors.red)),
Text(
'Can\'t display the icons because your device isn\'t connected to the internet.',
style: bodySmall.copyWith(color: Colors.black)),
Align(
alignment: Alignment.center,
child: TextButton(onPressed: onPressed, child: Text('Refresh')))
],
),
),
);
}
Expand Down
42 changes: 0 additions & 42 deletions lib/src/pages/home/widgets/icon_card.dart

This file was deleted.

Loading

0 comments on commit 549e539

Please sign in to comment.