|
| 1 | +import 'package:contraflutterkit/custom_widgets/circle_dot_widget.dart'; |
| 2 | +import 'package:contraflutterkit/utils/colors.dart'; |
| 3 | +import 'package:flutter/cupertino.dart'; |
| 4 | +import 'package:flutter_svg/flutter_svg.dart'; |
| 5 | + |
| 6 | +class PagerImageView extends StatefulWidget { |
| 7 | + final List<String> images; |
| 8 | + |
| 9 | + const PagerImageView({this.images}); |
| 10 | + |
| 11 | + @override |
| 12 | + _PagerImageViewState createState() => _PagerImageViewState(); |
| 13 | +} |
| 14 | + |
| 15 | +class _PagerImageViewState extends State<PagerImageView> { |
| 16 | + PageController _pageController; |
| 17 | + int currentPageValue = 0; |
| 18 | + int previousPageValue = 0; |
| 19 | + double _moveBar = 0.0; |
| 20 | + List<String> _images = []; |
| 21 | + final List<Widget> _cards = []; |
| 22 | + |
| 23 | + @override |
| 24 | + void initState() { |
| 25 | + _images = widget.images; |
| 26 | + _cards.add(SvgPicture.asset( |
| 27 | + _images[0], |
| 28 | + height: 290, |
| 29 | + width: 300, |
| 30 | + )); |
| 31 | + _cards.add(SvgPicture.asset( |
| 32 | + _images[1], |
| 33 | + height: 290, |
| 34 | + width: 300, |
| 35 | + )); |
| 36 | + _cards.add(SvgPicture.asset( |
| 37 | + _images[2], |
| 38 | + height: 290, |
| 39 | + width: 300, |
| 40 | + )); |
| 41 | + super.initState(); |
| 42 | + _pageController = PageController(initialPage: currentPageValue); |
| 43 | + } |
| 44 | + |
| 45 | + @override |
| 46 | + void dispose() { |
| 47 | + _pageController.dispose(); |
| 48 | + super.dispose(); |
| 49 | + } |
| 50 | + |
| 51 | + @override |
| 52 | + Widget build(BuildContext context) { |
| 53 | + return Column( |
| 54 | + children: <Widget>[ |
| 55 | + Container( |
| 56 | + height: 310, |
| 57 | + padding: EdgeInsets.symmetric(horizontal: 16), |
| 58 | + child: PageView.builder( |
| 59 | + physics: ClampingScrollPhysics(), |
| 60 | + itemBuilder: (context, index) { |
| 61 | + return _cards[index]; |
| 62 | + }, |
| 63 | + onPageChanged: (int page) { |
| 64 | + animatePage(page); |
| 65 | + }, |
| 66 | + itemCount: _cards.length, |
| 67 | + controller: _pageController, |
| 68 | + ), |
| 69 | + ), |
| 70 | + Padding( |
| 71 | + padding: const EdgeInsets.all(24.0), |
| 72 | + child: Row( |
| 73 | + mainAxisAlignment: MainAxisAlignment.center, |
| 74 | + children: <Widget>[ |
| 75 | + for (int i = 0; i < _cards.length; i++) |
| 76 | + if (i == currentPageValue) ...[ |
| 77 | + CircleDotWidget( |
| 78 | + isActive: true, |
| 79 | + color: white, |
| 80 | + borderColor: white, |
| 81 | + ) |
| 82 | + ] else |
| 83 | + CircleDotWidget( |
| 84 | + isActive: false, |
| 85 | + color: flamingo, |
| 86 | + borderColor: wood_smoke, |
| 87 | + ), |
| 88 | + ], |
| 89 | + ), |
| 90 | + ), |
| 91 | + ], |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + void animatePage(int page) { |
| 96 | + print('page is $page'); |
| 97 | + currentPageValue = page; |
| 98 | + if (previousPageValue == 0) { |
| 99 | + previousPageValue = currentPageValue; |
| 100 | + _moveBar = _moveBar + 0.14; |
| 101 | + } else { |
| 102 | + if (previousPageValue < currentPageValue) { |
| 103 | + previousPageValue = currentPageValue; |
| 104 | + _moveBar = _moveBar + 0.14; |
| 105 | + } else { |
| 106 | + previousPageValue = currentPageValue; |
| 107 | + _moveBar = _moveBar - 0.14; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + setState(() {}); |
| 112 | + } |
| 113 | +} |
0 commit comments