当前位置: 首页 > 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,清晰的排版和策略性放置的图像有助于吸引用户体验。

相关文章:

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. 在本节中&#xff0c;我们将使用“Te…...

使用 xlrd 和 xlwt 库进行 Excel 文件操作

使用 xlrd 和 xlwt 库进行 Excel 文件操作 在数据分析和处理的过程中,Excel 文件是最常用的数据存储格式之一。Python 提供了多种库来处理 Excel 文件,其中 xlrd 和 xlwt 是两个经典的库,分别用于读取和写入 Excel 文件。本文将详细介绍如何使用这两个库进行 Excel 文件的操…...

03.04、化栈为队

03.04、化栈为队 1、题目描述 实现一个 MyQueue 类&#xff0c;该类用两个栈来实现一个队列。 2、解题思路 本题要求使用两个栈来实现一个队列。队列遵循先进先出&#xff08;FIFO&#xff09;的原则&#xff0c;而栈遵循后进先出&#xff08;LIFO&#xff09;的原则。因此…...

Coppelia Sim (v-REP)仿真 机器人3D相机手眼标定与实时视觉追踪 (二)

coppelia sim[V-REP]仿真实现 机器人于3D相机手眼标定与实时视觉追踪 二 zmq API接口python调用python获取3D相机的数据获取彩色相机的数据获取深度相机的数据用matpolit显示 python控制机器人运动直接控制轴的位置用IK运动学直接移动到末端姿态 相机内参的标定记录拍照点的位置…...

苏州金龙技术创新赋能旅游新质生产力

2024年10月23日&#xff0c;备受瞩目的“2024第六届旅游出行大会”在云南省丽江市正式开幕。作为客车行业新质生产力标杆客车&#xff0c;苏州金龙在大会期间现场展示了新V系V12商旅版、V11和V8E纯电车型&#xff0c;为旅游出行提供全新升级方案。 其中&#xff0c;全新15座V1…...

ceph pg stale 恢复

问题 如果 ceph -s 看到 ceph 有类似如下状态的 pg data:volumes: 1/1 healthypools: 5 pools, 113 pgsobjects: 6.94k objects, 22 GiBusage: 24 GiB used, 33 TiB / 33 TiB availpgs: 0.885% pgs not active366/13880 objects degraded (2.637%)...

Openlayers高级交互(8/20):选取feature,平移feature

本示例介绍如何在vue+openlayers中使用Translate,选取feature,平移feature。选择的时候需要按住shift。Translate 功能通常是指在地图上平移某个矢量对象的位置。在 OpenLayers 中,可以通过修改矢量对象的几何位置来实现这一功能。 效果图 配置方式 1)查看基础设置:http…...

uniapp renderjs页面传值

scrip标签里加 lang“renderjs” &#xff0c;可以使用原生js的dom&#xff0c;但是我在使用中发现以下问题&#xff0c;导致数据不能动态获取 1. onLoad获取上级页面传值 // APP不会触发&#xff0c;h5可以 2. props不会触发 解决办法添加 script 逻辑层数据传入渲染层 ren…...

AI赋能R-Meta分析核心技术:从热点挖掘到高级模型、助力高效科研与论文发表

Meta分析是针对某一科研问题&#xff0c;根据明确的搜索策略、选择筛选文献标准、采用严格的评价方法&#xff0c;对来源不同的研究成果进行收集、合并及定量统计分析的方法&#xff0c;现已广泛应用于农林生态&#xff0c;资源环境等方面&#xff0c;成为Science、Nature论文的…...

AMD锐龙8845HS+780M核显 虚拟机安装macOS 15 Sequoia 15.0.1 (2024.10)

最近买了机械革命无界14X&#xff0c;CPU是8845HS&#xff0c;核显是780M&#xff0c;正好macOS 15也出了正式版&#xff0c;试试兼容性&#xff0c;安装过程和之前差不多&#xff0c;这次我从外网获得了8核和16核openCore&#xff0c;分享一下。 提前发一下ISO镜像地址和open…...

当事人单方委托专业机构或个人出具的书面意见,证据效力如何认定?

