android - 在 Flutter 中无法使用 BoxDecoration 图像
问题描述
我是flutter dart应用程序的新手,我只想在显示图像时将这组代码更改circleAvatar
为。square type
如果有人知道问题所在,这对我很有帮助。
下面是代码,CircleAvatar
我想将显示图像更改为正方形而不是圆形。
File _pickedImage;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Profile'),
),
body: ListView(
children: <Widget>[
Center(
child: CircleAvatar( //This is the cirtcle avatar want to change to square tyoe
radius: 10,
child: _pickedImage == null ? Text("Picture") : null,
backgroundImage:
_pickedImage != null ? FileImage(_pickedImage) : null,
),
),
const SizedBox(height: 10.0),
RaisedButton(
child: Text("Pick Image"),
onPressed: () {
_showPickOptionsDialog(context);
},
)
],
),
);
}
这是我尝试过的,我在 Container 下添加了 BoxDecorator 但这会导致我出错。Failed assertion: line 855 pos 14: 'file != null': is not true.
File _pickedImage;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Profile'),
),
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: _pickedImage != null ? FileImage(_pickedImage) : null,
fit: BoxFit.cover)),
child: Column(children: <Widget>[
RaisedButton(
child: Text("Pick Image"),
onPressed: () {
_showPickOptionsDialog(context);
},
)
],
),
),
);
}
解决方案
删除circleAvatar
并Container
改用
Widget _imageWidget() {
if (_pickedImage != null) {
return Container(
height: 50,
width: 50,
child: Text("Picture"),
decoration: BoxDecoration(
image: DecorationImage(
image: FileImage(_pickedImage),
),
),
);
} else {
return SizedBox();
}
}
推荐阅读
- php - 似乎无法从 https 重定向到在 conf 文件中工作的 http
- r - 当使用 apt / 从源代码编译安装时,R 处理某些字符的方式不同
- javascript - JavaScript:从下拉菜单中检索所选值的最佳方法是什么?
- bash - BASH 将变量传递给 grep 产生不同的结果
- css - angular 7 指令中的 nativeElement.value 不正确
- maven - 码头没有在任何端口上启动
- javascript - 如何阻止 Hammer js 事件从内部元素触发?
- sql-server - Foxpro 之间的条款
- tensorflow - 将我自己的 Keras CNN 模型转换为 TensorFlow Lite。TensorFlow Lite Demo App 中的检测结果错误
- python-3.x - Pandas 外部合并列序列不固定