如何使用image.file加载图像 [英] How to load images with image.file
本文介绍了如何使用image.file加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我似乎无法简单地将图像从硬盘驱动器加载到屏幕上。 Image.network似乎很简单。但是我不知道如何使用Image或Image.file。图片似乎需要流,所以我认为这不是我想要的。
I can't seem to simply load an image from the hard drive to the screen. Image.network seems straightforward. But I can't figure out how to use Image or Image.file. Image seems to require a stream, so I don't think that is what I am looking for.
import 'package:flutter/material.dart';
import 'dart:io';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
File file = new File("Someimage.jpeg");
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Image.file(file), //doesn't work, but no errors
);
}
}
我在pubspec.yaml文件中添加了Someimage,但是也不起作用:
I added Someimage to the pubspec.yaml file, but that doesn't work either:
assets:
- Someimage.jpeg
有人可以给我举一个例子吗?谢谢。
Can someone give me an example of how this is done? Thanks.
推荐答案
这是另一个使用jpg作为背景图像的示例。
Here is another example which uses a jpg as a background image. It also applies opacity to the image.
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new Scaffold(
resizeToAvoidBottomPadding: false,
appBar: new AppBar(
title: new Text("test"),
),
body: new Container(
decoration: new BoxDecoration(
image: new DecorationImage(
colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.6), BlendMode.dstATop),
image: new AssetImage("assets/images/keyboard.jpg"),
fit: BoxFit.cover,
),
),
),
),
);
}
}
这篇关于如何使用image.file加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文