当前位置: 首页 > news >正文

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: fluttercupertino_icons: ^1.0.8dev_dependencies:flutter_test:sdk: flutterflutter_lints: ^4.0.0
flutter:uses-material-design: trueassets:- 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,清晰的排版和策略性放置的图像有助于吸引用户体验。

http://www.lryc.cn/news/469069.html

相关文章:

  • 使用 xlrd 和 xlwt 库进行 Excel 文件操作
  • 03.04、化栈为队
  • Coppelia Sim (v-REP)仿真 机器人3D相机手眼标定与实时视觉追踪 (二)
  • 苏州金龙技术创新赋能旅游新质生产力
  • ceph pg stale 恢复
  • Openlayers高级交互(8/20):选取feature,平移feature
  • uniapp renderjs页面传值
  • AI赋能R-Meta分析核心技术:从热点挖掘到高级模型、助力高效科研与论文发表
  • AMD锐龙8845HS+780M核显 虚拟机安装macOS 15 Sequoia 15.0.1 (2024.10)
  • 当事人单方委托专业机构或个人出具的书面意见,证据效力如何认定?
  • AUTOSAR CP 中 BswM 模块功能与使用介绍(2/2)
  • PCB电路板为什么大多是绿色的
  • Golang | Leetcode Golang题解之第508题出现次数最多的子树元素和
  • 【安全解决方案】深入解析:如何通过CDN获取用户真实IP地址
  • git 免密的方法
  • 如何用 obdiag 排查 OceanBase数据库的卡合并问题——《OceanBase诊断系列》14
  • hackme靶机渗透流程
  • uniapp 常用的地区行业各种多选多选,支持回显,复制粘贴可使用
  • iOS 本地存储地址(位置)
  • uni.showLoading 时禁止点击(防止表单重复提交) 小程序调取微信支付
  • OpenClash与Tailscale冲突得问题
  • day02|计算机网络重难点之HTTP请求报文和响应报文
  • Flutter之build 方法详解
  • 开源呼叫中心系统与商业软件的对比
  • 【人工智能】——matplotlib教程
  • 【c++ gtest】使用谷歌提供的gtest和抖音豆包提供的AI大模型来对代码中的函数进行测试
  • 使用Angular构建动态Web应用
  • 25届电信保研经验贴(自动化所)
  • 大数据-190 Elasticsearch - ELK 日志分析实战 - 配置启动 Filebeat Logstash
  • 不同类型的 LED 驱动电源在检测方法上有哪些不同?-纳米软件