Line data Source code
1 : import 'package:flutter/material.dart';
2 : import 'package:flutter/services.dart';
3 : import 'package:flutter/widgets.dart';
4 : import 'package:pal/src/theme.dart';
5 : import 'package:pal/src/ui/editor/pages/create_helper/widgets/preview_card_swiper/dot_indicators.dart';
6 : import 'package:pal/src/ui/editor/pages/create_helper/widgets/preview_card_swiper/preview_card.dart';
7 :
8 : class PreviewCardSwiperWidget extends StatefulWidget {
9 : final List<PreviewCard> cards;
10 : final String note;
11 : final Function(int) onCardSelected;
12 :
13 2 : PreviewCardSwiperWidget({
14 : Key key,
15 : @required this.cards,
16 : this.note,
17 : this.onCardSelected,
18 2 : }) : super(key: key);
19 :
20 2 : @override
21 : _PreviewCardSwiperWidgetState createState() =>
22 2 : _PreviewCardSwiperWidgetState();
23 : }
24 :
25 : class _PreviewCardSwiperWidgetState extends State<PreviewCardSwiperWidget>
26 : with SingleTickerProviderStateMixin {
27 : PageController _controller;
28 : int _currentpage = 0;
29 :
30 2 : @override
31 : initState() {
32 2 : super.initState();
33 4 : _controller = PageController(
34 2 : initialPage: _currentpage,
35 : viewportFraction: 0.9,
36 : );
37 : }
38 :
39 2 : @override
40 : dispose() {
41 4 : _controller.dispose();
42 2 : super.dispose();
43 : }
44 :
45 2 : @override
46 : Widget build(BuildContext context) {
47 2 : return Column(
48 : mainAxisAlignment: MainAxisAlignment.spaceEvenly,
49 2 : children: [
50 4 : if (widget.note != null)
51 1 : Text(
52 2 : widget.note,
53 1 : style: TextStyle(
54 : fontWeight: FontWeight.w300,
55 3 : color: PalTheme.of(context).colors.color1,
56 : fontSize: 10,
57 : ),
58 : ),
59 2 : Expanded(
60 4 : child: LayoutBuilder(builder: (context, constraints) {
61 2 : return Stack(
62 2 : children: [
63 2 : PageView.builder(
64 2 : key: ValueKey('pal_PreviewCardSwiperWidget_PageView'),
65 2 : controller: _controller,
66 6 : itemCount: widget.cards.length,
67 2 : onPageChanged: _onPageViewChange,
68 2 : itemBuilder: (BuildContext context, int index) {
69 2 : return _buildPreviewCard(
70 : context,
71 : index,
72 : );
73 : },
74 : ),
75 2 : Positioned(
76 : bottom: 10,
77 : left: 0,
78 : right: 0,
79 2 : child: DotIndicatorsWidget(
80 2 : activePage: _currentpage,
81 6 : pagesCount: widget.cards.length,
82 : ),
83 : ),
84 : ],
85 : );
86 : }),
87 : ),
88 : ],
89 : );
90 : }
91 :
92 0 : void _onPageViewChange(int index) {
93 0 : HapticFeedback.selectionClick();
94 0 : setState(() {
95 0 : _currentpage = index;
96 : });
97 : }
98 :
99 2 : Widget _buildPreviewCard(
100 : BuildContext context,
101 : int index,
102 : ) {
103 6 : PreviewCard cardData = widget.cards[index];
104 :
105 2 : return Padding(
106 2 : padding: EdgeInsets.only(
107 : left: 8.0,
108 : right: 8.0,
109 : top: 10.0,
110 : bottom: 60.0,
111 : ),
112 2 : child: PreviewCardWidget(
113 : index: index,
114 : cardData: cardData,
115 2 : onTap: _onCardTap,
116 : ),
117 : );
118 : }
119 :
120 2 : void _onCardTap(int index) {
121 : int i = 0;
122 6 : for (PreviewCard cardData in widget.cards) {
123 4 : if (index == i++) {
124 4 : cardData.isSelected = !cardData.isSelected;
125 : } else {
126 1 : cardData.isSelected = false;
127 : }
128 : }
129 4 : this.setState(() {});
130 :
131 4 : if (widget.onCardSelected != null) {
132 6 : widget.onCardSelected(index);
133 : }
134 : }
135 : }
|