当前位置:网站首页>[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
2022-07-03 00:34:00 【Programmer community】
List of articles
- One 、 Load network pictures
- Two 、 Load static pictures
- 3、 ... and 、 Load local image
- Four 、 Complete code example
- 5、 ... and 、 Related resources
One 、 Load network pictures
Reference resources 【Flutter】Image Components ( Image Component Introduction | Image Constructors | Image.network Constructors | Image.asset Constructors ) 3、 ... and 、Image.network Constructors
Code example :
// Picture components , Load a picture from the network Image.network( // Picture address "https://img-blog.csdnimg.cn/2021032313493741. width: 200,),
Effect display :
Online pictures used in blogs : ( Drawing bed function , Prevent the picture from hanging up )
Two 、 Load static pictures
Steps for loading static pictures : First declare the picture , Then use the picture ;
① Declare image resources : stay pubspec.yaml Declare the image resource path in ;
② Visit pictures : stay dart Use... In the document AssetImage Class to access pictures ;
Configure static pictures : Registration hierarchy , assets Press on flutter Configure... In the secondary level of ;
flutter: uses-material-design: true assets: - images/sidalin.png - images/sidalin2.png
Code example : The following two methods can load static pictures
Image( width: 200, image: AssetImage("images/sidalin.png"),),Image.asset( 'images/sidalin2.png', width: 200,),
Execution effect :
3、 ... and 、 Load local image
install path_provider plug-in unit :
- Search plug-ins : stay https://pub.dev/packages Mid search path_provider plug-in unit ;
- Plug-in address : https://pub.dev/packages/path_provider
- Configuration plug-ins : stay pubspec.yaml Configure plug-ins in ;
dependencies: path_provider: ^2.0.1
- Get plug-ins : Click on pubspec.yaml In the upper right corner of the middle Pub get Button , Get plug-ins ;
- Import header file :
import 'package:path_provider/path_provider.dart';
Copy pictures to /storage/emulated/0/Android/data/kim.hsl.flutter_image_widget/files
In the path , This is also called path_provider The plug-in getExternalStorageDirectory()
Method of obtaining SD Card path ;
Copy picture command :
adb push sidalin3.png /storage/emulated/0/Android/data/kim.hsl.flutter_image_widget/files
Code example : stay initState Method calls asynchronous method to get SD Card path , stay build If in the method SD Card path is not empty , Only show Image Components ;
/// SD Card path String sdPath; @override void initState() {
// obtain SD Card path getSdPath(); } void getSdPath() async {
String path = (await getExternalStorageDirectory()).path; setState(() {
sdPath = path; }); }/// from SD Card loading picture if(sdPath != null) Image.file( File('$sdPath/sidalin3.png'), width: 200, ),
Execution effect :
Four 、 Complete code example
Code example :
import 'package:flutter/material.dart';import 'dart:io';import 'package:path_provider/path_provider.dart';void main() => runApp(MyApp());class MyApp extends StatelessWidget {
// This widget is the root of your application. @override Widget build(BuildContext context) {
return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(title: 'Flutter Demo Home Page'), ); }}class MyHomePage extends StatefulWidget {
MyHomePage({
Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState();}class _MyHomePageState extends State<MyHomePage> {
int _counter = 0; void _incrementCounter() {
setState(() {
_counter++; }); } /// SD Card path String sdPath; @override void initState() {
// obtain SD Card path getSdPath(); } void getSdPath() async {
String path = (await getExternalStorageDirectory()).path; setState(() {
sdPath = path; }); } @override Widget build(BuildContext context) {
print("sdPath : $sdPath"); return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( 'приветствовать товарища Сталина:', ), Text( '$_counter', style: Theme.of(context).textTheme.display1, ), // Picture components , Load a picture from the network Image.network( // Picture address "https://img-blog.csdnimg.cn/2021032313493741.png", width: 200, ), Image( width: 200, image: AssetImage("images/sidalin.png"), ), Image.asset( 'images/sidalin2.png', width: 200,), /// from SD Card loading picture if(sdPath != null) Image.file( File('$sdPath/sidalin3.png'), width: 200, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: Icon(Icons.add), ), // This trailing comma makes auto-formatting nicer for build methods. ); }}
pubspec.yaml The configuration file :
name: flutter_image_widgetdescription: A new Flutter application.version: 1.0.0+1environment: sdk: ">=2.1.0 <3.0.0"dependencies: flutter: sdk: flutter cupertino_icons: ^0.1.2 path_provider: ^2.0.1dev_dependencies: flutter_test: sdk: flutterflutter: uses-material-design: true assets: - images/sidalin.png - images/sidalin2.png
Imported files :
Execution effect :
5、 ... and 、 Related resources
Reference material :
- Flutter Official website : https://flutter.dev/
- Flutter Plug in download address : https://pub.dev/packages
- Flutter Developing documents : https://flutter.cn/docs ( Strongly recommend )
- official GitHub Address : https://github.com/flutter
- Flutter The Chinese community : https://flutter.cn/
- Flutter Practical tutorial : https://flutter.cn/docs/cookbook
- Flutter CodeLab : https://codelabs.flutter-io.cn/
- Dart Chinese document : https://dart.cn/
- Dart Developer website : https://api.dart.dev/
- Flutter Chinese net ( unofficial , The translation is very good ) : https://flutterchina.club/ , http://flutter.axuer.com/docs/
- Flutter Related issues : https://flutterchina.club/faq/ ( It is recommended to watch it at the introductory stage )
- GitHub Upper Flutter Open source examples : https://download.csdn.net/download/han1202012/15989510
Blog source download :
GitHub Address : https://github.com/han1202012/flutter_image_widget ( Keep updating with the progress of the blog , There may not be the source code of this blog )
Blog source snapshot : https://download.csdn.net/download/han1202012/16059814 ( The source code snapshot of this blog , You can find the source code of this blog )
边栏推荐
- pod生命周期详解
- TypeError: Cannot read properties of undefined (reading ***)
- [golang syntax] map common errors golang panic: assignment to entry in nil map
- Nc20806 District interval
- pageoffice-之bug修改之旅
- Multiprocess programming (4): shared memory
- Introduction of UART, RS232, RS485, I2C and SPI
- maya渔屋建模
- NC50528 滑动窗口
- NC17059 队列Q
猜你喜欢
maya渔屋建模
有哪些比较推荐的论文翻译软件?
What are the recommended thesis translation software?
AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决
Logback configuration file
mm中的GAN模型架构
Feature Engineering: summary of common feature transformation methods
How do educators find foreign language references?
TypeError: Cannot read properties of undefined (reading ***)
Markdown tutorial
随机推荐
Multiprocess programming (I): basic concepts
【luogu P4320】道路相遇(圆方树)
maya渔屋建模
免费自媒体必备工具分享
Gan model architecture in mm
Wechat applet obtains the information of an element (height, width, etc.) and converts PX to rpx.
Some introduction and precautions about XML
Nc50528 sliding window
详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
node_ Modules cannot be deleted
Mutual exclusion and synchronization of threads
Sysdig analysis container system call
Introduction of UART, RS232, RS485, I2C and SPI
Bigder: how to deal with the bugs found in the 32/100 test if they are not bugs
[target detection] r-cnn, fast r-cnn, fast r-cnn learning
LeedCode1480. Dynamic sum of one-dimensional array
关于Unity屏幕相关Screen的练习题目,Unity内部环绕某点做运动
Logback configuration file
FAQ | FAQ for building applications for large screen devices
关于QByteArray存储十六进制 与十六进制互转