作者:tengpou_260475 | 来源:互联网 | 2023-07-12 22:25
开发过程中,有些图片是要放到本地资源文件中,然后本地加载,这样做需要两个步骤就可以实现
1.创建本地资源文件夹Images,然后添加图片 1.jpg, 2.jpg
2.配置pubspec.yaml 中的资源路径,必须配置,如果不配置,在程序中图片就会加载不出来
3.使用 Image.asset("images/1.jpg",width:500,height:50) 加载图片
pubspec.yaml配置
name: demoone
description: A new Flutter project.# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# Read more about versioning at semver.org.
version: 1.0.0&#43;1environment:sdk: ">&#61;2.0.0-dev.68.0 <3.0.0"dependencies:flutter:sdk: flutter# The following adds the Cupertino Icons font to your application.# Use with the CupertinoIcons class for iOS style icons.cupertino_icons: ^0.1.2dev_dependencies:flutter_test:sdk: flutter# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec# 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/1.jpg- images/2.jpg# An image asset can refer to one or more resolution-specific "variants", see# https://flutter.io/assets-and-images/#resolution-aware.# For details regarding adding assets from package dependencies, see# https://flutter.io/assets-and-images/#from-packages# To add custom fonts to your application, add a fonts section here,# in this "flutter" section. Each entry in this list should have a# "family" key with the font family name, and a "fonts" key with a# list giving the asset and other descriptors for the font. For# example:# fonts:# - family: Schyler# fonts:# - asset: fonts/Schyler-Regular.ttf# - asset: fonts/Schyler-Italic.ttf# style: italic# - family: Trajan Pro# fonts:# - asset: fonts/TrajanPro.ttf# - asset: fonts/TrajanPro_Bold.ttf# weight: 700## For details regarding fonts from package dependencies,# see https://flutter.io/custom-fonts/#from-packages
程序代码
import &#39;package:flutter/material.dart&#39;;void main() &#61;> runApp(MyApp());class MyApp extends StatelessWidget {&#64;overrideWidget build(BuildContext context) {return MaterialApp(title: &#39;Flutter Positioned布局控件&#39;,home: Scaffold(appBar: AppBar(title: Text("Flutter资源图片"),),body: new Column(mainAxisAlignment: MainAxisAlignment.start,children: [new Expanded(child:Image.asset("images/1.jpg",width:500,height:50) ,),new Expanded(child:Image.asset("images/2.jpg",width:500,height:100) ,)],)));}
}