@@ -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 (
@@ -68,14 +92,18 @@ class _MyHomePageState extends State<MyHomePage> {
68
92
// image: const AssetImage('assets/test.png'), // Any Imageprovider will work, try with a NetworkImage for example...
69
93
image: const NetworkImage (
70
94
'https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png' ),
71
- shape: CustomCropShape .Square ,
95
+ shape: _currentShape,
96
+ ratio: _currentShape == CustomCropShape .Ratio
97
+ ? Ratio (width: _width, height: _height)
98
+ : null ,
72
99
canRotate: true ,
73
100
canMove: false ,
74
101
canScale: false ,
75
102
customProgressIndicator: const CupertinoActivityIndicator (),
76
103
),
77
104
),
78
105
Row (
106
+ mainAxisAlignment: MainAxisAlignment .spaceAround,
79
107
children: [
80
108
IconButton (
81
109
icon: const Icon (Icons .refresh), onPressed: controller.reset),
@@ -106,11 +134,61 @@ class _MyHomePageState extends State<MyHomePage> {
106
134
}
107
135
},
108
136
),
137
+ PopupMenuButton <CustomCropShape >(
138
+ icon: const Icon (Icons .crop_original),
139
+ onSelected: _changeCropShape,
140
+ itemBuilder: (BuildContext context) {
141
+ return CustomCropShape .values.map ((shape) {
142
+ return PopupMenuItem <CustomCropShape >(
143
+ value: shape,
144
+ child: getShapeIcon (shape),
145
+ );
146
+ }).toList ();
147
+ },
148
+ )
109
149
],
110
150
),
151
+ if (_currentShape == CustomCropShape .Ratio )
152
+ SizedBox (
153
+ child: Row (
154
+ children: [
155
+ Expanded (
156
+ child: TextField (
157
+ controller: _widthController,
158
+ keyboardType: TextInputType .number,
159
+ decoration: const InputDecoration (labelText: 'Width' ),
160
+ ),
161
+ ),
162
+ const SizedBox (width: 16.0 ),
163
+ Expanded (
164
+ child: TextField (
165
+ controller: _heightController,
166
+ keyboardType: TextInputType .number,
167
+ decoration: const InputDecoration (labelText: 'Height' ),
168
+ ),
169
+ ),
170
+ const SizedBox (width: 16.0 ),
171
+ ElevatedButton (
172
+ onPressed: _updateRatio,
173
+ child: const Text ('Update Ratio' ),
174
+ ),
175
+ ],
176
+ ),
177
+ ),
111
178
SizedBox (height: MediaQuery .of (context).padding.bottom),
112
179
],
113
180
),
114
181
);
115
182
}
183
+
184
+ Widget getShapeIcon (CustomCropShape shape) {
185
+ switch (shape) {
186
+ case CustomCropShape .Circle :
187
+ return const Icon (Icons .circle_outlined);
188
+ case CustomCropShape .Square :
189
+ return const Icon (Icons .square_outlined);
190
+ case CustomCropShape .Ratio :
191
+ return const Icon (Icons .crop_16_9_outlined);
192
+ }
193
+ }
116
194
}
0 commit comments