Skip to content

Flutter Widgets 介绍 (Introduction to Flutter Widgets)

Flutter 提供了丰富的 Widgets 来帮助开发者构建美观且功能强大的用户界面。Widgets 是 Flutter 应用的构建块,每个元素都是一个 Widget,无论是布局、按钮、文本、还是图像。以下是常见 Flutter Widgets 的分类及其简要介绍。

Flutter offers a rich set of Widgets to help developers build beautiful and powerful user interfaces. Widgets are the building blocks of a Flutter application; every element is a Widget, whether it's a layout, button, text, or image. Below is a classification of common Flutter Widgets along with a brief introduction.

1. 布局 Widgets (Layout Widgets)

布局 Widgets 用于控制其他 Widgets 的排列和布局方式。它们是构建复杂界面的基础。

Layout Widgets are used to control the arrangement and layout of other Widgets. They are fundamental in building complex interfaces.

1.1 Container:

一个容器 Widget,用于在子 Widget 周围添加填充、边距、边框等修饰。
A container Widget used to add padding, margins, borders, etc., around a child Widget.

主要参数 (Key Parameters)

  • alignment: 控制子 Widget 在 Container 内的对齐方式。
    Controls the alignment of the child Widget within the Container.

  • padding: 在子 Widget 周围添加内边距。
    Adds padding around the child Widget.

  • margin: 为 Container 自身添加外边距。
    Adds margin around the Container itself.

  • decoration: 设置 Container 的背景、边框、阴影等装饰。
    Sets the decoration for the Container, such as background, border, shadow, etc.

  • constraints: 限制 Container 的最大或最小宽度和高度。
    Constrains the Container to a maximum or minimum width and height.

  • widthheight: 设置 Container 的固定宽度和高度。
    Sets the fixed width and height of the Container.

  • color: 设置 Container 的背景色。
    Sets the background color of the Container.

  • child: Container 中的子 Widget。
    The child Widget inside the Container.

示例代码 (Example Code)

Container(
  width: 200,
  height: 100,
  padding: EdgeInsets.all(16.0),
  margin: EdgeInsets.symmetric(horizontal: 20.0, vertical: 30.0),
  alignment: Alignment.center,
  decoration: BoxDecoration(
    color: Colors.blue,
    borderRadius: BorderRadius.circular(8.0),
    boxShadow: [
      BoxShadow(
        color: Colors.black26,
        blurRadius: 10.0,
        offset: Offset(0, 4),
      ),
    ],
  ),
  child: Text(
    'Hello, Flutter!',
    style: TextStyle(color: Colors.white),
  ),
)

1.2 Row:

水平方向排列子 Widgets 的布局。

A layout that arranges child Widgets horizontally.

  • Column: 垂直方向排列子 Widgets 的布局。
    A layout that arranges child Widgets vertically.

  • Stack: 在同一平面上堆叠子 Widgets。
    A layout that stacks child Widgets on top of each other.

  • Expanded: 在 Row、Column 或 Flex 中使用,扩展子 Widget 以填充可用空间。
    A Widget that expands a child Widget to fill the available space in a Row, Column, or Flex.

  • Padding: 为子 Widget 添加内边距。
    A Widget that adds padding around a child Widget.

2. 文本 Widgets (Text Widgets)

文本 Widgets 用于显示和输入文本内容。

Text Widgets are used to display and input text content.

  • Text: 用于显示一段简单的文本。
    A Widget used to display a simple piece of text.

  • RichText: 用于显示富文本,支持不同的文本样式。
    A Widget used to display rich text with different text styles.

  • TextField: 一个输入框 Widget,用户可以通过它输入文本。
    An input box Widget where users can enter text.

3. 按钮 Widgets (Button Widgets)

按钮 Widgets 用于用户与应用进行交互,触发操作或事件。

Button Widgets are used for user interaction with the application, triggering actions or events.

  • ElevatedButton: 带有阴影效果的按钮,适合强调操作。
    A button with a shadow effect, suitable for emphasizing actions.

  • TextButton: 一个没有阴影效果的文本按钮。
    A text button without a shadow effect.

  • IconButton: 一个包含图标的按钮。
    A button that contains an icon.

  • FloatingActionButton: 一个悬浮在界面上的圆形按钮,通常用于主要操作。
    A circular button that floats over the interface, typically used for primary actions.

