first commit
This commit is contained in:
162
moehu/moehu_fontend/lib/widget/ImagePickerWidget.dart
Normal file
162
moehu/moehu_fontend/lib/widget/ImagePickerWidget.dart
Normal file
@@ -0,0 +1,162 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:peach/config/Colors.dart';
|
||||
import 'package:peach/entity/_.dart';
|
||||
import 'package:peach/utils/_.dart';
|
||||
|
||||
/*
|
||||
* 选择相册或者拍照
|
||||
*/
|
||||
class ImagePickerWidget extends StatelessWidget {
|
||||
final Widget child;
|
||||
final Function onChange;
|
||||
|
||||
const ImagePickerWidget({this.child, this.onChange});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
_showCupertinoActionSheet(context);
|
||||
},
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
|
||||
_showCupertinoActionSheet(BuildContext context) async {
|
||||
var result = await showModalBottomSheet(
|
||||
context: context,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
|
||||
builder: (context) {
|
||||
return new Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
new ListTile(
|
||||
leading: new Icon(Icons.photo_camera),
|
||||
title: new Text("拍照"),
|
||||
onTap: () async {
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
Divider(height: 1),
|
||||
new ListTile(
|
||||
leading: new Icon(Icons.photo_library),
|
||||
title: new Text("相册"),
|
||||
onTap: () async {
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
Divider(height: 1),
|
||||
Container(
|
||||
height: Screen.setHeight(40),
|
||||
child: Text("取 消", style: TextStyle(color: ColorConfig.ThemeColor,fontSize: Screen.setFontSize(18)),),
|
||||
alignment: Alignment.center,
|
||||
)
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
if (result == 'photograph') {
|
||||
image(ImageSource.camera);
|
||||
} else if (result == 'album') {
|
||||
image(ImageSource.gallery);
|
||||
}
|
||||
}
|
||||
|
||||
Future image(ImageSource type) async {
|
||||
final pickedFile = await ImagePicker().getImage(source: type);
|
||||
if (pickedFile != null) {
|
||||
onChange(ImagePickerEntity(
|
||||
path: File(pickedFile.path), pickedFile: pickedFile));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// import 'dart:io';
|
||||
//
|
||||
// import 'package:flutter/cupertino.dart';
|
||||
// import 'package:flutter/material.dart';
|
||||
// import 'package:image_picker/image_picker.dart';
|
||||
// import 'package:peach/config/Colors.dart';
|
||||
// import 'package:peach/entity/_.dart';
|
||||
//
|
||||
// /*
|
||||
// * 选择相册或者拍照
|
||||
// */
|
||||
// class ImagePickerWidget extends StatelessWidget {
|
||||
// final Widget child;
|
||||
// final Function onChange;
|
||||
//
|
||||
// const ImagePickerWidget({this.child, this.onChange});
|
||||
//
|
||||
// @override
|
||||
// Widget build(BuildContext context) {
|
||||
// return GestureDetector(
|
||||
// onTap: () {
|
||||
// _showCupertinoActionSheet(context);
|
||||
// },
|
||||
// child: child,
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// _showCupertinoActionSheet(BuildContext context) async {
|
||||
// var result = await showCupertinoModalPopup(
|
||||
// context: context,
|
||||
// builder: (context) {
|
||||
// return CupertinoActionSheet(
|
||||
// actions: <Widget>[
|
||||
// CupertinoActionSheetAction(
|
||||
// child: Text(
|
||||
// '拍照',
|
||||
// style: TextStyle(color: ColorConfig.ThemeColor),
|
||||
// ),
|
||||
// onPressed: () {
|
||||
// Navigator.of(context).pop('photograph');
|
||||
// },
|
||||
// isDefaultAction: true,
|
||||
// ),
|
||||
// CupertinoActionSheetAction(
|
||||
// child: Text(
|
||||
// '相册',
|
||||
// style: TextStyle(color: ColorConfig.ThemeColor),
|
||||
// ),
|
||||
// onPressed: () {
|
||||
// Navigator.of(context).pop('album');
|
||||
// },
|
||||
// isDefaultAction: true,
|
||||
// ),
|
||||
// ],
|
||||
// cancelButton: CupertinoActionSheetAction(
|
||||
// child: Text(
|
||||
// '取消',
|
||||
// style: TextStyle(color: ColorConfig.ThemeColor),
|
||||
// ),
|
||||
// onPressed: () {
|
||||
// Navigator.of(context).pop('cancel');
|
||||
// },
|
||||
// ),
|
||||
// );
|
||||
// },
|
||||
// );
|
||||
//
|
||||
// if (result == 'photograph') {
|
||||
// image(ImageSource.camera);
|
||||
// } else if (result == 'album') {
|
||||
// image(ImageSource.gallery);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Future image(ImageSource type) async {
|
||||
// final pickedFile = await ImagePicker().getImage(source: type);
|
||||
// if (pickedFile != null) {
|
||||
// onChange(ImagePickerEntity(
|
||||
// path: File(pickedFile.path), pickedFile: pickedFile));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user