-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy path26. flutter_pageview_changer.dart
113 lines (104 loc) · 3.69 KB
/
26. flutter_pageview_changer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import 'package:flutter/material.dart';
import 'package:ig_posts/features/1.flutter_frost_glass.view.dart';
import 'package:ig_posts/features/5.flutter_swiggy_button.dart';
import 'package:provider/provider.dart';
class PageChangerNotifier extends ChangeNotifier {
int intialIndex = 0;
final PageController pageController = PageController();
changeIndex({required bool forward}) {
debugPrint(forward.toString());
if (forward) {
intialIndex++;
pageController.jumpToPage(intialIndex);
notifyListeners();
} else {
if (intialIndex > 0) {
intialIndex--;
pageController.jumpToPage(intialIndex);
notifyListeners();
}
}
}
}
class FlutterPageChangerWidget extends StatelessWidget {
const FlutterPageChangerWidget({super.key});
@override
Widget build(BuildContext context) {
PageChangerNotifier pageChangerNotifier({required bool renderUI}) =>
Provider.of<PageChangerNotifier>(context, listen: renderUI);
PageController pageController =
pageChangerNotifier(renderUI: true).pageController;
Widget changerButton(
{required IconData iconData, required Function onTap}) {
return GestureDetector(
onTap: () {
onTap();
},
child: CircleAvatar(
backgroundColor: bgColor,
child: Icon(iconData, color: Colors.white),
),
);
}
int currentPageIndex = pageChangerNotifier(renderUI: true).intialIndex;
return Scaffold(
backgroundColor: bgColorFaint,
appBar: AppBar(
backgroundColor: bgColor,
title: Text("Flutter page changer", style: boldText())),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text("Page index : $currentPageIndex", style: boldText(fSize: 30)),
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
changerButton(
iconData: Icons.arrow_back_ios,
onTap: () {
pageChangerNotifier(renderUI: false)
.changeIndex(forward: false);
}),
SizedBox(
height: 600,
width: 300,
child: PageView.builder(
controller: pageController,
scrollDirection: Axis.horizontal,
itemCount: 4,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 600,
width: 300,
decoration: BoxDecoration(
color: currentPageIndex % 2 == 0
? Colors.blue
: Colors.red,
borderRadius: BorderRadius.circular(25)),
child: Center(
child: Text(index.toString(), style: boldText())),
),
);
},
),
),
changerButton(
iconData: Icons.arrow_forward_ios,
onTap: () {
debugPrint("agaga");
pageChangerNotifier(renderUI: false)
.changeIndex(forward: true);
}),
],
),
],
),
),
);
}
}