mirror of
https://github.com/Iconica-Development/flutter_introduction.git
synced 2025-05-19 03:53:45 +02:00
feat: make the transition when navigating to the introduction screen more fluent
This commit is contained in:
parent
25b3988aea
commit
d3d76de017
7 changed files with 68 additions and 38 deletions
|
@ -7,12 +7,18 @@ import "package:introduction_repository_interface/introduction_repository_interf
|
|||
|
||||
class FirebaseIntroductionRepository
|
||||
implements IntroductionRepositoryInterface {
|
||||
bool? _shouldShowIntroduction;
|
||||
List<IntroductionPageData>? _introductionPages;
|
||||
|
||||
final _introductionCollection = FirebaseFirestore.instance
|
||||
.collection("flutter_introduction")
|
||||
.doc("flutter_introduction");
|
||||
|
||||
@override
|
||||
Future<List<IntroductionPageData>> fetchIntroductionPages() async {
|
||||
if (_introductionPages != null) {
|
||||
return _introductionPages!;
|
||||
}
|
||||
try {
|
||||
var introductionPagesData = await _introductionCollection
|
||||
.collection("pages")
|
||||
|
@ -27,6 +33,7 @@ class FirebaseIntroductionRepository
|
|||
introductionPagesData.docs.map((e) => e.data()).toList();
|
||||
|
||||
introductionPages.sort((a, b) => a.id.compareTo(b.id));
|
||||
_introductionPages = introductionPages;
|
||||
return introductionPages;
|
||||
} on Exception catch (_) {
|
||||
throw Exception();
|
||||
|
@ -51,6 +58,9 @@ class FirebaseIntroductionRepository
|
|||
|
||||
@override
|
||||
Future<bool> shouldShow() async {
|
||||
if (_shouldShowIntroduction != null) {
|
||||
return _shouldShowIntroduction!;
|
||||
}
|
||||
try {
|
||||
await FirebaseAuth.instance.signInAnonymously();
|
||||
var deviceId = await _getDeviceId();
|
||||
|
@ -64,8 +74,10 @@ class FirebaseIntroductionRepository
|
|||
if (!introductionCompleted.exists) {
|
||||
return true;
|
||||
}
|
||||
// ignore: avoid_dynamic_calls
|
||||
return !introductionCompleted.data()!["introduction_completed"];
|
||||
_shouldShowIntroduction =
|
||||
// ignore: avoid_dynamic_calls
|
||||
!introductionCompleted.data()!["introduction_completed"];
|
||||
return _shouldShowIntroduction!;
|
||||
} on Exception catch (_) {
|
||||
throw Exception();
|
||||
}
|
||||
|
@ -83,4 +95,10 @@ class FirebaseIntroductionRepository
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> prefetchIntroduction() async {
|
||||
await shouldShow();
|
||||
await fetchIntroductionPages();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,38 +17,33 @@ class Button extends StatelessWidget {
|
|||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var theme = Theme.of(context);
|
||||
|
||||
return Expanded(
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(
|
||||
maxWidth: 180,
|
||||
maxHeight: 32,
|
||||
),
|
||||
child: showButton
|
||||
? FilledButton(
|
||||
style: ButtonStyle(
|
||||
backgroundColor: WidgetStateProperty.all(
|
||||
introductionTheme.buttonBackgroundColor,
|
||||
),
|
||||
shape: WidgetStateProperty.all(
|
||||
RoundedRectangleBorder(
|
||||
borderRadius: const BorderRadius.all(Radius.circular(16)),
|
||||
side: BorderSide(
|
||||
color: introductionTheme.buttonBorderColor,
|
||||
width: 1,
|
||||
),
|
||||
child: showButton
|
||||
? FilledButton(
|
||||
style: ButtonStyle(
|
||||
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||
minimumSize: WidgetStateProperty.all(const Size(160, 32)),
|
||||
backgroundColor: WidgetStateProperty.all(
|
||||
introductionTheme.buttonBackgroundColor,
|
||||
),
|
||||
shape: WidgetStateProperty.all(
|
||||
RoundedRectangleBorder(
|
||||
borderRadius: const BorderRadius.all(Radius.circular(16)),
|
||||
side: BorderSide(
|
||||
color: introductionTheme.buttonBorderColor,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
),
|
||||
onPressed: onPressed,
|
||||
child: Text(
|
||||
text,
|
||||
style: introductionTheme.buttonTextStyle ??
|
||||
theme.textTheme.bodyMedium,
|
||||
),
|
||||
)
|
||||
: const SizedBox(),
|
||||
),
|
||||
),
|
||||
onPressed: onPressed,
|
||||
child: Text(
|
||||
text,
|
||||
style: introductionTheme.buttonTextStyle ??
|
||||
theme.textTheme.bodyMedium,
|
||||
),
|
||||
)
|
||||
: const SizedBox.shrink(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
import "package:flutter/material.dart";
|
||||
import "package:flutter_introduction/flutter_introduction.dart";
|
||||
import "package:flutter_introduction/src/config/introduction_theme.dart";
|
||||
import "package:flutter_introduction/src/enums/introduction_layout_style.dart";
|
||||
|
||||
class IntroductionPage extends StatelessWidget {
|
||||
const IntroductionPage({
|
||||
|
|
|
@ -7,4 +7,6 @@ abstract class IntroductionRepositoryInterface {
|
|||
Future<void> setCompleted({bool value = true});
|
||||
|
||||
Future<bool> shouldShow();
|
||||
|
||||
Future<void> prefetchIntroduction();
|
||||
}
|
||||
|
|
|
@ -11,7 +11,11 @@ class LocalIntroductionRepository implements IntroductionRepositoryInterface {
|
|||
Future<bool> shouldShow() async => !_completed;
|
||||
|
||||
@override
|
||||
Future<List<IntroductionPageData>> fetchIntroductionPages() {
|
||||
throw Exception();
|
||||
Future<List<IntroductionPageData>> fetchIntroductionPages() async => [];
|
||||
|
||||
@override
|
||||
Future<void> prefetchIntroduction() async {
|
||||
await shouldShow();
|
||||
await fetchIntroductionPages();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,4 +16,7 @@ class IntroductionService {
|
|||
|
||||
Future<bool> shouldShow() async =>
|
||||
introductionRepositoryInterface.shouldShow();
|
||||
|
||||
Future<void> prefetchIntroduction() async =>
|
||||
introductionRepositoryInterface.prefetchIntroduction();
|
||||
}
|
||||
|
|
|
@ -3,10 +3,10 @@ import "package:shared_preferences/shared_preferences.dart";
|
|||
|
||||
class SharedPreferencesIntroductionRepository
|
||||
implements IntroductionRepositoryInterface {
|
||||
bool? _shouldShowIntroduction;
|
||||
|
||||
@override
|
||||
Future<List<IntroductionPageData>> fetchIntroductionPages() async {
|
||||
throw Exception();
|
||||
}
|
||||
Future<List<IntroductionPageData>> fetchIntroductionPages() async => [];
|
||||
|
||||
@override
|
||||
Future<void> setCompleted({bool value = true}) async {
|
||||
|
@ -16,8 +16,18 @@ class SharedPreferencesIntroductionRepository
|
|||
|
||||
@override
|
||||
Future<bool> shouldShow() async {
|
||||
if (_shouldShowIntroduction != null) {
|
||||
return !_shouldShowIntroduction!;
|
||||
}
|
||||
var sharedPrefs = await SharedPreferences.getInstance();
|
||||
var shouldShow = sharedPrefs.getBool("_completedIntroduction") ?? true;
|
||||
return !shouldShow;
|
||||
_shouldShowIntroduction = shouldShow;
|
||||
return !_shouldShowIntroduction!;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> prefetchIntroduction() async {
|
||||
await shouldShow();
|
||||
await fetchIntroductionPages();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue