简介
用于展示本地的文件
- 将给定的File对象解码为图片
- 可以指定缩放scale
基本用法
简写形式和Image也都非常相似
- Image.file 是ImageFile 的一种简写形式
代码演示
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
class FileImageDemo extends StatefulWidget {
@override
_FileImageDemoState createState() => _FileImageDemoState();
}
class _FileImageDemoState extends State<FileImageDemo> {
File _image;
Future getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_image = image;
});
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Center(
child: _image == null
? Text('您还未选择任何图片.')
: Image.file(_image,scale: 0.5,fit: BoxFit.cover,),
),
FlatButton(
onPressed: getImage,
child: Text('点击选择图片',style: TextStyle(color: Colors.white),),
color: Colors.lightBlue
)
],
);
}
}