Reputation: 2286
I am using the package Image_picker. The objective is to attach an image in the screen from the Library. I got to access to the image library, but for some reason is not retrieving the image in the screen after selecting it.
This is the code:
File image;
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
AppBarTransparentComponent(centerTitle: true, title: 'New Message', showBack: true, backgroundColor: TheBaseColors.lightBlue),
];
},
body: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.max,
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
controller: _subjectController,
decoration: InputDecoration(
border: OutlineInputBorder(),
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
controller: _bodyController,
maxLines: null,
expands: true,
textAlignVertical: TextAlignVertical.top,
decoration: InputDecoration(labelText: 'Body', border: OutlineInputBorder()),
),
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: Wrap(
children: <Widget>[
Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
child: image != null
? ClipRRect(
borderRadius: BorderRadius.circular(50),
child: Image.file(
image,
width: 100,
height: 100,
fit: BoxFit.fitHeight,
),
)
: Container(
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(50)),
width: 100,
height: 100,
child: Icon(
Icons.camera_alt,
color: Colors.grey[800],
),
),
),
FlatButton(
onPressed: () =>
{ imgFromGallery()
},
child: Row(
// Replace with a Row for horizontal icon + text
children: <Widget>[
Text('Attach files'),
IconButton(
icon: Icon(Icons.attach_file),
onPressed: () {
imgFromGallery();
},
),
],
),
),
FlatButton(
onPressed: () => {},
color: TheBaseColors.lightGreen,
padding: EdgeInsets.all(10.0),
child: Text('Send'),
),
],
),
],
),
],
),
),
],
),
),
),
);
}
Future imgFromGallery() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery, imageQuality: 25, maxWidth: 1920, maxHeight: 1080);
setState(() {
image = image;
});
}
}
How can I make the image selected visible in the screen? I am setting up the state with the selected image and then returning it in a container. What am I missing?
Upvotes: 0
Views: 128
Reputation: 5470
File image;
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
AppBarTransparentComponent(centerTitle: true, title: 'New Message', showBack: true, backgroundColor: TheBaseColors.lightBlue),
];
},
body: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.max,
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
controller: _subjectController,
decoration: InputDecoration(
border: OutlineInputBorder(),
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
controller: _bodyController,
maxLines: null,
expands: true,
textAlignVertical: TextAlignVertical.top,
decoration: InputDecoration(labelText: 'Body', border: OutlineInputBorder()),
),
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: Wrap(
children: <Widget>[
Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
child: image != null
? ClipRRect(
borderRadius: BorderRadius.circular(50),
child: Image.file(
image,
width: 100,
height: 100,
fit: BoxFit.fitHeight,
),
)
: Container(
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(50)),
width: 100,
height: 100,
child: Icon(
Icons.camera_alt,
color: Colors.grey[800],
),
),
),
FlatButton(
onPressed: () =>
{ imgFromGallery()
},
child: Row(
// Replace with a Row for horizontal icon + text
children: <Widget>[
Text('Attach files'),
IconButton(
icon: Icon(Icons.attach_file),
onPressed: () {
imgFromGallery();
},
),
],
),
),
FlatButton(
onPressed: () => {},
color: TheBaseColors.lightGreen,
padding: EdgeInsets.all(10.0),
child: Text('Send'),
),
],
),
],
),
],
),
),
],
),
),
),
);
}
Future imgFromGallery() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery, imageQuality: 25, maxWidth: 1920, maxHeight: 1080);
setState(() {
this.image = image;
});
}
}
Upvotes: 1