裁判要旨&#xff1a;当事人就专门性问题单方自行委托专业机构或者个人出具的书面意见&#xff0c;虽然不属于民事诉讼法上所称的由人民法院经由司法鉴定程序所获得的鉴定意见&#xff0c;但法律并未排除其作为证据的资格。对一方当事人就专门性问题自行委托有关机构或者人员出…...

AUTOSAR CP 中 BswM 模块功能与使用介绍(2/2)

三、 AUTOSAR BswM 模块详解及 ARXML 示例 BswM 模块的主要功能 BswM&#xff08;Basic Software Mode Manager&#xff09;模块在 AUTOSAR 架构中扮演着模式管理的核心角色。它负责管理车辆的各种模式&#xff08;如启动、运行、停车等&#xff09;&#xff0c;并根据不同的…...

PCB电路板为什么大多是绿色的

PCB电路板为什么大多是绿色的 1.绿色油墨为什么最常用&#xff1f;1.1.性能角度1.2.经济和历史角度1.3.人文和环保角度 2.误区&#xff1a;黑色PCB板更高端&#xff1f;3.总结 PCB电路板上面的绿色是一层阻焊油墨&#xff08;solder mask&#xff09;&#xff0c;主要作用&…...

Golang | Leetcode Golang题解之第508题出现次数最多的子树元素和

题目&#xff1a; 题解&#xff1a; func findFrequentTreeSum(root *TreeNode) (ans []int) {cnt : map[int]int{}maxCnt : 0var dfs func(*TreeNode) intdfs func(node *TreeNode) int {if node nil {return 0}sum : node.Val dfs(node.Left) dfs(node.Right)cnt[sum]if…...

【安全解决方案】深入解析:如何通过CDN获取用户真实IP地址

一、业务场景 某大型互联网以及电商公司为了防止客户端获取到真实的ip地址&#xff0c;以及达到保护后端业务服务器不被网站攻击&#xff0c;同时又可以让公安要求留存网站日志和排查违法行为&#xff0c;以及打击犯罪的时候&#xff0c;获取不到真实的ip地址&#xff0c;发现…...

git 免密的方法

方法一&#xff1a; 通过生成credential配置 git config --global credential.helper store 查看.gitconfig文件&#xff0c;发现多了一行 [credential] helper store 方法二&#xff1a; 修改仓库中.git/config文件 url http://账号:密码git.test.com.cn/test/xx.git或者带…...

如何用 obdiag 排查 OceanBase数据库的卡合并问题——《OceanBase诊断系列》14

1. 背景 卡合并在OceanBase中是一个复杂的问题&#xff0c;其产生可能源于多种因素。目前&#xff0c;对于卡合并的明确界定尚不存在统一标准&#xff0c;一方面&#xff0c;我们界定超过36小时未完成合并为合并超时&#xff0c;此时RS会记录ERROR日志&#xff1b;另一方面&am…...

hackme靶机渗透流程

一&#xff0c;搭建环境 本次测试使用hackme的靶机 攻击为kali&#xff08;192.168.30.130&#xff09;与物理机 二&#xff0c;信息收集 1.确定IP 先确定mac信息&#xff0c;再搭配主机扫描确定靶机的IP地址 00:0C:29:D0:F5:74 确定靶机地址为 192.168.30.133 2.扫描靶机…...

uniapp 常用的地区行业各种多选多选,支持回显,复制粘贴可使用

uniapp 常用的地区行业各种多选多选&#xff0c;支持回显 必须导入uni-popup 弹出层 该组件 1.目前项目开发中使用到这类似挺多的&#xff0c;记录一下&#xff0c;方便以后是使用 2.使用前提&#xff0c;目前不做无限级&#xff0c;只支持二维数组&#xff0c;模板里只循环了两…...

iOS 本地存储地址(位置)

前言: UserDefaults 存在沙盒的 Library --> Preferences--> .plist文件 CoreData 存在沙盒的 Library --> Application Support--> xx.sqlite 一个小型数据库里 (注:Application Support 这个文件夹已开始是没有的,只有当你写了存储代码,运行之后,目录里才会出…...

浏览器访问 AWS ECS 上部署的 Docker 容器(监听 80 端口)

✅ 一、ECS 服务配置 Dockerfile 确保监听 80 端口 EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]或 EXPOSE 80 CMD ["python3", "-m", "http.server", "80"]任务定义&#xff08;Task Definition&…...

