Bootstrap

Flutter Image和Text图文组件实战案例

In this section, we’ll go through the process of building a user interface that showcases a product using the Text and Image widgets. We’ll follow Flutter’s best practices to ensure a clean and effective UI structure.

在本节中,我们将使用“Text”和“Image”小部件构建一个展示产品的用户界面。我们将遵循Flutter的最佳实践,以确保一个干净有效的UI结构。

Setting Up Your Project

设置项目

Create a New Flutter Project: If you don’t have an existing Flutter project, begin by creating a new one using the following command in your terminal:

创建一个新的Flutter项目:如果您没有现有的Flutter项目,请在终端中使用以下命令创建一个新项目:

flutter create product_catalog

Prepare an Image: Download an image that represents your product. If you can’t find an assets folder, create one at the root of your project and call it assets. Save this image in the assets folder within your project.

准备一个图像:下载一个代表您的产品的图像。如果你找不到assets文件夹,在项目的根目录下创建一个,命名为assets。将此图像保存在项目的assets文件夹中。

Update pubspec.yaml: Open your pubspec.yaml file and add the following lines under the flutter section to declare your image asset:

更新pubspec.yaml。打开你的pubspec.yaml文件,并在’ flutter '部分下添加以下行来声明您的图像资产:

assets:
- assets/product_image.jpg

此时: 完整的配置信息如下

name: c01_hello
description: "A new Flutter project."
publish_to: 'none'
version: 1.0.0+1
environment:
  sdk: ^3.5.3
dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.8

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^4.0.0
flutter:
  uses-material-design: true
  assets:
    - assets/1.jpg

我在项目根目录创建了一个assets目录, 然后放了一个1.jpg图片文件进去.

Building the UI

构建UI

In this example, we’ll construct a simple UI layout that displays an image of the product along with its description using the Text and Image widgets.

在本例中,我们将构建一个简单的UI布局,该布局使用“Text”和“image”小部件显示产品的图像及其描述。

Open lib/main.dart: Replace the default code in thelib/main.dart file with the following code:

打开lib/main.dart。使用以下代码替换’ lib/main.dart '文件中的默认代码。

import "package:flutter/material.dart";

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

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

  
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: "Product Catalog",
      home: ProductScreen(),
    );
  }
}

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

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("产品目录"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Image.asset(
              "assets/1.jpg", // 图片地址要包含 assets 目录
              width: 200,
              height: 200,
            ),
            const SizedBox(height: 20),
            const Text(
              "Flutter零基础快速入门班",
              style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 10),
            const Text(
              "从零开始学Flutter, 快速掌握现代跨平台APP开发核心技术.",
              textAlign: TextAlign.center,
            ),
          ],
        ),
      ),
    );
  }
}

效果预览如下:

在这里插入图片描述

Code Explanation

代码的解释

We begin by importing the required flutter/material.dartpackage.

我们首先导入所需的’ flutter/material.dart '包。

The main() function is the entry point of our application. It runs the MyApp widget.

main()函数是我们应用程序的入口点。它运行“MyApp”小部件。

The MyApp widget is a MaterialApp which defines the title of the app and sets the ProductScreen as the home screen.

“MyApp”小部件是一个“MaterialApp”,它定义了应用程序的标题,并将“ProductScreen”设置为主屏幕。

The ProductScreen widget is where the main UI is constructed. It returns a Scaffold widget containing an AppBar and the main content.

“ProductScreen”小部件是构建主UI的地方。它返回一个’ Scaffold ‘小部件,其中包含’ AppBar '和主要内容。

Within the Column widget, we use the Image.asset widget to display our product image. We specify the width and height of the image to control its dimensions.

在“Column”小部件中,我们使用Image.asset的小部件来显示我们的产品图像。我们指定图像的宽度和高度来控制其尺寸。

We add some space below the image using the SizedBox widget.

我们使用’ SizeBox '小部件在图像下方添加一些空间。

The first Text widget displays the product name with a larger font size and bold style.

第一个“Text”小部件以较大的字体和粗体样式显示产品名称。

Another SizedBox widget adds spacing between the two text elements.

另一个’ SizeBox '小部件在两个文本元素之间添加间距。

The final Text widget presents a detailed description of the product. We use the textAlign property to center-align the text.

最后的“Text”小部件显示了产品的详细描述。我们使用’ textAlign '属性来居中对齐文本。

Running the App

运行应用程序

Run Your App: Save the changes and run your app usingthe command:

运行你的应用: 保存修改并使用下面的命令运行你的应用:

flutter run

Observe the UI: When the app launches, you’ll see an AppBar with the title “Product Catalog” and a centered layout containing the product image and its description.

观察UI:当应用程序启动时,你会看到一个标题为“产品目录”的AppBar和一个包含产品图像及其描述的中心布局。

Customization and Beyond

定制和超越

This example illustrates how to create an appealing UI using the Text and Image widgets. You can further enhance the UI by experimenting with different fonts, colors, and layouts. As you explore Flutter’s widget library, remember that a well-structured UI, clear typography, and strategically placed images contribute to an engaging user experience.

这个例子说明了如何使用“文本”和“图像”小部件创建一个吸引人的UI。您可以通过尝试不同的字体、颜色和布局来进一步增强UI。当您探索Flutter的小部件库时,请记住,结构良好的UI,清晰的排版和策略性放置的图像有助于吸引用户体验。

;