@@ -41,6 +41,7 @@ class MyHomePage extends StatefulWidget {
41
41
class _MyHomePageState extends State <MyHomePage > {
42
42
late CustomImageCropController controller;
43
43
CustomCropShape _currentShape = CustomCropShape .Circle ;
44
+ CustomImageFit _imageFit = CustomImageFit .fillCropSpace;
44
45
final TextEditingController _widthController = TextEditingController ();
45
46
final TextEditingController _heightController = TextEditingController ();
46
47
final TextEditingController _radiusController = TextEditingController ();
@@ -67,6 +68,12 @@ class _MyHomePageState extends State<MyHomePage> {
67
68
});
68
69
}
69
70
71
+ void _changeImageFit (CustomImageFit imageFit) {
72
+ setState (() {
73
+ _imageFit = imageFit;
74
+ });
75
+ }
76
+
70
77
void _updateRatio () {
71
78
setState (() {
72
79
if (_widthController.text.isNotEmpty) {
@@ -94,76 +101,74 @@ class _MyHomePageState extends State<MyHomePage> {
94
101
Expanded (
95
102
child: CustomImageCrop (
96
103
cropController: controller,
97
- // image: const AssetImage('assets/test.png'), // Any Imageprovider will work, try with a NetworkImage for example...
98
- image: const NetworkImage (
99
- 'https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png' ),
104
+ image: const AssetImage ('assets/test.png' ), // Any Imageprovider will work, try with a NetworkImage for example...
105
+ // image: const NetworkImage('https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png'),
100
106
shape: _currentShape,
101
- ratio: _currentShape == CustomCropShape .Ratio
102
- ? Ratio (width: _width, height: _height)
103
- : null ,
107
+ ratio: _currentShape == CustomCropShape .Ratio ? Ratio (width: _width, height: _height) : null ,
104
108
canRotate: true ,
105
- canMove: false ,
106
- canScale: false ,
107
- borderRadius:
108
- _currentShape == CustomCropShape .Ratio ? _radius : 0 ,
109
+ canMove: true ,
110
+ canScale: true ,
111
+ borderRadius: _currentShape == CustomCropShape .Ratio ? _radius : 0 ,
109
112
customProgressIndicator: const CupertinoActivityIndicator (),
110
- // use custom paint if needed
111
- // pathPaint: Paint()
112
- // ..color = Colors.red
113
- // ..strokeWidth = 4.0
114
- // ..style = PaintingStyle.stroke
115
- // ..strokeJoin = StrokeJoin.round,
113
+ imageFit : _imageFit,
114
+ pathPaint: Paint ()
115
+ ..color = Colors .red
116
+ ..strokeWidth = 4.0
117
+ ..style = PaintingStyle .stroke
118
+ ..strokeJoin = StrokeJoin .round,
116
119
),
117
120
),
118
121
Row (
119
122
mainAxisAlignment: MainAxisAlignment .spaceAround,
120
123
children: [
121
- IconButton (
122
- icon: const Icon (Icons .refresh), onPressed: controller.reset),
123
- IconButton (
124
- icon: const Icon (Icons .zoom_in),
125
- onPressed: () =>
126
- controller.addTransition (CropImageData (scale: 1.33 ))),
127
- IconButton (
128
- icon: const Icon (Icons .zoom_out),
129
- onPressed: () =>
130
- controller.addTransition (CropImageData (scale: 0.75 ))),
131
- IconButton (
132
- icon: const Icon (Icons .rotate_left),
133
- onPressed: () =>
134
- controller.addTransition (CropImageData (angle: - pi / 4 ))),
135
- IconButton (
136
- icon: const Icon (Icons .rotate_right),
137
- onPressed: () =>
138
- controller.addTransition (CropImageData (angle: pi / 4 ))),
139
- IconButton (
140
- icon: const Icon (Icons .crop),
141
- onPressed: () async {
142
- final image = await controller.onCropImage ();
143
- if (image != null ) {
144
- Navigator .of (context).push (MaterialPageRoute (
145
- builder: (BuildContext context) =>
146
- ResultScreen (image: image)));
147
- }
148
- },
149
- ),
150
- PopupMenuButton <CustomCropShape >(
124
+ IconButton (icon: const Icon (Icons .refresh), onPressed: controller.reset),
125
+ IconButton (icon: const Icon (Icons .zoom_in), onPressed: () => controller.addTransition (CropImageData (scale: 1.33 ))),
126
+ IconButton (icon: const Icon (Icons .zoom_out), onPressed: () => controller.addTransition (CropImageData (scale: 0.75 ))),
127
+ IconButton (icon: const Icon (Icons .rotate_left), onPressed: () => controller.addTransition (CropImageData (angle: - pi / 4 ))),
128
+ IconButton (icon: const Icon (Icons .rotate_right), onPressed: () => controller.addTransition (CropImageData (angle: pi / 4 ))),
129
+ PopupMenuButton (
151
130
icon: const Icon (Icons .crop_original),
152
131
onSelected: _changeCropShape,
153
132
itemBuilder: (BuildContext context) {
154
133
return CustomCropShape .values.map (
155
134
(shape) {
156
- return PopupMenuItem < CustomCropShape > (
135
+ return PopupMenuItem (
157
136
value: shape,
158
137
child: getShapeIcon (shape),
159
138
);
160
139
},
161
140
).toList ();
162
141
},
163
142
),
143
+ PopupMenuButton (
144
+ icon: const Icon (Icons .fit_screen),
145
+ onSelected: _changeImageFit,
146
+ itemBuilder: (BuildContext context) {
147
+ return CustomImageFit .values.map (
148
+ (imageFit) {
149
+ return PopupMenuItem (
150
+ value: imageFit,
151
+ child: Text (imageFit.name),
152
+ );
153
+ },
154
+ ).toList ();
155
+ },
156
+ ),
157
+ IconButton (
158
+ icon: const Icon (
159
+ Icons .crop,
160
+ color: Colors .green,
161
+ ),
162
+ onPressed: () async {
163
+ final image = await controller.onCropImage ();
164
+ if (image != null ) {
165
+ Navigator .of (context).push (MaterialPageRoute (builder: (BuildContext context) => ResultScreen (image: image)));
166
+ }
167
+ },
168
+ ),
164
169
],
165
170
),
166
- if (_currentShape == CustomCropShape .Ratio )
171
+ if (_currentShape == CustomCropShape .Ratio ) ...[
167
172
SizedBox (
168
173
child: Row (
169
174
children: [
@@ -198,6 +203,7 @@ class _MyHomePageState extends State<MyHomePage> {
198
203
],
199
204
),
200
205
),
206
+ ],
201
207
SizedBox (height: MediaQuery .of (context).padding.bottom),
202
208
],
203
209
),
0 commit comments