树莓派超全系列教程文档--(62)使用rpicam-app通过网络流式传输视频

使用rpicam-app通过网络流式传输视频 使用 rpicam-app 通过网络流式传输视频UDPTCPRTSPlibavGStreamerRTPlibcamerasrc GStreamer 元素 文章来源&#xff1a; http://raspberry.dns8844.cn/documentation 原文网址 使用 rpicam-app 通过网络流式传输视频 本节介绍来自 rpica…...

基于距离变化能量开销动态调整的WSN低功耗拓扑控制开销算法matlab仿真

目录 1.程序功能描述 2.测试软件版本以及运行结果展示 3.核心程序 4.算法仿真参数 5.算法理论概述 6.参考文献 7.完整程序 1.程序功能描述 通过动态调整节点通信的能量开销&#xff0c;平衡网络负载&#xff0c;延长WSN生命周期。具体通过建立基于距离的能量消耗模型&am…...

最新SpringBoot+SpringCloud+Nacos微服务框架分享

文章目录 前言一、服务规划二、架构核心1.cloud的pom2.gateway的异常handler3.gateway的filter4、admin的pom5、admin的登录核心 三、code-helper分享总结 前言 最近有个活蛮赶的&#xff0c;根据Excel列的需求预估的工时直接打骨折&#xff0c;不要问我为什么&#xff0c;主要…...

SpringCloudGateway 自定义局部过滤器

场景&#xff1a; 将所有请求转化为同一路径请求&#xff08;方便穿网配置&#xff09;在请求头内标识原来路径&#xff0c;然后在将请求分发给不同服务 AllToOneGatewayFilterFactory import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; impor…...

【论文阅读28】-CNN-BiLSTM-Attention-(2024)

本文把滑坡位移序列拆开、筛优质因子&#xff0c;再用 CNN-BiLSTM-Attention 来动态预测每个子序列&#xff0c;最后重构出总位移&#xff0c;预测效果超越传统模型。 文章目录 1 引言2 方法2.1 位移时间序列加性模型2.2 变分模态分解 (VMD) 具体步骤2.3.1 样本熵&#xff08;S…...

【学习笔记】深入理解Java虚拟机学习笔记——第4章 虚拟机性能监控,故障处理工具

第2章 虚拟机性能监控&#xff0c;故障处理工具 4.1 概述 略 4.2 基础故障处理工具 4.2.1 jps:虚拟机进程状况工具 命令&#xff1a;jps [options] [hostid] 功能&#xff1a;本地虚拟机进程显示进程ID&#xff08;与ps相同&#xff09;&#xff0c;可同时显示主类&#x…...

Spring是如何解决Bean的循环依赖:三级缓存机制

1、什么是 Bean 的循环依赖 在 Spring框架中,Bean 的循环依赖是指多个 Bean 之间‌互相持有对方引用‌,形成闭环依赖关系的现象。 多个 Bean 的依赖关系构成环形链路,例如: 双向依赖:Bean A 依赖 Bean B,同时 Bean B 也依赖 Bean A(A↔B)。链条循环: Bean A → Bean…...

C++.OpenGL (14/64)多光源(Multiple Lights)

多光源(Multiple Lights) 多光源渲染技术概览 #mermaid-svg-3L5e5gGn76TNh7Lq {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-3L5e5gGn76TNh7Lq .error-icon{fill:#552222;}#mermaid-svg-3L5e5gGn76TNh7Lq .erro…...

Python基于历史模拟方法实现投资组合风险管理的VaR与ES模型项目实战

说明&#xff1a;这是一个机器学习实战项目&#xff08;附带数据代码文档&#xff09;&#xff0c;如需数据代码文档可以直接到文章最后关注获取。 1.项目背景 在金融市场日益复杂和波动加剧的背景下&#xff0c;风险管理成为金融机构和个人投资者关注的核心议题之一。VaR&…...