11import 'dart:async' ;
2+ import 'dart:ffi' ;
23import 'package:flutter/material.dart' ;
34
5+ List <T > map <T >(List list, Function handler) {
6+ List <T > result = [];
7+ for (var i = 0 ; i < list.length; i++ ) {
8+ result.add (handler (i, list[i]));
9+ }
10+ return result;
11+ }
12+
413class GFSlider extends StatefulWidget {
514 GFSlider (
615 {@required this .items,
16+ this .pagerSize,
17+ this .passiveIndicator,
18+ this .activeIndicator,
19+ this .pagination,
720 this .height,
821 this .aspectRatio: 16 / 9 ,
922 this .viewportFraction: 0.8 ,
@@ -26,6 +39,18 @@ class GFSlider extends StatefulWidget {
2639 initialPage: enableInfiniteScroll ? realPage + initialPage : initialPage,
2740 );
2841
42+ /// The pagination dots size can be defined using [double] .
43+ final double pagerSize;
44+
45+ /// The slider pagination's active color.
46+ final Color activeIndicator;
47+
48+ /// The slider pagination's passive color.
49+ final Color passiveIndicator;
50+
51+ /// The [GFSlider] shows pagination on state true.
52+ final bool pagination;
53+
2954 /// The widgets to be shown as sliders.
3055 final List <Widget > items;
3156
@@ -177,56 +202,91 @@ class _GFSliderState extends State<GFSlider> with TickerProviderStateMixin {
177202 timer? .cancel ();
178203 }
179204
205+ int _current = 0 ;
206+
180207 @override
181208 Widget build (BuildContext context) {
182- return getPageWrapper (PageView .builder (
183- physics: widget.scrollPhysics,
184- scrollDirection: widget.scrollDirection,
185- controller: widget.pageController,
186- reverse: widget.reverse,
187- itemCount: widget.enableInfiniteScroll ? null : widget.items.length,
188- onPageChanged: (int index) {
189- int currentPage = _getRealIndex (index + widget.initialPage, widget.realPage, widget.items.length);
190- if (widget.onPageChanged != null ) {
191- widget.onPageChanged (currentPage);
192- }
193- },
194- itemBuilder: (BuildContext context, int i) {
195- final int index =
196- _getRealIndex (i + widget.initialPage, widget.realPage, widget.items.length);
197-
198- return AnimatedBuilder (
199- animation: widget.pageController,
200- child: widget.items[index],
201- builder: (BuildContext context, child) {
202- // on the first render, the pageController.page is null,
203- // this is a dirty hack
204- if (widget.pageController.position.minScrollExtent == null ||
205- widget.pageController.position.maxScrollExtent == null ) {
206- Future .delayed (Duration (microseconds: 1 ), () {
207- setState (() {});
208- });
209- return Container ();
210- }
211- double value = widget.pageController.page - i;
212- value = (1 - (value.abs () * 0.3 )).clamp (0.0 , 1.0 );
213-
214- final double height =
215- widget.height ?? MediaQuery .of (context).size.width * (1 / widget.aspectRatio);
216- final double distortionValue =
217- widget.enlargeMainPage ? Curves .easeOut.transform (value) : 1.0 ;
218-
219- if (widget.scrollDirection == Axis .horizontal) {
220- return Center (child: SizedBox (height: distortionValue * height, child: child));
221- } else {
222- return Center (
223- child: SizedBox (
224- width: distortionValue * MediaQuery .of (context).size.width, child: child));
209+ return Stack (
210+ children: < Widget > [
211+ getPageWrapper (PageView .builder (
212+ physics: widget.scrollPhysics,
213+ scrollDirection: widget.scrollDirection,
214+ controller: widget.pageController,
215+ reverse: widget.reverse,
216+ itemCount: widget.enableInfiniteScroll ? null : widget.items.length,
217+ onPageChanged: (int index) {
218+
219+ int currentPage = _getRealIndex (index + widget.initialPage, widget.realPage, widget.items.length);
220+ if (widget.onPageChanged != null ) {
221+ widget.onPageChanged (currentPage);
222+ _current = currentPage;
225223 }
224+ _current = currentPage;
226225 },
227- );
228- },
229- ));
226+ itemBuilder: (BuildContext context, int i) {
227+ final int index =
228+ _getRealIndex (i + widget.initialPage, widget.realPage, widget.items.length);
229+
230+ return AnimatedBuilder (
231+ animation: widget.pageController,
232+ child: widget.items[index],
233+ builder: (BuildContext context, child) {
234+ // on the first render, the pageController.page is null,
235+ // this is a dirty hack
236+ if (widget.pageController.position.minScrollExtent == null ||
237+ widget.pageController.position.maxScrollExtent == null ) {
238+ Future .delayed (Duration (microseconds: 1 ), () {
239+ setState (() {});
240+ });
241+ return Container ();
242+ }
243+ double value = widget.pageController.page - i;
244+ value = (1 - (value.abs () * 0.3 )).clamp (0.0 , 1.0 );
245+
246+ final double height =
247+ widget.height ?? MediaQuery .of (context).size.width * (1 / widget.aspectRatio);
248+ final double distortionValue =
249+ widget.enlargeMainPage ? Curves .easeOut.transform (value) : 1.0 ;
250+
251+ if (widget.scrollDirection == Axis .horizontal) {
252+ return Center (child: SizedBox (height: distortionValue * height, child: child));
253+ } else {
254+ return Center (
255+ child: SizedBox (
256+ width: distortionValue * MediaQuery .of (context).size.width, child: child));
257+ }
258+ },
259+ );
260+ },
261+ )),
262+ widget.pagination == true ? Positioned (
263+ left: 0.0 ,
264+ right: 0.0 ,
265+ bottom: 0.0 ,
266+ child: Container (
267+ child: Row (
268+ mainAxisAlignment: MainAxisAlignment .center,
269+ children: map <Widget >(
270+ widget.items,
271+ (indexx, url) {
272+ return Container (
273+ width: widget.pagerSize == null ? 8.0 : widget.pagerSize,
274+ height: widget.pagerSize == null ? 8.0 : widget.pagerSize,
275+ margin: EdgeInsets .symmetric (vertical: 10.0 , horizontal: 2.0 ),
276+ decoration: BoxDecoration (
277+ shape: BoxShape .circle,
278+ color: _current == indexx
279+ ? widget.activeIndicator == null ? Color .fromRGBO (0 , 0 , 0 , 0.9 ) : widget.activeIndicator
280+ : widget.passiveIndicator == null ? Color .fromRGBO (0 , 0 , 0 , 0.4 ) : widget.passiveIndicator,
281+ ),
282+ );
283+ },
284+ ),
285+ ),
286+ ),
287+ ) : Container (),
288+ ],
289+ );
230290 }
231291}
232292
0 commit comments