feat!: change homeEntry to an onComplete callback

Breaking: this changes the startNavigatorUserStory interface without any backwards compatibility
This commit is contained in:
Joey Boerwinkel 2024-04-04 11:10:32 +02:00
parent bf0060273b
commit 5d66928411
3 changed files with 32 additions and 31 deletions

View file

@ -20,8 +20,15 @@ class Home extends StatelessWidget {
const Home({super.key}); const Home({super.key});
@override @override
Widget build(BuildContext context) => Widget build(BuildContext context) => startNavigatorUserStory(
startNavigatorUserStory(config, context); context,
config,
onComplete: (context) async {
await Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => const HomeEntry()),
);
},
);
} }
List<GoRoute> getStartRoutes() => getStartStoryRoutes( List<GoRoute> getStartRoutes() => getStartStoryRoutes(
@ -33,7 +40,6 @@ StartUserStoryConfiguration config = StartUserStoryConfiguration(
splashScreenBuilder: (context, onFinish) => SplashScreen( splashScreenBuilder: (context, onFinish) => SplashScreen(
onFinish: onFinish, onFinish: onFinish,
), ),
homeEntry: const HomeEntry(),
introductionOptionsBuilder: (ctx) => IntroductionOptions( introductionOptionsBuilder: (ctx) => IntroductionOptions(
pages: [ pages: [
IntroductionPage( IntroductionPage(

View file

@ -12,7 +12,6 @@ class StartUserStoryConfiguration {
this.introductionBuilder, this.introductionBuilder,
this.introductionOptionsBuilder, this.introductionOptionsBuilder,
this.introductionService, this.introductionService,
this.homeEntry,
this.homeScreenRoute, this.homeScreenRoute,
this.introductionFallbackScreen, this.introductionFallbackScreen,
this.introductionScrollPhysics, this.introductionScrollPhysics,
@ -41,8 +40,6 @@ class StartUserStoryConfiguration {
/// The route that is used to navigate to the home screen. /// The route that is used to navigate to the home screen.
final String? homeScreenRoute; final String? homeScreenRoute;
final Widget? homeEntry;
final IntroductionOptions Function(BuildContext context)? final IntroductionOptions Function(BuildContext context)?
introductionOptionsBuilder; introductionOptionsBuilder;
final Widget? introductionFallbackScreen; final Widget? introductionFallbackScreen;

View file

@ -4,21 +4,32 @@ import 'package:flutter/material.dart';
import 'package:flutter_start/flutter_start.dart'; import 'package:flutter_start/flutter_start.dart';
import 'package:flutter_start/src/services/killswitch_service.dart'; import 'package:flutter_start/src/services/killswitch_service.dart';
/// Enter the start user story with the Navigator 1.0 API.
///
/// Requires a Navigator widget to exist in the given [context].
///
/// Customization can be done through the [configuration] parameter.
///
/// [onComplete] triggers as soon as the userstory is finished.
///
/// The context provided here is a context has a guaranteed navigator.
Widget startNavigatorUserStory( Widget startNavigatorUserStory(
StartUserStoryConfiguration configuration,
BuildContext context, BuildContext context,
) { StartUserStoryConfiguration configuration, {
required void Function(BuildContext context) onComplete,
}) {
if (configuration.splashScreenBuilder == null && if (configuration.splashScreenBuilder == null &&
configuration.splashScreenCenterWidget == null && configuration.splashScreenCenterWidget == null &&
configuration.splashScreenBackgroundColor == null) { configuration.splashScreenBackgroundColor == null) {
return _introduction(configuration, context); return _introduction(configuration, context, onComplete);
} }
return _splashScreen(configuration, context); return _splashScreen(configuration, context, onComplete);
} }
Widget _splashScreen( Widget _splashScreen(
StartUserStoryConfiguration configuration, StartUserStoryConfiguration configuration,
BuildContext context, BuildContext context,
void Function(BuildContext context) onComplete,
) { ) {
var navigator = Navigator.of(context); var navigator = Navigator.of(context);
@ -59,18 +70,18 @@ Widget _splashScreen(
if ((!configuration.showIntroduction || introductionSeen) && if ((!configuration.showIntroduction || introductionSeen) &&
context.mounted) { context.mounted) {
await navigator.pushReplacement( onComplete(context);
MaterialPageRoute(
builder: (context) => _home(configuration, context),
),
);
return; return;
} }
if (context.mounted) { if (context.mounted) {
await navigator.pushReplacement( await navigator.pushReplacement(
MaterialPageRoute( MaterialPageRoute(
builder: (context) => _introduction(configuration, context), builder: (context) => _introduction(
configuration,
context,
onComplete,
),
), ),
); );
} }
@ -98,15 +109,12 @@ Widget _splashScreen(
Widget _introduction( Widget _introduction(
StartUserStoryConfiguration configuration, StartUserStoryConfiguration configuration,
BuildContext context, BuildContext context,
void Function(BuildContext context) onComplete,
) { ) {
var introduction = Introduction( var introduction = Introduction(
service: configuration.introductionService ?? service: configuration.introductionService ??
IntroductionService(SharedPreferencesIntroductionDataProvider()), IntroductionService(SharedPreferencesIntroductionDataProvider()),
navigateTo: () async => Navigator.of(context).pushReplacement( navigateTo: () async => onComplete(context),
MaterialPageRoute(
builder: (context) => _home(configuration, context),
),
),
options: configuration.introductionOptionsBuilder?.call(context) ?? options: configuration.introductionOptionsBuilder?.call(context) ??
const IntroductionOptions(), const IntroductionOptions(),
physics: configuration.introductionScrollPhysics, physics: configuration.introductionScrollPhysics,
@ -119,13 +127,3 @@ Widget _introduction(
), ),
); );
} }
Widget _home(
StartUserStoryConfiguration configuration,
BuildContext context,
) {
var home = configuration.homeEntry;
return Scaffold(
body: home,
);
}