Flutter加載本地圖片

1.創(chuàng)建文件夾

2.配置pubspec.yaml文件

也可以這樣寫:

assets:
   - lib/images/woman.png
   - lib/images/2.0x/woman.png
   - lib/images/3.0x/woman.png

區(qū)別可能是這樣更具體,如果images文件夾中有不必要的圖片就不會(huì)被配置...

其中2.0x3.0x是為了適配不同屏幕分別率,其下的圖片是外層同名圖片的變體(asset variants),根據(jù)不同屏幕分別率加載合適的圖片,具體參考Flutter文檔resolution-aware的詳細(xì)說明。

遺憾的是asset variants機(jī)制只應(yīng)用于resolution-aware,而沒有所謂的theme-aware來(lái)支持深色模式切換。官方文檔只說以后可能會(huì)有其他方面的支持:

Flutter uses asset variants when choosing resolution-appropriate images. In the future, this mechanism might be extended to include variants for different locales or regions, reading directions, and so on.

3.使用

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const Center(
      child: Image(image: AssetImage('lib/images/woman.png')),
    );
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容