flutter - 使用 SvgPicture.assets 加载图片时出现颤振问题
问题描述
我正在尝试将图像加载到我的颤振应用程序中,并且由于某些原因,我收到了一个我不知道如何修复的错误
这是我的代码:
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
class LogInPage extends StatefulWidget {
const LogInPage({Key? key}) : super(key: key);
@override
_LogInPageState createState() => _LogInPageState();
}
class _LogInPageState extends State<LogInPage> {
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
double viewInsert = MediaQuery.of(context).viewInsets.bottom;
double defaultLoginSize = size.height - (size.height * 0.2);
double defaultRegisterSize = size.height - (size.height * 0.1);
return Scaffold(
body: Stack(
children: [
Align(
alignment: Alignment.center,
child: SingleChildScrollView(
child: Container(
width: size.width,
height: defaultLoginSize,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Welcome',
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 24),
),
SizedBox(height: 40),
SvgPicture.asset('assets/Image1.png'), // this is where I am trying to load
// the picture
SizedBox(height: 40),
Container(
margin: EdgeInsets.symmetric(vertical: 10),
padding:
EdgeInsets.symmetric(horizontal: 20, vertical: 5),
width: size.width,
),
],
),
),
),
),
],
),
);
}
}
这是我得到的错误:
SVG 捕获的
异常 解析单帧图片流时引发以下 _Exception:
异常:FormatException:意外的扩展字节(偏移量 0)
图片提供者:ExactAssetPicture(name: "assets/MyBarber3.png", bundle: null, colorFilter: >null)
图片键:AssetBundlePictureKey(bundle: PlatformAssetBundle#05902(), name:
"assets/MyBarber3.png", colorFilter: null) 这是我的 ymal 文件:
dependencies:
flutter:
sdk: flutter
firebase_core: ^1.4.0
firebase_database: ^7.1.2
firebase_auth: ^3.0.1
cloud_firestore: ^2.4.0
firebase_storage: ^10.0.1
flutter_svg: ^0.22.0
google_fonts: ^2.1.0
cupertino_icons: ^1.0.2
assets:
- assets/
有人知道我该如何解决吗?
解决方案
您的代码清楚地表明您正在尝试在 SvgPicture.asset 函数中加载 png
SvgPicture.asset('assets/Image1.png'), // here
您可以使用加载您的 png 图像
Image.asset('assets/Image1.png')
或者
AssetImage('assets/Image1.png')
否则放置svg图片,它工作正常。
推荐阅读
- flutter - 颤振导航栏未选择的文本颜色未显示
- java - 如何在反应式 Spring Security OAuth2 中信任自定义证书?
- javascript - 从 Xrm.Page 删除后表单中托管的 WebResource 从上下文访问
- sql - 优化案例表达式
- docker - 詹金斯 - 码头工人:找不到命令
- html - CSS Cascade: !important 与动画
- javascript - 将 prettier 作为 NPM 包安装和在 VS Code 中安装 prettier 扩展有什么区别
- javascript - Chrome 质疑任何在此文本框中输入文本的方式?
- python-3.x - 使用 Selenium Python3 向文本区域发送密钥
- vue.js - 即使前端源具有 https URL,是什么让浏览器从 http URL 加载内容?