Controller
class ImageController extends GetxController {
final ImagePicker imagePicker = ImagePicker();
final pickedImage = Rx<File?>(null);
final imagefiles = RxList<File>([]);
Future<void> pickMultiImage() async {
try {
var pickedfile = await imagePicker.pickImage(source: ImageSource.camera);
//you can use ImageCourse.camera for Camera capture
if (pickedfile != null) {
pickedImage.value = File(pickedfile.path);
imagefiles.add(pickedImage.value!);
}
} catch (e) {
print('error while picking file.');
}
}
}
Page
Image shows in Gridview
class TestPage extends StatelessWidget {
final imageController = Get.put(ImageController());
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: KAppbar(),
body: Obx(
() => SingleChildScrollView(
child: Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 15),
child: Row(
children: [
SizedBox(
width: 5,
),
IconButton(
onPressed: () {
imageController.pickMultiImage();
},
icon: Icon(Icons.add_a_photo),
iconSize: 40,
),
Text(
'Attachments',
),
],
),
),
SizedBox(
height: 10,
),
imageController.imagefiles.isEmpty
? GridView.builder(
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 15,
),
itemCount: 1,
primary: false,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return Container(
// height: 130,
width: double.infinity,
color: Colors.amber,
child: Center(
child: IconButton(
onPressed: () => imageController.pickMultiImage(),
icon: Icon(
Icons.add,
color: Colors.grey,
size: 15,
),
),
),
//background color of inner container
);
},
)
: GridView.builder(
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemCount: imageController.imagefiles.length,
primary: false,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
final item = imageController.imagefiles[index];
return Stack(
children: [
Container(
width: double.infinity,
margin: EdgeInsets.all(5),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(5),
child: Image.file(
File(item.path),
fit: BoxFit.cover,
),
),
),
Positioned(
top: 0,
right: 0,
left: 0,
bottom: 0,
child: InkWell(
onTap: () {
//if you want to remove image
imageController.imagefiles.removeAt(index);
},
child: Container(
margin: EdgeInsets.all(60),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
color: Colors.white.withOpacity(0.5),
),
child: Icon(
Icons.delete,
color: Colors.redAccent,
size: 30,
),
),
),
),
],
);
},
),
],
)),
),
);
}
}