【Flutter】Flutter 资源文件使用 ( 导入资源图片 | 使用图片资源 )
文章目錄
- 一、Flutter 導(dǎo)入資源圖片
- 二、Flutter 使用資源圖片
- 三、完整代碼示例
- 四、相關(guān)資源
一、Flutter 導(dǎo)入資源圖片
Flutter 資源路徑配置 : 資源路徑在根目錄中的 pubspec.yaml 配置文件中配置 ;
將 flutter 節(jié)點(diǎn)下的 assets 節(jié)點(diǎn)的注釋打開 , 即刪除前面的 # 注釋符號 ;
然后在 flutter 項(xiàng)目根目錄創(chuàng)建 images 目錄 , 將圖片 hunter.png 拷貝到該 images 目錄中 ;
并在 pubspec.yaml 配置文件的 assets 節(jié)點(diǎn)下配置 - images/hunter.png 信息 ;
# The following section is specific to Flutter. flutter:# The following line ensures that the Material Icons font is# included with your application, so that you can use the icons in# the material Icons class.uses-material-design: true# To add assets to your application, add an assets section, like this:assets:- images/hunter.png之后就可以在 flutter 項(xiàng)目中使用該文件了 ;
下圖展示了資源文件目錄結(jié)構(gòu)以及配置文件中的配置信息 ;
導(dǎo)入資源圖片樣式 :
二、Flutter 使用資源圖片
Image 組件中使用資源圖片 , 在其 image 字段使用 AssetImage 類型的圖片即可 ;
代碼示例 : 設(shè)置一個(gè) 200 x 200 大小的 Image 組件 , 顯示 images/hunter.png 資源圖片 ;
Image(width: 200,height: 200,image: AssetImage("images/hunter.png"), )三、完整代碼示例
完整代碼示例 :
import 'package:flutter/material.dart';class ResourcePage extends StatefulWidget {@override_ResourcePageState createState() => _ResourcePageState(); }class _ResourcePageState extends State<ResourcePage> {@overrideWidget build(BuildContext context) {return MaterialApp(title: "資源文件使用",theme: ThemeData(primarySwatch: Colors.blue),home: Scaffold(appBar: AppBar(title: Text("資源文件使用"),leading: GestureDetector(onTap: (){Navigator.pop(context);},child: Icon(Icons.arrow_back_ios),),),body: Container(// 居中顯示alignment: Alignment.center,// 垂直線性布局child: Column(mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[Image(width: 200,height: 200,image: AssetImage("images/hunter.png"),)],),),),);} }運(yùn)行效果 :
四、相關(guān)資源
參考資料 :
- Flutter 官網(wǎng) : https://flutter.dev/
- Flutter 開發(fā)文檔 : https://flutter.cn/docs ( 強(qiáng)烈推薦 )
- 官方 GitHub 地址 : https://github.com/flutter
- Flutter 中文社區(qū) : https://flutter.cn/
- Flutter 實(shí)用教程 : https://flutter.cn/docs/cookbook
- Flutter CodeLab : https://codelabs.flutter-io.cn/
- Dart 中文文檔 : https://dart.cn/
- Dart 開發(fā)者官網(wǎng) : https://api.dart.dev/
- Flutter 中文網(wǎng) ( 非官方 , 翻譯的很好 ) : https://flutterchina.club/ , http://flutter.axuer.com/docs/
- Flutter 相關(guān)問題 : https://flutterchina.club/faq/ ( 入門階段推薦看一遍 )
博客源碼下載 :
-
GitHub 地址 : https://github.com/han1202012/flutter_cmd ( 隨博客進(jìn)度一直更新 , 有可能沒有本博客的源碼 )
-
博客源碼快照 : https://download.csdn.net/download/han1202012/15539996 ( 本篇博客的源碼快照 , 可以找到本博客的源碼 )
總結(jié)
以上是生活随笔為你收集整理的【Flutter】Flutter 资源文件使用 ( 导入资源图片 | 使用图片资源 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Flutter】Flutter 手势交
- 下一篇: 【错误记录】Flutter 插件不兼容