flutter - 将图像插入容器 Flutter 应用程序
问题描述
我正在查看我在 startflutter.com 上找到的这个模板,完整的代码如下所示
我尝试将自己的图像插入圆圈,似乎没有办法让图像完全进入盒子(它总是被裁剪)
@override
Widget build(BuildContext context) {
final alucard = Hero(
tag: 'hero',
child: Padding(
padding: EdgeInsets.all(16.0),
child: CircleAvatar(
radius: 72.0,
backgroundColor: Colors.transparent,
backgroundImage: AssetImage('assets/alucard.jpg'),
),
),
);
我想在容器中插入图像,就像这样
@override
Widget build(BuildContext context) {
final alucard = Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage("images/logo.png"),
fit: BoxFit.fill,
)
)
);
但这不起作用并且不会出现,这有什么问题?
这是整个页面的代码......
import 'package:flutter/material.dart';
class HomePage extends StatelessWidget {
static String tag = 'home-page';
@override
Widget build(BuildContext context) {
final alucard = Hero(
tag: 'hero',
child: Padding(
padding: EdgeInsets.all(16.0),
child: CircleAvatar(
radius: 72.0,
backgroundColor: Colors.transparent,
backgroundImage: AssetImage('assets/alucard.jpg'),
),
),
);
final welcome = Padding(
padding: EdgeInsets.all(8.0),
child: Text(
'Welcome Alucard',
style: TextStyle(fontSize: 28.0, color: Colors.white),
),
);
final lorem = Padding(
padding: EdgeInsets.all(8.0),
child: Text(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec hendrerit condimentum mauris id ',
style: TextStyle(fontSize: 16.0, color: Colors.white),
),
);
final body = Container(
width: MediaQuery.of(context).size.width,
padding: EdgeInsets.all(28.0),
decoration: BoxDecoration(
gradient: LinearGradient(colors: [
Colors.blue,
Colors.lightBlueAccent,
]),
),
child: Column(
children: <Widget>[alucard, welcome, lorem],
),
);
return Scaffold(
body: body,
);
}
}
解决方案
用这个改变你的容器会很好
Container(
height: 120.0,
width: 120.0,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/assets/alucard.jpg'),
fit: BoxFit.fill,
),
shape: BoxShape.circle,
),
)
推荐阅读
- python - 在python中将列表中的条目格式化为8位二进制
- java - MacOS 上带有 JavaFX 的状态菜单
- regex - 使用正则表达式匹配带有破折号的单词
- r - 为什么 ddply 在更改函数顺序时会给出 NA 值?
- c - 在 C 中正确打印二叉树
- codeigniter - Codeigniter,将参数传递给语言文件
- python - 如何使用 execute_script 在新选项卡中提交表单
- cmake - 如何在现代 CMake 中覆盖优化级别标志
- java - Retrofit 2 请求返回空对象引用
- ruby - Facebook通过Graph API上传视频字幕不起作用