Skip to content

Commit e686581

Browse files
committed
Added Next Button
1 parent c23f0c8 commit e686581

File tree

2 files changed

+138
-36
lines changed

2 files changed

+138
-36
lines changed

lib/UI/page_indicator_buttons.dart

Lines changed: 105 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import 'package:intro_views_flutter/Models/page_button_view_model.dart';
77
class SkipButton extends StatelessWidget {
88
//callback for skip button
99
final VoidCallback onTap;
10+
1011
//view model
1112
final PageButtonViewModel pageButtonViewModel;
1213
final Widget child;
14+
1315
//Constructor
1416
SkipButton({
1517
this.onTap,
@@ -50,9 +52,11 @@ class SkipButton extends StatelessWidget {
5052
class DoneButton extends StatelessWidget {
5153
//Callback
5254
final VoidCallback onTap;
55+
5356
//View Model
5457
final PageButtonViewModel pageButtonViewModel;
5558
final Widget child;
59+
5660
//Constructor
5761
DoneButton({
5862
this.onTap,
@@ -84,36 +88,122 @@ class DoneButton extends StatelessWidget {
8488
}
8589
}
8690

91+
/// Nexg button class
92+
93+
class NextButton extends StatelessWidget {
94+
//callback for skip button
95+
final VoidCallback onTap;
96+
97+
//view model
98+
final PageButtonViewModel pageButtonViewModel;
99+
final Widget child;
100+
101+
//Constructor
102+
NextButton({
103+
this.onTap,
104+
this.pageButtonViewModel,
105+
this.child,
106+
});
107+
108+
@override
109+
Widget build(BuildContext context) {
110+
//Calculating opacity to create a fade in effect
111+
double opacity = 1.0;
112+
final TextStyle style = DefaultTextStyle.of(context).style;
113+
if (pageButtonViewModel.activePageIndex ==
114+
pageButtonViewModel.totalPages - 2 &&
115+
pageButtonViewModel.slideDirection == SlideDirection.rightToLeft) {
116+
opacity = 1.0 - pageButtonViewModel.slidePercent;
117+
} else if (pageButtonViewModel.activePageIndex ==
118+
pageButtonViewModel.totalPages - 1 &&
119+
pageButtonViewModel.slideDirection == SlideDirection.leftToRight) {
120+
opacity = pageButtonViewModel.slidePercent;
121+
}
122+
123+
return FlatButton(
124+
onPressed: onTap,
125+
child: Opacity(
126+
opacity: opacity,
127+
child: DefaultTextStyle.merge(
128+
style: style,
129+
child: child,
130+
), //Text
131+
), //Opacity
132+
); //FlatButton
133+
}
134+
}
135+
87136
class PageIndicatorButtons extends StatelessWidget {
88137
//Some variables
89138
final int acitvePageIndex;
90139
final int totalPages;
91140
final VoidCallback onPressedDoneButton; //Callback for Done Button
141+
final VoidCallback onPressedNextButton;
92142
final VoidCallback onPressedSkipButton; //Callback for Skip Button
93143
final SlideDirection slideDirection;
94144
final double slidePercent;
95145
final bool showSkipButton;
146+
final bool showNextButton;
96147

97148
final Widget doneText;
98149
final Widget skipText;
150+
final Widget nextText;
99151
final TextStyle textStyle;
100152

101153
final bool doneButtonPersist;
102154

155+
Widget _getDoneORNextButton(){
156+
if (acitvePageIndex == totalPages - 1 ||
157+
(acitvePageIndex == totalPages - 2 &&
158+
slideDirection == SlideDirection.rightToLeft ||
159+
doneButtonPersist)){
160+
return DoneButton(
161+
child: doneText,
162+
onTap: onPressedDoneButton,
163+
pageButtonViewModel: PageButtonViewModel(
164+
//view Model
165+
activePageIndex: acitvePageIndex,
166+
totalPages: totalPages,
167+
slidePercent: doneButtonPersist ? 0.0 : slidePercent,
168+
slideDirection: slideDirection,
169+
),
170+
);
171+
}else if ((acitvePageIndex < totalPages - 1 ||
172+
(acitvePageIndex == totalPages - 1 &&
173+
slideDirection == SlideDirection.leftToRight)) &&
174+
showNextButton){
175+
return NextButton(
176+
child: nextText,
177+
onTap: onPressedNextButton,
178+
pageButtonViewModel: PageButtonViewModel(
179+
//View Model
180+
activePageIndex: acitvePageIndex,
181+
totalPages: totalPages,
182+
slidePercent: slidePercent,
183+
slideDirection: slideDirection,
184+
),
185+
);
186+
}else{
187+
return Container();
188+
}
189+
}
190+
103191
//Constructor
104-
PageIndicatorButtons({
105-
@required this.acitvePageIndex,
106-
@required this.totalPages,
107-
this.onPressedDoneButton,
108-
this.slideDirection,
109-
this.slidePercent,
110-
this.onPressedSkipButton,
111-
this.showSkipButton = true,
112-
this.skipText,
113-
this.doneText,
114-
this.textStyle,
115-
this.doneButtonPersist,
116-
});
192+
PageIndicatorButtons(
193+
{@required this.acitvePageIndex,
194+
@required this.totalPages,
195+
this.onPressedDoneButton,
196+
this.slideDirection,
197+
this.slidePercent,
198+
this.onPressedSkipButton,
199+
this.onPressedNextButton,
200+
this.showSkipButton = true,
201+
this.skipText,
202+
this.nextText,
203+
this.doneText,
204+
this.textStyle,
205+
this.doneButtonPersist,
206+
this.showNextButton = true});
117207

118208
@override
119209
Widget build(BuildContext context) {
@@ -149,23 +239,8 @@ class PageIndicatorButtons extends StatelessWidget {
149239
), //Padding
150240
Padding(
151241
padding: const EdgeInsets.only(bottom: 10.0),
152-
child: (acitvePageIndex == totalPages - 1 ||
153-
(acitvePageIndex == totalPages - 2 &&
154-
slideDirection == SlideDirection.rightToLeft ||
155-
doneButtonPersist))
156-
? DoneButton(
157-
child: doneText,
158-
onTap: onPressedDoneButton,
159-
pageButtonViewModel: PageButtonViewModel(
160-
//view Model
161-
activePageIndex: acitvePageIndex,
162-
totalPages: totalPages,
163-
slidePercent: doneButtonPersist ? 0.0 : slidePercent,
164-
slideDirection: slideDirection,
165-
),
166-
)
167-
: Container(), //Row
168-
),
242+
child: _getDoneORNextButton() //Row
243+
)
169244
],
170245
),
171246
),

lib/intro_views_flutter.dart

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class IntroViewsFlutter extends StatefulWidget {
3030
/// Whether you want to show the skip button or not.
3131
final bool showSkipButton;
3232

33+
/// Whether you want to show the next button or not.
34+
final bool showNextButton;
35+
3336
/// TextStyles for done, skip Buttons
3437
///
3538
/// overrides [pageButtonFontFamily] [pageButtonsColor] [pageButtonTextSize]
@@ -38,6 +41,9 @@ class IntroViewsFlutter extends StatefulWidget {
3841
/// run a function after skip Button pressed
3942
final VoidCallback onTapSkipButton;
4043

44+
/// run a function after next Button pressed
45+
final VoidCallback onTapNextButton;
46+
4147
/// set the Text Size for skip, done buttons
4248
///
4349
/// gets overridden by [pageButtonTextStyles]
@@ -52,6 +58,10 @@ class IntroViewsFlutter extends StatefulWidget {
5258
/// typicaly a Text Widget
5359
final Widget doneText;
5460

61+
/// Override 'NEXT' Text with Your Own Text,
62+
/// typicaly a Text Widget
63+
final Widget nextText;
64+
5565
/// Override 'Skip' Text with Your Own Text,
5666
/// typicaly a Text Widget
5767
final Widget skipText;
@@ -78,11 +88,14 @@ class IntroViewsFlutter extends StatefulWidget {
7888
this.onTapDoneButton,
7989
this.showSkipButton = true,
8090
this.pageButtonTextStyles,
91+
this.showNextButton = true,
8192
this.pageButtonTextSize = 18.0,
8293
this.pageButtonFontFamily,
8394
this.onTapSkipButton,
95+
this.onTapNextButton,
8496
this.pageButtonsColor,
8597
this.doneText = const Text("DONE"),
98+
this.nextText = const Text("NEXT"),
8699
this.skipText = const Text("SKIP"),
87100
this.doneButtonPersist = false,
88101
this.columnMainAxisAlignment = MainAxisAlignment.spaceAround,
@@ -140,17 +153,17 @@ class _IntroViewsFlutterState extends State<IntroViewsFlutter>
140153
if (slidePercent > 0.5) {
141154
animatedPageDragger = AnimatedPageDragger(
142155
slideDirection: slideDirection,
143-
transitionGoal:
144-
TransitionGoal.open, //we have to animate the open page reveal
156+
transitionGoal: TransitionGoal.open,
157+
//we have to animate the open page reveal
145158
slidePercent: slidePercent,
146159
slideUpdateStream: slideUpdateStream,
147160
vsync: this,
148161
);
149162
} else {
150163
animatedPageDragger = AnimatedPageDragger(
151164
slideDirection: slideDirection,
152-
transitionGoal:
153-
TransitionGoal.close, //we have to close the page reveal
165+
transitionGoal: TransitionGoal.close,
166+
//we have to close the page reveal
154167
slidePercent: slidePercent,
155168
slideUpdateStream: slideUpdateStream,
156169
vsync: this,
@@ -236,8 +249,8 @@ class _IntroViewsFlutterState extends State<IntroViewsFlutter>
236249
textStyle: textStyle,
237250
acitvePageIndex: activePageIndex,
238251
totalPages: pages.length,
239-
onPressedDoneButton: widget
240-
.onTapDoneButton, //void Callback to be executed after pressing done button
252+
onPressedDoneButton: widget.onTapDoneButton,
253+
//void Callback to be executed after pressing done button
241254
slidePercent: slidePercent,
242255
slideDirection: slideDirection,
243256
onPressedSkipButton: () {
@@ -253,6 +266,20 @@ class _IntroViewsFlutterState extends State<IntroViewsFlutter>
253266
});
254267
},
255268
showSkipButton: widget.showSkipButton,
269+
showNextButton: widget.showNextButton,
270+
onPressedNextButton: () {
271+
//method executed on pressing next button
272+
setState(() {
273+
activePageIndex = activePageIndex + 1;
274+
nextPageIndex = nextPageIndex + 1;
275+
// after skip pressed invoke function
276+
// this can be used for analytics/page transition
277+
if (widget.onTapNextButton != null) {
278+
widget.onTapNextButton();
279+
}
280+
});
281+
},
282+
nextText: widget.nextText,
256283
doneText: widget.doneText,
257284
skipText: widget.skipText,
258285
doneButtonPersist: widget.doneButtonPersist,

0 commit comments

Comments
 (0)