File profileImg;
Widget profilePic() {
return Stack(
children: <Widget>[
new Image.file(profileImg),
Positioned(
left: 50.0,
right: 50.0,
bottom: 40.0,
height: 64.0,
child: RaisedGradientButton(
onPressed: () async {
File image= await ImagePicker.pickImage(source:ImageSource.gallery);
image=profileImg;
print(image.path);
},
child: new Text(
"Upload",
style: TextStyle(fontSize: 20.0, color: Colors.white),
),
gradient: LinearGradient(
colors: <Color>[
const Color(0xFF000000),
const Color(0xFF000000),
const Color(0xFF40079B)
],
),
), // child widget
),
]);
}
Run Code Online (Sandbox Code Playgroud)
我已经创建了一张图片,在图片的底部,我有一个按钮可以从图库中选择图片。在从图库中选择图片之前,我想在从图库中选择图片后为图片设置背景颜色,我想设置所选图片图像视图中的图像
使用Image_picker&FutureBuilder显示所选图库图像的最小示例。
Future<File> profileImg;
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text("Gallery Image Picker"),
),
body: profilePic());
}
Widget profilePic() {
return ListView(children: <Widget>[
FutureBuilder(
builder: (context, data) {
if (data.hasData) {
return Container(
height: 200.0,
child: Image.file(
data.data,
fit: BoxFit.contain,
height: 200.0,
),
color: Colors.blue,
);
}
return Container(
height: 200.0,
child: Image.network('https://via.placeholder.com/150'),
color: Colors.blue,
);
},
future: profileImg,
),
RaisedButton(
color: Colors.blue,
onPressed: () {
profileImg = ImagePicker.pickImage(source: ImageSource.gallery)
.whenComplete(() {
setState(() {});
});
},
child: new Text(
"Pick Gallery Image",
style: TextStyle(fontSize: 20.0, color: Colors.white),
),
),
]);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4530 次 |
| 最近记录: |