首页 > 解决方案 > 在 Flutter 中将图像与左上角对齐

问题描述

我对颤动很陌生,不知道如何对齐我的一个包含图像的子小部件。这是我到目前为止所尝试的:

@override
Widget build(BuildContext context) {
  return Scaffold(
  appBar: AppBar(
    title: Text(widget.username),
    backgroundColor: new Color(0xFF24292E),
  ),
  body: Center(
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.max,
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        Image.asset(
          'assets/profile.png', width: 100.0, height: 100.0,
        ),
      ],
    ),
  ),
);
}

然而,图像停留在屏幕的中央顶部。如何将其相对于屏幕移动到左上角?谢谢。

标签: dartflutterflutter-layout

解决方案


您已使列居中,这就是为什么其中的所有内容都居中的原因。只需删除顶级 Center() 小部件,您的图像就会左上角对齐。

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text(widget.username),
      backgroundColor: new Color(0xFF24292E),
    ),
    body: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.max,
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        Image.asset(
          'assets/profile.png', width: 100.0, height: 100.0,
        ),
      ],
    ),
  );
}

推荐阅读