-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move approuter to separate provider. (Fix hotreload problem)
- Loading branch information
1 parent
0637f3c
commit 4d27a3e
Showing
4 changed files
with
114 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import 'package:converterpro/models/order.dart'; | ||
import 'package:converterpro/models/properties_list.dart'; | ||
import 'package:converterpro/models/settings.dart'; | ||
import 'package:converterpro/pages/conversion_page.dart'; | ||
import 'package:converterpro/pages/error_page.dart'; | ||
import 'package:converterpro/pages/reorder_properties_page.dart'; | ||
import 'package:converterpro/pages/reorder_units_page.dart'; | ||
import 'package:converterpro/pages/settings_page.dart'; | ||
import 'package:converterpro/pages/splash_screen.dart'; | ||
import 'package:converterpro/utils/app_scaffold.dart'; | ||
import 'package:converterpro/utils/utils.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
|
||
final isEverythingLoadedProvider = Provider<bool>((ref) => | ||
ref.watch(SignificantFigures.provider).hasValue && | ||
ref.watch(RemoveTrailingZeros.provider).hasValue && | ||
ref.watch(IsDarkAmoled.provider).hasValue && | ||
ref.watch(CurrentThemeMode.provider).hasValue && | ||
ref.watch(CurrentLocale.provider).hasValue && | ||
ref.watch(PropertiesOrderNotifier.provider).hasValue && | ||
ref.watch(UnitsOrderNotifier.provider).hasValue && | ||
ref.watch(propertiesListProvider).hasValue); | ||
|
||
final routerProvider = Provider<GoRouter>( | ||
(ref) => GoRouter( | ||
routes: [ | ||
GoRoute( | ||
path: '/', | ||
builder: (context, _) => const SplashScreen(), | ||
), | ||
ShellRoute( | ||
builder: (context, state, child) { | ||
return AppScaffold( | ||
child: child, | ||
); | ||
}, | ||
routes: [ | ||
GoRoute( | ||
path: '/conversions/:property', | ||
pageBuilder: (context, state) { | ||
final String property = state.pathParameters['property']!; | ||
final int? pageNumber = pageNumberMap[property]; | ||
if (pageNumber == null) { | ||
throw Exception('property not found: $property'); | ||
} else { | ||
return NoTransitionPage(child: ConversionPage(pageNumber)); | ||
} | ||
}, | ||
), | ||
GoRoute( | ||
path: '/settings', | ||
name: 'settings', | ||
pageBuilder: (context, state) => | ||
const NoTransitionPage(child: SettingsPage()), | ||
routes: [ | ||
GoRoute( | ||
path: 'reorder-properties', | ||
name: 'reorder-properties', | ||
pageBuilder: (context, state) => | ||
const NoTransitionPage(child: ReorderPropertiesPage()), | ||
), | ||
GoRoute( | ||
path: 'reorder-units', | ||
name: 'reorder-units', | ||
pageBuilder: (context, state) => | ||
const NoTransitionPage(child: ChoosePropertyPage()), | ||
routes: [ | ||
GoRoute( | ||
path: ':property', | ||
pageBuilder: (context, state) { | ||
final String property = state.pathParameters['property']!; | ||
final int? pageNumber = pageNumberMap[property]; | ||
if (pageNumber == null) { | ||
throw Exception('property not found: $property'); | ||
} else { | ||
return NoTransitionPage( | ||
child: ChoosePropertyPage( | ||
selectedProperty: pageNumber, | ||
isPropertySelected: true, | ||
), | ||
); | ||
} | ||
}, | ||
), | ||
], | ||
), | ||
], | ||
), | ||
], | ||
), | ||
], | ||
redirect: (context, state) { | ||
// Bypass splashscreen if variables are already loaded | ||
if (state.uri.toString() == '/') { | ||
if (ref.read(isEverythingLoadedProvider)) { | ||
final List<int> conversionsOrderDrawer = | ||
ref.read(PropertiesOrderNotifier.provider).value!; | ||
return '/conversions/${reversePageNumberListMap[conversionsOrderDrawer.indexWhere((val) => val == 0)]}'; | ||
} | ||
} | ||
return null; | ||
}, | ||
errorBuilder: (context, state) => const ErrorPage(), | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters