Skip to content

Commit 0e0c273

Browse files
committed
fix(currentPage): Revert currentPage as a double to have dots animation
1 parent fe3a2c0 commit 0e0c273

File tree

2 files changed

+58
-33
lines changed

2 files changed

+58
-33
lines changed

lib/src/introduction_screen.dart

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import '/src/model/position.dart';
1313
import '/src/ui/intro_button.dart';
1414
import '/src/ui/intro_page.dart';
1515

16-
bool defaultCanProgressFunction(int page) {
16+
bool kDefaultCanProgressFunction(int page) {
1717
return true;
1818
}
1919

20+
typedef CanProgress = bool Function(int page);
21+
2022
class IntroductionScreen extends StatefulWidget {
2123
/// All pages of the onboarding
2224
final List<PageViewModel>? pages;
@@ -263,18 +265,17 @@ class IntroductionScreen extends StatefulWidget {
263265
///
264266
/// @Default `true`
265267
/// ```dart
266-
/// canProgress: (page) {
267-
/// int _currentPage = page.round();
268-
/// if (_currentPage == 0 && _textFieldController1.text.isEmpty) {
268+
/// canProgress: (int page) {
269+
/// if (page == 0 && _textFieldController1.text.isEmpty) {
269270
/// return false;
270-
/// } else if (_currentPage == 1 && _textFieldController2.text.isEmpty) {
271+
/// } else if (page == 1 && _textFieldController2.text.isEmpty) {
271272
/// return false;
272273
/// } else {
273274
/// return true;
274275
/// }
275276
/// }
276277
/// ```
277-
final Function canProgress;
278+
final CanProgress canProgress;
278279

279280
IntroductionScreen(
280281
{Key? key,
@@ -333,7 +334,7 @@ class IntroductionScreen extends StatefulWidget {
333334
this.scrollPhysics = const BouncingScrollPhysics(),
334335
this.rtl = false,
335336
this.allowImplicitScrolling = false,
336-
this.canProgress = defaultCanProgressFunction,
337+
this.canProgress = kDefaultCanProgressFunction,
337338
this.safeAreaList = const [false, false, false, false]})
338339
: assert(
339340
pages != null || rawPages != null,
@@ -384,7 +385,8 @@ class IntroductionScreen extends StatefulWidget {
384385
'customProgress can only be used if isProgress = true',
385386
),
386387
assert(
387-
(infiniteAutoScroll && autoScrollDuration != null) || !infiniteAutoScroll,
388+
(infiniteAutoScroll && autoScrollDuration != null) ||
389+
!infiniteAutoScroll,
388390
'infiniteAutoScroll can only be true if autoScrollDuration != null',
389391
),
390392
super(key: key);
@@ -395,7 +397,7 @@ class IntroductionScreen extends StatefulWidget {
395397

396398
class IntroductionScreenState extends State<IntroductionScreen> {
397399
late PageController _pageController;
398-
int _currentPage = 0;
400+
double _currentPage = 0;
399401
bool _isSkipPressed = false;
400402
bool _isScrolling = false;
401403
late bool _showBottom;
@@ -409,15 +411,17 @@ class IntroductionScreenState extends State<IntroductionScreen> {
409411
final int initialPage = min(widget.initialPage, getPagesLength() - 1);
410412
_pageController = PageController(initialPage: initialPage);
411413
_showBottom = widget.showBottomPart;
412-
_currentPage = initialPage;
414+
_currentPage = initialPage.toDouble();
413415
_autoScroll(widget.autoScrollDuration);
414416
if (widget.hideBottomOnKeyboard) {
415417
final keyboardVisibilityController = KeyboardVisibilityController();
416-
keyboardSubscription = keyboardVisibilityController.onChange.listen((bool visible) {
417-
setState(() {
418-
_showBottom = !visible;
419-
});
420-
});
418+
keyboardSubscription = keyboardVisibilityController.onChange.listen(
419+
(bool visible) {
420+
setState(() {
421+
_showBottom = !visible;
422+
});
423+
},
424+
);
421425
}
422426
}
423427

@@ -434,10 +438,14 @@ class IntroductionScreenState extends State<IntroductionScreen> {
434438
return (widget.pages ?? widget.rawPages!).length;
435439
}
436440

441+
int getCurrentPage() => _currentPage.round();
442+
437443
Future<void> _autoScroll(int? _durationInt) async {
438444
if (_durationInt != null) {
439445
final Duration _autoscrollDuration = Duration(milliseconds: _durationInt);
440-
final _animationDuration = Duration(milliseconds: widget.animationDuration);
446+
final _animationDuration = Duration(
447+
milliseconds: widget.animationDuration,
448+
);
441449
final int pagesLength = getPagesLength() - 1;
442450
if (widget.infiniteAutoScroll) {
443451
while (true) {
@@ -465,7 +473,11 @@ class IntroductionScreenState extends State<IntroductionScreen> {
465473
}
466474
}
467475

468-
Future<void> _movePage(Duration autoscrollDuration, Duration animationDuration, bool forward) async {
476+
Future<void> _movePage(
477+
Duration autoscrollDuration,
478+
Duration animationDuration,
479+
bool forward,
480+
) async {
469481
await Future.delayed(autoscrollDuration);
470482
if (!_isSkipPressed && !_isScrolling) {
471483
if (forward) {
@@ -483,9 +495,15 @@ class IntroductionScreenState extends State<IntroductionScreen> {
483495
}
484496
}
485497

486-
void next() => {animateScroll(_currentPage + 1), FocusScope.of(context).unfocus()};
498+
void next() {
499+
animateScroll(getCurrentPage() + 1);
500+
FocusScope.of(context).unfocus();
501+
}
487502

488-
void previous() => {animateScroll(_currentPage - 1), FocusScope.of(context).unfocus()};
503+
void previous() {
504+
animateScroll(getCurrentPage() - 1);
505+
FocusScope.of(context).unfocus();
506+
}
489507

490508
Future<void> _onSkip() async {
491509
if (widget.onSkip != null) {
@@ -504,7 +522,7 @@ class IntroductionScreenState extends State<IntroductionScreen> {
504522
}
505523

506524
Future<void> animateScroll(int page) async {
507-
bool isValidToProgress = widget.canProgress(_currentPage);
525+
bool isValidToProgress = widget.canProgress(getCurrentPage());
508526
if (isValidToProgress) {
509527
_isScrolling = true;
510528
await _pageController.animateToPage(
@@ -522,7 +540,7 @@ class IntroductionScreenState extends State<IntroductionScreen> {
522540
final metrics = notification.metrics;
523541
if (metrics is PageMetrics && metrics.page != null) {
524542
if (mounted) {
525-
setState(() => _currentPage = metrics.page!.round());
543+
setState(() => _currentPage = metrics.page!);
526544
}
527545
}
528546
return false;
@@ -544,7 +562,8 @@ class IntroductionScreenState extends State<IntroductionScreen> {
544562
child: widget.overrideSkip ??
545563
IntroButton(
546564
child: widget.skip!,
547-
style: widget.baseBtnStyle?.merge(widget.skipStyle) ?? widget.skipStyle,
565+
style: widget.baseBtnStyle?.merge(widget.skipStyle) ??
566+
widget.skipStyle,
548567
semanticLabel: widget.skipSemantic,
549568
onPressed: _onSkip,
550569
),
@@ -553,7 +572,8 @@ class IntroductionScreenState extends State<IntroductionScreen> {
553572
leftBtn = widget.overrideBack ??
554573
IntroButton(
555574
child: widget.back!,
556-
style: widget.baseBtnStyle?.merge(widget.backStyle) ?? widget.backStyle,
575+
style: widget.baseBtnStyle?.merge(widget.backStyle) ??
576+
widget.backStyle,
557577
semanticLabel: widget.backSemantic,
558578
onPressed: !_isScrolling ? previous : null,
559579
);
@@ -564,15 +584,17 @@ class IntroductionScreenState extends State<IntroductionScreen> {
564584
rightBtn = widget.overrideDone ??
565585
IntroButton(
566586
child: widget.done!,
567-
style: widget.baseBtnStyle?.merge(widget.doneStyle) ?? widget.doneStyle,
587+
style: widget.baseBtnStyle?.merge(widget.doneStyle) ??
588+
widget.doneStyle,
568589
semanticLabel: widget.doneSemantic,
569590
onPressed: !_isScrolling ? widget.onDone : null,
570591
);
571592
} else if (!isLastPage && widget.showNextButton) {
572593
rightBtn = widget.overrideNext ??
573594
IntroButton(
574595
child: widget.next!,
575-
style: widget.baseBtnStyle?.merge(widget.nextStyle) ?? widget.nextStyle,
596+
style: widget.baseBtnStyle?.merge(widget.nextStyle) ??
597+
widget.nextStyle,
576598
semanticLabel: widget.nextSemantic,
577599
onPressed: !_isScrolling ? next : null,
578600
);
@@ -603,14 +625,16 @@ class IntroductionScreenState extends State<IntroductionScreen> {
603625
allowImplicitScrolling: widget.allowImplicitScrolling,
604626
physics: widget.freeze
605627
? const NeverScrollableScrollPhysics()
606-
: !widget.canProgress(_currentPage)
628+
: !widget.canProgress(getCurrentPage())
607629
? const NeverScrollableScrollPhysics()
608630
: widget.scrollPhysics,
609631
children: widget.pages
610632
?.mapIndexed(
611633
(index, page) => IntroPage(
612634
page: page,
613-
scrollController: (CustomList(widget.scrollControllers)?.elementAtOrNull(index)),
635+
scrollController: CustomList(
636+
widget.scrollControllers,
637+
)?.elementAtOrNull(index),
614638
),
615639
)
616640
.toList() ??
@@ -649,15 +673,18 @@ class IntroductionScreenState extends State<IntroductionScreen> {
649673
child: widget.isProgress
650674
? widget.customProgress ??
651675
Semantics(
652-
label: "Page ${_currentPage.round() + 1} of ${getPagesLength()}",
676+
label:
677+
"Page ${getCurrentPage() + 1} of ${getPagesLength()}",
653678
excludeSemantics: true,
654679
child: DotsIndicator(
655680
reversed: widget.rtl,
656681
dotsCount: getPagesLength(),
657682
position: _currentPage,
658683
decorator: widget.dotsDecorator,
659-
onTap: widget.isProgressTap && !widget.freeze
660-
? (pos) => animateScroll(pos.toInt())
684+
onTap: widget.isProgressTap &&
685+
!widget.freeze
686+
? (pos) =>
687+
animateScroll(pos.toInt())
661688
: null,
662689
),
663690
)

pubspec.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
name: introduction_screen
22
description: Introduction/Onboarding package for flutter app with some customizations possibilities
3-
43
version: 3.1.9
5-
64
homepage: https://github.com/pyozer/introduction_screen
75

86
environment:
@@ -13,7 +11,7 @@ dependencies:
1311
sdk: flutter
1412

1513
collection: ^1.17.1
16-
dots_indicator: ^3.0.0
14+
dots_indicator: ^2.1.2
1715
flutter_keyboard_visibility: ^5.4.1
1816

1917
dev_dependencies:

0 commit comments

Comments
 (0)