当前位置:网站首页>Flutter introduction advanced trip (5) Image Widget
Flutter introduction advanced trip (5) Image Widget
2022-08-09 13:25:00 【Xie Dong_】
Review of past topics:
We have learned the Widget used for text display in Flutter. For example, we will use Text Widget to display a line or a piece of basic text. If we need to set style, color and other attributes with Text, we can customize it by specifying style for TextThe styles in TextStyle show the effects we need. For text input controls, we learned about TextField and learned that simple text input requirements can be completed through TextField. InputDecoration can be used to add styles to the input box, such as comfortable auxiliary prompts, borders, and both sides.icon and so on.
Today, let's learn about another Widget---Image. As the name suggests, Image is a control used to display pictures. Like Text, it belongs to StatelessWidget. I will explain StatelessWidget and StatefullWidget in detail in a later article.Readers can ignore this knowledge point for the time being. When doing native Android development, we can specify different image sources for ImageView, which can be source networks, drawables, bitmaps, files, etc. Flutter also supports loading images from different sources., but the images that Flutter loads with different resources are slightly different from native Android. Let's enter the topic of this issue together.
How Flutter loads different resource images:
- new Image, to get the image from ImageProvider.
- new Image.asset, to get the image from the AssetBundle.
- new Image.network, for getting an image from a URL.
- new Image.file, to get an image from a file.
- new Image.memory, to get the image from memory
Image in flutter supports JPEG, PNG, GIF, Animated GIF, WebP, Animated WebP, BMP, and WBMP image formats.
1. Load image from network:
In order to develop good code reading habits, let's first look at the construction method of Image.network
Image.network(String src, {Key key,double scale: 1.0,//reduction factorthis.width,//widththis.height,//highthis.color,this.colorBlendMode,this.fit, //fill methodthis.alignment: Alignment.center,this.repeat: ImageRepeat.noRepeat, //Image arrangementthis.centerSlice,this.matchTextDirection: false,this.gaplessPlayback: false,Map headers,}) Through the construction method, we can initially clearly understand that to load network images in Flutter, we only need to specify the src of the image in Image.network, which is the url of the target image. If you need to configure the image width, height, and zoomIt is better to write than the corresponding construction method. The following example code shows loading the image from the target url, and setting the width and height of the image to 500*500;
Rendering:

Sample code:
import 'package:flutter/material.dart';void main() {runApp(new MaterialApp(home: new ImageDemo()));}class ImageDemo extends StatelessWidget {@overrideWidget build(BuildContext context) {return new Scaffold(appBar: new AppBar(title: new TextField(decoration: new InputDecoration(),),),body: new Image.network("https://p1.ssl.qhmsg.com/dr/220__/t01d5ccfbf9d4500c75.jpg",width:500,height: 500,));}}2. Load images from the project's local directory:
To load images in the project directory in Flutter, we need to declare and import resources from pubspec.yaml before we can use the image resources in the dart file. Otherwise, even if the image exists in the project directory, after you specify the path in the dart fileIt also cannot be loaded normally. Readers should pay attention to this point, which is a bit different from writing native Android.
For example, I imported the two pictures aaa.png and a.png in the images folder into the project through pubspec.yaml:

Replace the body part of the above code with Image.asset method.
body: new Image.asset('images/aaa.png',width: 500,height: 500,)The construction method of Image.asset is similar to that of network, so I won't post it. Readers can check the source code by themselves. Let's take a look at the pictures we loaded from the project directory to end our study on Image in this issue.
Rendering:

Next: Flutter Getting Started and Advanced Tour (6) Layout Widget
边栏推荐
- API调用,API传参,面向对接开发,你真的会写接口文档吗?
- Flutter入门进阶之旅(六)Layout Widget
- How to upload local file trial version in binary mode in ABAP report
- WebView注入Js代码实现大图自适应屏幕点击图片预览详情
- Intranet penetration tool ngrok usage tutorial
- Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?
- 曲鸟全栈UI自动化教学(八):框架代码讲解和进一步优化
- Customize VIEW to realize in-app message reminder to rotate up and down
- Rust 入门指南(使用JSON)
- WebView injects Js code to realize large image adaptive screen click image preview details
猜你喜欢

Scala 高阶(七):集合内容汇总(上篇)

Blocking, non-blocking, multiplexing, synchronous, asynchronous, BIO, NIO, AIO all in one pot

26. Pipeline parameter substitution command xargs

报告:想学AI的学生数量已涨200%,老师都不够用了

中断系统结构及中断控制详解

曼城推出可检测情绪的智能围巾,把球迷给整迷惑了

箭头函数和普通函数的常见区别

win10编译x264库(也有生成好的lib文件)

水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...

两个链表相加
随机推荐
无需精子卵子子宫体外培育胚胎,Cell论文作者这番话让网友们炸了
The latest interview summary in 20022 brought by Ali senior engineer is too fragrant
Go 事,如何成为一个Gopher ,并在7天找到 Go 语言相关工作,第1篇
ABAP 报表中如何以二进制方式上传本地文件试读版
世界第4疯狂的科学家,在103岁生日那天去世了
K个结点的组内逆序调整
金融业“限薪令”出台/ 软银出售过半阿里持仓/ DeepMind新实验室成立... 今日更多新鲜事在此...
How to save Simulink simulation model as image or PDF
Flutter入门进阶之旅(二)Hello Flutter
Golang学习之路(五):Golang的函数
Apexsqlrecover cannot connect to database
ABP中的数据过滤器 (转载非原创)
Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
Extract EventBus encapsulation to base class using annotations
数字化转型之支撑保障单元
Customize VIEW to realize in-app message reminder to rotate up and down
ERP不规范,同事两行泪 (转载非原创)
Nature:猪死亡1小时后,器官再次运转
罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
MySQL5.6到8.0的账号迁移