4. 图像 Widgets (Image Widgets)

图像 Widgets 用于显示图片。

Image Widgets are used to display images.

  • Image: 一个通用的图像显示 Widget,支持多种图像来源。
    A general image display Widget that supports multiple image sources.

  • Image.asset: 从应用程序的资产中加载和显示图片。
    Loads and displays an image from the application's assets.

  • Image.network: 从网络中加载和显示图片。
    Loads and displays an image from the web.

5. 输入 Widgets (Input Widgets)

输入 Widgets 允许用户通过界面输入数据或做出选择。

Input Widgets allow users to input data or make selections through the interface.

  • TextField: 允许用户输入文本的输入框。
    An input box that allows users to enter text.

  • Checkbox: 一个复选框,用户可以通过点击来选择或取消选择。
    A checkbox that users can click to select or deselect.

  • Radio: 一组单选按钮中的一个,用户可以选择其中之一。
    One of a group of radio buttons where the user can select only one option.

  • Switch: 一个开关按钮,用户可以切换开关状态。
    A switch button that users can toggle on or off.

  • Slider: 一个滑块,用户可以拖动滑块来选择值。
    A slider that users can drag to select a value.

6. 列表 Widgets (List Widgets)

列表 Widgets 用于显示一组滚动的内容。

List Widgets are used to display a scrollable set of content.

  • ListView: 一个垂直或水平滚动的列表,用于显示大量数据。
    A vertically or horizontally scrollable list used to display a large amount of data.

  • GridView: 一个滚动的二维网格列表,用于显示网格中的内容。
    A scrollable 2D grid list used to display content in a grid format.

  • ListTile: 用于在 ListView 或其他列表中显示单个项目。
    Used to display a single item in a ListView or other list.

7. 导航 Widgets (Navigation Widgets)

导航 Widgets 用于在不同页面之间进行导航。

Navigation Widgets are used to navigate between different pages.

  • Navigator: 管理页面的堆栈,用于页面之间的导航。
    Manages a stack of pages and is used for navigation between pages.

  • BottomNavigationBar: 用于在应用程序底部显示一组导航选项。
    Displays a set of navigation options at the bottom of an application.

  • Drawer: 从屏幕边缘滑出的侧边栏,通常用于应用的主要导航。
    A sidebar that slides in from the edge of the screen, typically used for primary navigation in an app.

8. 动画 Widgets (Animation Widgets)

动画 Widgets 用于在应用中创建平滑的过渡效果和动画。

Animation Widgets are used to create smooth transitions and animations in the application.

  • AnimatedContainer: 一个支持动画过渡的容器,属性变化时会自动进行动画。
    A container that supports animated transitions, automatically animating when its properties change.

  • AnimatedOpacity: 用于控制 Widget 的透明度,并且在透明度变化时进行动画过渡。
    Controls the opacity of a Widget and animates the transition when the opacity changes.

  • AnimatedBuilder: 一个通用的动画工具类,用于构建更复杂的动画。
    A general-purpose animation tool used to build more complex animations.

9. 手势 Widgets (Gesture Widgets)

手势 Widgets 用于检测用户的手势输入,如点击、滑动等。

Gesture Widgets are used to detect user gesture inputs, such as taps, swipes, etc.

  • GestureDetector: 一个通用的手势检测 Widget,可以检测点击、双击、滑动等手势。
    A general-purpose gesture detector Widget that can detect taps, double-taps, swipes, and other gestures.

  • InkWell: 一个带有点击涟漪效果的 Widget,通常用于按钮。
    A Widget with a ripple effect on tap, commonly used for buttons.

10. 状态管理 Widgets (State Management Widgets)

状态管理 Widgets 用于管理和维护应用的状态。

State Management Widgets are used to manage and maintain the state of the application.

  • StatefulWidget: 支持状态变化的 Widget,每当状态变化时,Widget 重新构建。
    A Widget that supports state changes, rebuilding the Widget whenever the state changes.

  • StatelessWidget: 一个不变的 Widget,其状态在生命周期中不会改变。
    An immutable Widget whose state does not change during its lifecycle.