flutter10 - 資源管理

Flutter10 - 資源管理

資源也在pubspec.yaml文件裏來管理應用

flutter:
  assets:
    - assets/my_icon.png
    - assets/background.png

Asset 變體(variant)

如果應用程序目錄中有以下文件:

  • …/pubspec.yaml

  • …/graphics/my_icon.png

  • …/graphics/background.png

  • …/graphics/dark/background.png

  • …etc.

    然後pubspec.yaml文件中只需包含:

    flutter:
      assets:
        - graphics/background.png
    

    那麼這兩個graphics/background.pnggraphics/dark/background.png 都將包含在您的asset bundle中。前者被認爲是main asset (主資源),後者被認爲是一種變體(variant)。

選擇匹配當前設備分辨率的圖片時,Flutter會使用到asset變體

加載assets

可以通過AssetBundle對象訪問其asset 。有兩種主要方法允許從Asset bundle中加載字符串或圖片(二進制)文件。

加載文本assets

  • 通過rootBundle 對象加載:每個Flutter應用程序都有一個rootBundle對象, 通過它可以輕鬆訪問主資源包,直接使用package:flutter/services.dart中全局靜態的rootBundle對象來加載asset即可。
  • 通過 DefaultAssetBundle 加載:建議使用 DefaultAssetBundle 來獲取當前BuildContext的AssetBundle。 這種方法不是使用應用程序構建的默認asset bundle,而是使父級widget在運行時動態替換的不同的AssetBundle,這對於本地化或測試場景很有用。

通常,可以使用DefaultAssetBundle.of()在應用運行時來間接加載asset(例如JSON文件),而在widget上下文之外,或其它AssetBundle句柄不可用時,可以使用rootBundle直接加載這些asset,例如:

import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;

Future<String> loadAsset() async {
  return await rootBundle.loadString('assets/config.json');
}

加載圖片

聲明分辨率相關圖片assets

AssetImage 可以將asset的請求邏輯映射到最接近當前設備像素比例(dpi)的asset。

前提是要有特定的目錄結構來保存asset

  • …/image.png
  • …/Mx/image.png
  • …/Nx/image.png
  • …etc.

其中M和N是倍數

  • …/my_icon.png
  • …/2.0x/my_icon.png
  • …/3.0x/my_icon.png

在設備像素比率爲1.8的設備上,.../2.0x/my_icon.png 將被選擇。對於2.7的設備像素比率,.../3.0x/my_icon.png將被選擇。

pubspec.yaml中asset部分中的每一項都應與實際文件相對應,但主資源項除外。

主資源缺少某個資源時,會按分辨率從低到高的順序去選擇

加載圖片

加載圖片,可以使用 AssetImage類。

new AssetImage(‘graphics/background.png’),

Widget build(BuildContext context) {
  return new DecoratedBox(
    decoration: new BoxDecoration(
      image: new DecorationImage(
        image: new AssetImage('graphics/background.png'),
      ),
    ),
  );
}

AssetImage 並非是一個widget, 它實際上是一個ImageProvider

Image.asset()方法可以直接得到一個顯示圖片的widge

Widget build(BuildContext context) {
  return Image.asset('graphics/background.png');
}

使用默認的 asset bundle 加載資源時,內部會自動處理分辨率等

ImageStreamImageCache 時你會注意到有與縮放相關的參數

依賴包中的資源圖片

要加載依賴包中的圖像,必須給AssetImage提供package參數

例如,假設應用程序依賴於一個名爲“my_icons”的包,它具有如下目錄結構:

  • …/pubspec.yaml
  • …/icons/heart.png
  • …/icons/1.5x/heart.png
  • …/icons/2.0x/heart.png
  • …etc.

然後加載圖像,使用:

 new AssetImage('icons/heart.png', package: 'my_icons')

new Image.asset('icons/heart.png', package: 'my_icons')

注意:包在使用本身的資源時也應該加上package參數來獲取

打包包中的 assets

如果在pubspec.yaml文件中聲明瞭期望的資源,它將會打包到相應的package中。特別是,包本身使用的資源必須在pubspec.yaml中指定。

包也可以選擇在其lib/文件夾中包含未在其pubspec.yaml文件中聲明的資源。在這種情況下,對於要打包的圖片,應用程序必須在pubspec.yaml中指定包含哪些圖像。 例如,一個名爲“fancy_backgrounds”的包,可能包含以下文件:

  • …/lib/backgrounds/background1.png
  • …/lib/backgrounds/background2.png
  • …/lib/backgrounds/background3.png

要包含第一張圖像,必須在pubspec.yaml的assets部分中聲明它:

flutter:
  assets:
    - packages/fancy_backgrounds/backgrounds/background1.png

lib/是隱含的,所以它不應該包含在資產路徑中。

特定平臺assets

如果要給我們的應用設置APP圖標或者添加啓動圖,那我們必須使用特定平臺的assets。

android中跟以前一樣

ios如下

更新啓動頁

Flutter框架加載時,Flutter會使用本地平臺機制繪製啓動頁。此啓動頁將持續到Flutter渲染應用程序的第一幀時。

Android

要將啓動屏幕(splash screen)添加到您的Flutter應用程序, 請導航至.../android/app/src/main。在res/drawable/launch_background.xml,通過自定義drawable來實現自定義啓動界面(你也可以直接換一張圖片)。

iOS

要將圖片添加到啓動屏幕(splash screen)的中心,請導航至.../ios/Runner。在Assets.xcassets/LaunchImage.imageset, 拖入圖片,並命名爲LaunchImage.png[email protected][email protected]。 如果你使用不同的文件名,那您還必須更新同一目錄中的Contents.json文件,

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章