SizedBox和Padding..它们有任何区别吗?

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

SizedBox and Padding.. Does it make any difference?

问题

我想在文本“NAME”和“Thashreef”之间留出空间。所以我使用了padding: EdgeInsets.only(top:10),在视频教程中是SizedBox(height:10)。这两个函数都一样吗?
英文:

I wanted to leave a space between the texts 'NAME' and 'Thashreef'. So i used padding: EdgeInsets.only(top:10) and in the video tutorial it was SizedBox(height:10). Are both these functions same?


void main()=> runApp(MaterialApp(
  home: FirstPage()
));

class FirstPage extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      backgroundColor: Colors.grey[900],
      appBar: AppBar(
        title: Text('Ninja ID Card'),
        centerTitle: true,
        backgroundColor: Colors.grey[850],
        elevation: 0.0,
      ),
      body: Padding(
        padding: EdgeInsets.fromLTRB(20,30,40,50),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text('NAME',style: TextStyle(
              color:Colors.grey,
              letterSpacing: 2.0),),
            Padding(
              padding: const EdgeInsets.only(top:10),
              child: Text('Thashreef',
                style: TextStyle(
                    color: Colors.yellowAccent,
                    letterSpacing:2.0,fontSize: 28.0,fontWeight: FontWeight.bold)),
            )

          ],
        ),
      ),
    );
  }
}```

</details>


# 答案1
**得分**: 6

`Padding` 和 `SizedBox` `Widgets` 不同。

[Padding](https://api.flutter.dev/flutter/widgets/Padding-class.html) 用于在 `Widget` 周围或特定边缘周围添加空间。

[SizedBox](https://api.flutter.dev/flutter/widgets/SizedBox-class.html) 是一个 `Widget`,不需要具有子项,只需设置高度或宽度即可。这意味着它可以用作包含多个子项的 `Row` 或 `Column` 等 `Widgets` 内部的简单间隔器。这很可能是您所遵循的教程的情况。

<details>
<summary>英文:</summary>

`Padding` and `SizedBox` `Widgets` are not the same. 

[Padding](https://api.flutter.dev/flutter/widgets/Padding-class.html) serves to surround a `Widget` with space all around it or on specific sides. 

[SizedBox](https://api.flutter.dev/flutter/widgets/SizedBox-class.html) is a `Widget` that doesn&#39;t require to have a child and can be set with just a height or width. This means it can be used as a simple spacer inside `Widgets` that contain multiple children like `Row` or `Column`. As was likely the case of the tutorial you followed.

</details>



huangapple
  • 本文由 发表于 2020年1月3日 22:56:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580726.html
匿名

发表评论

匿名网友

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

确定