@@ -40,6 +40,12 @@ class MyHomePage extends StatefulWidget {
40
40
41
41
class _MyHomePageState extends State <MyHomePage > {
42
42
late CustomImageCropController controller;
43
+ CustomCropShape _currentShape = CustomCropShape .Circle ;
44
+ final TextEditingController _widthController = TextEditingController ();
45
+ final TextEditingController _heightController = TextEditingController ();
46
+
47
+ double _width = 16 ;
48
+ double _height = 9 ;
43
49
44
50
@override
45
51
void initState () {
@@ -53,6 +59,24 @@ class _MyHomePageState extends State<MyHomePage> {
53
59
super .dispose ();
54
60
}
55
61
62
+ void _changeCropShape (CustomCropShape newShape) {
63
+ setState (() {
64
+ _currentShape = newShape;
65
+ });
66
+ }
67
+
68
+ void _updateRatio () {
69
+ setState (() {
70
+ if (_widthController.text.isNotEmpty) {
71
+ _width = double .tryParse (_widthController.text) ?? 16 ;
72
+ }
73
+ if (_heightController.text.isNotEmpty) {
74
+ _height = double .tryParse (_heightController.text) ?? 9 ;
75
+ }
76
+ });
77
+ FocusScope .of (context).unfocus ();
78
+ }
79
+
56
80
@override
57
81
Widget build (BuildContext context) {
58
82
return Scaffold (
@@ -62,20 +86,54 @@ class _MyHomePageState extends State<MyHomePage> {
62
86
),
63
87
body: Column (
64
88
children: [
89
+ // SizedBox(
90
+ // height: 40,
91
+ // child: Row(
92
+ // mainAxisAlignment: MainAxisAlignment.center,
93
+ // children: [
94
+ // IconButton(
95
+ // icon: const Icon(Icons.crop_free),
96
+ // color: _currentShape == CustomCropShape.Circle
97
+ // ? Colors.blue
98
+ // : Colors.grey,
99
+ // onPressed: () => _changeCropShape(CustomCropShape.Circle),
100
+ // ),
101
+ // IconButton(
102
+ // icon: const Icon(Icons.crop_square),
103
+ // color: _currentShape == CustomCropShape.Square
104
+ // ? Colors.blue
105
+ // : Colors.grey,
106
+ // onPressed: () => _changeCropShape(CustomCropShape.Square),
107
+ // ),
108
+ // IconButton(
109
+ // icon: const Icon(Icons.crop_16_9),
110
+ // color: _currentShape == CustomCropShape.Ratio
111
+ // ? Colors.blue
112
+ // : Colors.grey,
113
+ // onPressed: () => _changeCropShape(CustomCropShape.Ratio),
114
+ // ),
115
+ // ],
116
+ // ),
117
+ // ),
118
+
65
119
Expanded (
66
120
child: CustomImageCrop (
67
121
cropController: controller,
68
122
// image: const AssetImage('assets/test.png'), // Any Imageprovider will work, try with a NetworkImage for example...
69
123
image: const NetworkImage (
70
124
'https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png' ),
71
- shape: CustomCropShape .Square ,
125
+ shape: _currentShape,
126
+ ratio: _currentShape == CustomCropShape .Ratio
127
+ ? Ratio (width: _width, height: _height)
128
+ : null ,
72
129
canRotate: true ,
73
130
canMove: false ,
74
131
canScale: false ,
75
132
customProgressIndicator: const CupertinoActivityIndicator (),
76
133
),
77
134
),
78
135
Row (
136
+ mainAxisAlignment: MainAxisAlignment .spaceAround,
79
137
children: [
80
138
IconButton (
81
139
icon: const Icon (Icons .refresh), onPressed: controller.reset),
@@ -106,11 +164,61 @@ class _MyHomePageState extends State<MyHomePage> {
106
164
}
107
165
},
108
166
),
167
+ PopupMenuButton <CustomCropShape >(
168
+ icon: const Icon (Icons .crop_original),
169
+ onSelected: _changeCropShape,
170
+ itemBuilder: (BuildContext context) {
171
+ return CustomCropShape .values.map ((shape) {
172
+ return PopupMenuItem <CustomCropShape >(
173
+ value: shape,
174
+ child: getShapeIcon (shape),
175
+ );
176
+ }).toList ();
177
+ },
178
+ )
109
179
],
110
180
),
181
+ if (_currentShape == CustomCropShape .Ratio )
182
+ SizedBox (
183
+ child: Row (
184
+ children: [
185
+ Expanded (
186
+ child: TextField (
187
+ controller: _widthController,
188
+ keyboardType: TextInputType .number,
189
+ decoration: const InputDecoration (labelText: 'Width' ),
190
+ ),
191
+ ),
192
+ const SizedBox (width: 16.0 ),
193
+ Expanded (
194
+ child: TextField (
195
+ controller: _heightController,
196
+ keyboardType: TextInputType .number,
197
+ decoration: const InputDecoration (labelText: 'Height' ),
198
+ ),
199
+ ),
200
+ const SizedBox (width: 16.0 ),
201
+ ElevatedButton (
202
+ onPressed: _updateRatio,
203
+ child: const Text ('Update Ratio' ),
204
+ ),
205
+ ],
206
+ ),
207
+ ),
111
208
SizedBox (height: MediaQuery .of (context).padding.bottom),
112
209
],
113
210
),
114
211
);
115
212
}
213
+
214
+ Widget getShapeIcon (CustomCropShape shape) {
215
+ switch (shape) {
216
+ case CustomCropShape .Circle :
217
+ return const Icon (Icons .circle_outlined);
218
+ case CustomCropShape .Square :
219
+ return const Icon (Icons .square_outlined);
220
+ case CustomCropShape .Ratio :
221
+ return const Icon (Icons .crop_16_9_outlined);
222
+ }
223
+ }
116
224
}
0 commit comments