Menu
CoddyTech

内边距

本课属于 Coddy Flutter 学习旅程的“你的第一个应用”单元。第 17 课,共 32 课。

紧贴文字的盒子看起来很拥挤。padding: 会在盒子内部添加空白,位于盒子边缘和子项之间:

Container(
  padding: EdgeInsets.all(16),
  color: Colors.yellow,
  child: Text('Room to breathe'),
)

EdgeInsets.all(16) 表示四边各有 16 像素的空白。EdgeInsets.symmetric(horizontal: 24, vertical: 8) 分别设置两侧以及顶部和底部的空白。

Container 还有 margin:。它是盒子外部的空白,位于盒子和相邻元素之间。着色部分不会变大。

challenge icon

挑战

简单

给 Fresh 留出一些空间:在绿色方框内的四周各添加 24 像素的内边距。

亲手试试

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: Container(
            color: Colors.lightGreen,
            child: Text('Fresh', style: TextStyle(fontSize: 32)),
          ),
        ),
      ),
    );
  }
}
quiz icon自我测试

这节课包含一个小测验。开始学习即可作答,并记录你的进度。

你的第一个应用中的所有课

自己练习: Flutter 在线编译器