Skip to content

Commit

Permalink
Merge pull request #21 from hoangph271/20-changing-any-state-causes-t…
Browse files Browse the repository at this point in the history
…he-app-to-switch-back-to-index-page

20 changing any state causes the app to switch back to index page
  • Loading branch information
hoangph271 authored Jan 10, 2024
2 parents b193573 + 1e4359d commit cc8cadc
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 68 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ flutter run

## TODO List:

- [x] User toolbox
- [ ] no more results banner
- [ ] [The search box not visible on the web after splash screen disappeared](https://github.com/hoangph271/sane_gallery/issues/9)
- [x] User toolbox
- [x] macos support
- [x] splash screen
- [x] [Failed to load favorites](https://github.com/hoangph271/sane_gallery/issues/5)
Expand Down
2 changes: 2 additions & 0 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = KW473QP9X5;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
Expand Down Expand Up @@ -669,6 +670,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = KW473QP9X5;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
Expand Down
16 changes: 12 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'src/gifs/gifs_controller.dart';
import 'src/gifs/gifs_service.dart';
import 'src/settings/settings_controller.dart';
Expand All @@ -13,7 +14,7 @@ void main() async {
runApp(app);
}

Future<SaneGallery> initApp() async {
Future<Widget> initApp() async {
final settingsController = SettingsController(SettingsService());
final gifsController = GifsController(GifsService());

Expand All @@ -22,8 +23,15 @@ Future<SaneGallery> initApp() async {
gifsController.init(),
]);

return SaneGallery(
settingsController: settingsController,
gifsController: gifsController,
return MultiProvider(
providers: [
ChangeNotifierProvider<SettingsController>(
create: (context) => settingsController,
),
ChangeNotifierProvider<GifsController>(
create: (context) => gifsController,
),
],
child: SaneGalleryApp(),
);
}
22 changes: 7 additions & 15 deletions lib/src/app.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';

import 'package:sane_gallery/src/gifs/gifs_controller.dart';
import 'package:sane_gallery/src/router.dart';
import 'package:sane_gallery/src/settings/settings_controller.dart';

class SaneGallery extends StatelessWidget {
final SettingsController settingsController;
final GifsController gifsController;

const SaneGallery({
super.key,
required this.settingsController,
required this.gifsController,
});
class SaneGalleryApp extends StatelessWidget {
const SaneGalleryApp({super.key});

@override
Widget build(BuildContext context) {
final settingsController = Provider.of<SettingsController>(context);

return ListenableBuilder(
listenable: Listenable.merge([settingsController, gifsController]),
listenable: settingsController,
builder: (BuildContext context, Widget? _) {
return MaterialApp.router(
restorationScopeId: 'app',
Expand All @@ -37,10 +32,7 @@ class SaneGallery extends StatelessWidget {
theme: ThemeData(),
darkTheme: ThemeData.dark(),
themeMode: settingsController.themeMode,
routerConfig: routerConfig(
settingsController,
gifsController,
),
routerConfig: saneRouter,
);
},
);
Expand Down
51 changes: 27 additions & 24 deletions lib/src/gifs/gifs_view.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:provider/provider.dart';
import 'package:sane_gallery/src/gifs/gifs_controller.dart';
import 'package:sane_gallery/src/gifs/gifs_favorites/favorited_gifs_view.dart';
import 'package:sane_gallery/src/gifs/gif_model.dart';
Expand All @@ -13,13 +14,7 @@ import 'package:http/http.dart' as http;
class GifsView extends StatefulWidget {
static const pathName = '/';

final SettingsController settingsController;
final GifsController gifsController;

const GifsView(
{super.key,
required this.settingsController,
required this.gifsController});
const GifsView({super.key});

@override
State<GifsView> createState() => _GifsViewState();
Expand Down Expand Up @@ -64,7 +59,7 @@ class _GifsViewState extends State<GifsView> {

Future<GifFetchResult> _fetchGifs(String keyword, int offset) async {
final SettingsController(:apiRoot, :apiKey, :pageSize) =
widget.settingsController;
Provider.of<SettingsController>(context, listen: false);

final url = Uri.parse(
'$apiRoot/gifs/search?api_key=$apiKey&q=$keyword&limit=$pageSize&offset=$offset&rating=g&lang=en');
Expand All @@ -86,7 +81,10 @@ class _GifsViewState extends State<GifsView> {

@override
Widget build(BuildContext context) {
final favoritesCount = widget.gifsController.favoriteIds.length;
final gifsController = Provider.of<GifsController>(context);
final settingsController = Provider.of<SettingsController>(context);

final favoritesCount = gifsController.favoriteIds.length;

return SafeArea(
child: DefaultTabController(
Expand All @@ -95,21 +93,26 @@ class _GifsViewState extends State<GifsView> {
appBar: AppBar(
title: const SaneTitle(),
),
body: TabBarView(children: [
GifsSearchView(
pagingController: _pagingController,
searchController: _searchController,
onSearch: _handleSearch,
settingsController: widget.settingsController,
gifsController: widget.gifsController,
totalItemsCount: _totalItemsCount,
),
FavoritedGifsView(
settingsController: widget.settingsController,
gifsController: widget.gifsController,
),
SettingsView(settingsController: widget.settingsController),
]),
body: ListenableBuilder(
listenable: gifsController,
builder: (context, child) {
return TabBarView(children: [
GifsSearchView(
pagingController: _pagingController,
searchController: _searchController,
onSearch: _handleSearch,
settingsController: settingsController,
gifsController: gifsController,
totalItemsCount: _totalItemsCount,
),
FavoritedGifsView(
settingsController: settingsController,
gifsController: gifsController,
),
SettingsView(settingsController: settingsController),
]);
},
),
bottomNavigationBar: TabBar(
tabs: [
const Tab(
Expand Down
36 changes: 12 additions & 24 deletions lib/src/router.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
import 'package:go_router/go_router.dart';
import 'package:sane_gallery/src/gifs/gifs_controller.dart';
import 'package:sane_gallery/src/gifs/gifs_view.dart';
import 'package:sane_gallery/src/magic_toolbox/magic_toolbox.dart';
import 'package:sane_gallery/src/settings/settings_controller.dart';

routerConfig(
SettingsController settingsController,
GifsController gifsController,
) {
final router = GoRouter(
routes: [
GoRoute(
path: GifsView.pathName,
builder: (context, state) => GifsView(
settingsController: settingsController,
gifsController: gifsController,
),
),
GoRoute(
path: MagicToolbox.pathName,
builder: (context, state) => const MagicToolbox(),
),
],
);

return router;
}
final saneRouter = GoRouter(
routes: [
GoRoute(
path: GifsView.pathName,
builder: (context, state) => const GifsView(),
),
GoRoute(
path: MagicToolbox.pathName,
builder: (context, state) => const MagicToolbox(),
),
],
);
16 changes: 16 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.10.0"
nested:
dependency: transitive
description:
name: nested
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
url: "https://pub.dev"
source: hosted
version: "1.0.0"
octo_image:
dependency: transitive
description:
Expand Down Expand Up @@ -445,6 +453,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.7.3"
provider:
dependency: "direct main"
description:
name: provider
sha256: "9a96a0a19b594dbc5bf0f1f27d2bc67d5f95957359b461cd9feb44ed6ae75096"
url: "https://pub.dev"
source: hosted
version: "6.1.1"
rxdart:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies:
flip_card: ^0.7.0
flutter_svg: ^2.0.9
go_router: ^13.0.1
provider: ^6.1.1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit cc8cadc

Please sign in to comment.