在Flutter中容器内的空白空间

huangapple go评论53阅读模式
英文:

Empty space in container in flutter

问题

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.deepPurple,
        body: Container(
          decoration: const BoxDecoration(
            gradient: LinearGradient(
                colors: [Colors.deepPurple, Colors.purple],
                begin: Alignment.topLeft,
                end: Alignment.bottomRight),
          ),
          child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              children: [
                Image.asset(
                  'assets/images/quiz-logo.png',
                  height: 400,
                  fit: BoxFit.fill,
                ),
                const Text(
                  "Learn Flutter the fun way!",
                  style: TextStyle(fontSize: 30, color: Colors.white),
                ),
              ]),
        ),
      ),
    ),
  );
}
英文:
import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.deepPurple,
        body: Container(
          decoration: const BoxDecoration(
            gradient: LinearGradient(
                colors: [Colors.deepPurple, Colors.purple],
                begin: Alignment.topLeft,
                end: Alignment.bottomRight),
          ),
          child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              children: [
                Image.asset(
                  'assets/images/quiz-logo.png',
                  height: 400,
                  fit: BoxFit.fill,
                ),
                const Text(
                  "Learn Flutter the fun way!",
                  style: TextStyle(fontSize: 30, color: Colors.white),
                ),
              ]),
        ),
      ),
    ),
  );
}

在Flutter中容器内的空白空间

I was trying to add an image to the screen in flutter. However when i change the height of image below 500, i get an empty space on the right side with a different color.

答案1

得分: 2

当您将图像高度更改为400时,它已被调整大小以保持比例,因此其宽度也在减小。

使您的主容器具有全宽度,并使其内容居中。

Container(
	alignment: Alignment.center,
	width: double.infinity,
	child: ...
)

还可以帮助将您的列交叉轴居中。

Column(
    crossAxisAlignment: CrossAxisAlignment.center,
    children: ...
)
英文:

When you change your image to the height of 400, it's been resized to maintain proportion, so its width is also being reduced.

Make your main container full width, and it's contents centered.

Container(
	alignment: Alignment.center,
	width: double.infinity,
	child: ...
)

May also help putting your Column cross axis centered.

Column(
    crossAxisAlignment: CrossAxisAlignment.center,
    children: ...
)

答案2

得分: 1

Flutter布局黄金规则
> 约束向下传递。尺寸向上增加。父级设置位置。

有几种方法可以获得全宽度,如Dainel Possamai所包括的width: double.infinity在容器上将解决这个问题。

还可以这样做

Column(
 crossAxisAlignment: CrossAxisAlignment.stretch,

或者

body: Container(
  constraints: BoxConstraints.expand(),

了解更多关于布局/约束的信息。

英文:

Flutter layout golden rules
>Constraints go down. Sizes go up. Parent sets position.

There are couple of ways to get full width as Dainel Possamai included width: double.infinity on container will solve the issue.

Also you can do

Column(
 crossAxisAlignment: CrossAxisAlignment.stretch,

Or

body: Container(
  constraints: BoxConstraints.expand(),

Find more about layout/constraints.

huangapple
  • 本文由 发表于 2023年6月19日 22:43:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定