Created
July 10, 2023 03:13
-
-
Save CoderNamedHendrick/0d8fef2f360ffeb32e1914b3f59d5fe9 to your computer and use it in GitHub Desktop.
image picker initial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'package:flutter/material.dart'; | |
import 'package:image_picker/image_picker.dart'; | |
class ImagePickerField extends StatefulWidget { | |
const ImagePickerField({Key? key}) : super(key: key); | |
@override | |
State<ImagePickerField> createState() => _ImagePickerFieldState(); | |
} | |
class _ImagePickerFieldState extends State<ImagePickerField> { | |
File? imageFile; | |
@override | |
Widget build(BuildContext context) { | |
return InkResponse( | |
onTap: () async { | |
final image = | |
await ImagePicker().pickImage(source: ImageSource.gallery); | |
if (image == null) return; | |
setState(() { | |
imageFile = File(image.path); | |
}); | |
}, | |
child: Container( | |
height: 100, | |
width: double.infinity, | |
decoration: ShapeDecoration( | |
color: Theme.of(context).colorScheme.secondary, | |
shape: const RoundedRectangleBorder( | |
borderRadius: BorderRadius.all(Radius.circular(12)), | |
), | |
), | |
child: imageFile == null | |
? const Row( | |
crossAxisAlignment: CrossAxisAlignment.center, | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Icon(Icons.add_circle_outline_outlined, color: Colors.white), | |
SizedBox(width: 8), | |
Text('Pick image', style: TextStyle(color: Colors.white)), | |
], | |
) | |
: Image.file(imageFile!), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment