Flutter Can';t使用“对齐”小部件和“对齐”属性对齐行的子级
我在颤振中使用Flutter Can';t使用“对齐”小部件和“对齐”属性对齐行的子级,flutter,dart,flutter-layout,Flutter,Dart,Flutter Layout,我在颤振中使用对齐时遇到了一些困难。我正在尝试制作一张一次性卡片。 我不能让图像居中,图标放在右上角 小部件说明 卡由列组成,每个列都有一行 x图标是第一行的第一个子图标 我用Align小部件包装了图标,但它不会向右移动: Row(children: [ Align( alignment: Alignment.topRight, child: Icon(Icons.cancel),) ] ) 对于owl图像,我将其包装在容器中,并使用对齐属性放置在中
对齐时遇到了一些困难。我正在尝试制作一张一次性卡片。
我不能让图像居中,图标放在右上角
小部件说明
卡由列
组成,每个列
都有一行
x
图标是第一行的第一个子图标
我用Align
小部件包装了图标,但它不会向右移动:
Row(children: [
Align(
alignment: Alignment.topRight,
child: Icon(Icons.cancel),)
]
)
对于owl图像,我将其包装在容器中
,并使用对齐
属性放置在中心
:
Row(children: [
Container(
width: 100,
height: 150,
alignment: Alignment.center,
child: Image(
alignment: Alignment.center,
image: NetworkImage('https://flutter.github.io/assets-for-api-docs/assets/widgets/owl.jpg'),
)
),
]),
似乎无论我在布局上尝试什么,它都不会移动卡中的对象
以下是卡
小部件:
class CustomCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(children: [
Align(
alignment: Alignment.topRight,
child: Icon(Icons.cancel) ,
)
]
),
SizedBox(
height: 8,
),
Row(children: [
Container(
width: 100,
height: 150,
alignment: Alignment.center,
child: Image(
alignment: Alignment.center,
image: NetworkImage(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl.jpg'),
))
]),
],
));
}
}
无论我怎么做,我都无法正确放置它们,我是否完全错过了对齐小部件和对齐属性的概念?您需要做的就是将MainAxisAlignment设置为行的中心。请运行下面的代码
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: CustomCard(),
),
);
}
}
class CustomCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Align(
alignment: Alignment.topRight,
child: Icon(Icons.cancel),
),
SizedBox(
height: 8,
),
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Container(
width: 100,
height: 150,
child: Image(
image: NetworkImage(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl.jpg'),
))
]),
],
));
}
}
只需将MainAxisAlignment设置为行的中心。请运行下面的代码
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: CustomCard(),
),
);
}
}
class CustomCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Align(
alignment: Alignment.topRight,
child: Icon(Icons.cancel),
),
SizedBox(
height: 8,
),
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Container(
width: 100,
height: 150,
child: Image(
image: NetworkImage(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl.jpg'),
))
]),
],
));
}
}
谢谢你的回答,这困扰了我很长时间。您还可以从图标
中删除行
元素,这是我没有想到的。你的例子很好用。非常感谢。谢谢你的回答,这困扰了我很长时间。您还可以从图标
中删除行
元素,这是我没有想到的。你的例子很好用。非常感谢。