我是Flutter的新手,我喜欢它,但是我对建筑物的布局并不满意。
我正在编写一个包含Cards ListView的应用程序。 每张卡都在一个容器内,并包含一张图像(高度和宽度固定)和一个文本。
我无法正确处理卡内的图像。我希望图像能覆盖盒子的宽度。 谢谢。
这是代码:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final title = 'MyApp';
return MaterialApp(
title: title,
home: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: ListView(
children: <Widget>[
Container(
margin:EdgeInsets.all(8.0),
child: Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
child: InkWell(
onTap: () => print("ciao"),
child: Column(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8.0),
topRight: Radius.circular(8.0),
),
child: Image.asset(
'img/britannia.jpg',
width: 300,
height: 150,
fit:BoxFit.fill
),
),
ListTile(
title: Text('Pub 1'),
subtitle: Text('Location 1'),
),
],
),
),
),
),
],
),
),
);
}
}
答案 0 :(得分:14)
这对我有用
Image.network(imageUrl, fit: BoxFit.fitWidth,),
答案 1 :(得分:9)
我不知道如何。但这确实可以将固定大小的图像保存在容器中 只需在容器中添加对齐方式
Container(
height: double.infinity,
alignment: Alignment.center, // This is needed
child: Image.asset(
Constants.ASSETS_IMAGES + "logo.png",
fit: BoxFit.contain,
width: 300,
),
);
答案 2 :(得分:4)
Image.asset(
'assets/images/desert.jpg',
height: 150,
width: MediaQuery.of(context).size.width,
fit:BoxFit.cover
)
答案 3 :(得分:3)
您需要在crossAxisAlignment: CrossAxisAlignment.stretch,
中添加-Column
,以便孩子可以占用水平空间。
工作代码:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final title = 'MyApp';
return MaterialApp(
title: title,
home: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: ListView(
children: <Widget>[
Container(
margin:EdgeInsets.all(8.0),
child: Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
child: InkWell(
onTap: () => print("ciao"),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch, // add this
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8.0),
topRight: Radius.circular(8.0),
),
child: Image.network(
'https://placeimg.com/640/480/any',
// width: 300,
height: 150,
fit:BoxFit.fill
),
),
ListTile(
title: Text('Pub 1'),
subtitle: Text('Location 1'),
),
],
),
),
),
),
Container(
margin:EdgeInsets.all(8.0),
child: Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
child: InkWell(
onTap: () => print("ciao"),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8.0),
topRight: Radius.circular(8.0),
),
child: Image.network(
'https://placeimg.com/640/480/any',
// width: 300,
height: 150,
fit:BoxFit.fill
),
),
ListTile(
title: Text('Pub 1'),
subtitle: Text('Location 1'),
),
],
),
),
),
),
Container(
margin:EdgeInsets.all(8.0),
child: Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
child: InkWell(
onTap: () => print("ciao"),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8.0),
topRight: Radius.circular(8.0),
),
child: Image.network(
'https://placeimg.com/640/480/any',
// width: 300,
height: 150,
fit:BoxFit.fill
),
),
ListTile(
title: Text('Pub 1'),
subtitle: Text('Location 1'),
),
],
),
),
),
),
],
),
),
);
}
}
输出:
答案 4 :(得分:2)
将图像小部件放在容器内,并为容器提供对齐中心,并为图像提供特定的宽高。
return Container(
alignment: Alignment.center,// use aligment
color: Color.fromRGBO(0, 96, 91, 1.0),
child: Image.asset('assets/images/splash_logo.png',
height: 150,
width: 150,
fit: BoxFit.cover),
);
答案 5 :(得分:1)
我用了这个并且与我一起工作得很好!
Container(
width: MediaQuery.of(context).size.width,
height: 175,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(YOUTUBE_THUMBNAIL_PART_ONE +
video.key +
YOUTUBE_THUMBNAIL_PART_TWO),
),
)),
答案 6 :(得分:1)
您想要做的可能是使用较大容器的大小。 在这种情况下,您的媒体应该占据整个专用空间:
return Container(
alignment: Alignment.center,
height: double.infinity,
width: double.infinity,
child: Image.asset(
'', //TODO fill path
height: double.infinity,
width: double.infinity,
fit: BoxFit.cover,
),
);
您可以使用fit
值:
BoxFit.cover
将覆盖整个容器BoxFit.fitWidth
将适合宽度,并最终放置水平白条以填充空间BoxFit.fitHeight
将适合高度,并最终放置垂直白条以填充空间答案 7 :(得分:0)
如果可以使用fit属性,那么我将这个非常清晰的备忘单(章节fit属性)详细说明所有内容:https://medium.com/jlouage/flutter-boxdecoration-cheat-sheet-72cedaa1ba20
答案 8 :(得分:0)
我遇到了同样的问题,我只是在使用MediaQuery类获取屏幕宽度后,在Image.asset类中添加了fit:BoxFit.fill **
Container(
child:Image.asset(link,fit: BoxFit.fill,)
width: screensize.width,
height: 150,
))
答案 9 :(得分:0)
您只需要 fit
类的 Image
属性:
Image.asset(
'your_image_asset',
fit: BoxFit.fill, // Expands to fill parent (changes aspect ratio)
)
或
Image.asset(
'your_image_asset',
fit: BoxFit.cover, // Zooms the image (maintains aspect ratio)
)
答案 10 :(得分:0)
Center(
child: Container(
margin: EdgeInsets.only(top: 75),
width: 120,
height: 120,
alignment: Alignment.center,
child: Image.asset(
"assets/images/call.png",
fit: BoxFit.cover,
height: 45,
width: 45,
),
decoration: new BoxDecoration(
color: Colors.white,
borderRadius: new BorderRadius.all(new Radius.circular(120)),
border: new Border.all(
color: Colors.blue,
width: 4.0,
),
),
),
)
使用您想要的边框
# training loop
w0 = -1
w1 = 1
b = 1
a = 0.5
epochs = 50000
for i in range(epochs):
# loop over all the points to calculate the gradient
dw0 = 0
dw1 = 0
db = 0
for point in data:
x0 = point[0]
x1 = point[1]
y = point[-1]
z = (w0 * x0) + (w1 * x1) + b
# print(z)
a = 1/(1 + np.exp(-z)) # sigmoid
# print(a)
dz = a - y # dz = dL/dz (derivative of loss function wrt z)
dw0 += dz * x0
dw1 += dz * x1
db += dz
dw0 /= len(data) # av. val over all data points
dw1 /= len(data)
db /= len(data)
# gradient descent step
w0 = w0 - (a * dw0)
w1 = w1 - (a * dw1)
b = b - (a * db)
print(w0, w1, b)
print()