https://github.com/FilipeSilva18/FlutterImagePickerExample
测试代码未写保存部分
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Image Picker',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
File _pickedImage;
void _pickImage() async {
final imageSource = await showDialog<ImageSource>(
context: context,
builder: (context) =>
AlertDialog(
title: Text("Select the image source"),
actions: <Widget>[
MaterialButton(
child: Text("Camera"),
onPressed: () => Navigator.pop(context, ImageSource.camera),
),
MaterialButton(
child: Text("Gallery"),
onPressed: () => Navigator.pop(context, ImageSource.gallery),
)
],
)
);
if(imageSource != null) {
final file = await ImagePicker.pickImage(source: imageSource);
if(file != null) {
setState(() => _pickedImage = file);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Image Picker"),
),
body: Center(
child: _pickedImage == null ?
Text("Nothing to show") :
Image(image: FileImage(_pickedImage)),
),
floatingActionButton: FloatingActionButton(
onPressed: _pickImage,
child: Icon(Icons.image),
),
);
}
}
版本: image_picker: 0.6.7+12
© 版权声明
文章版权归作者所有,未经允许请勿转载。