[英]how can I use showChoiceDialog() in my main.dart file
here is code of imagepicker in flutter .这是 flutter 中 imagepicker 的代码。
how can I use showChoiceDialog(), opengallery() ,openCamera() in my other files .如何在我的其他文件中使用showChoiceDialog()、opengallery()、openCamera() 。 what class objects and variable should i have to make in order to use this functions in other file .how can I use showChoiceDialog(), opengallery() ,openCamera() in my other files .
为了在其他文件中使用此函数,我应该制作什么类对象和变量。如何在我的其他文件中使用showChoiceDialog()、opengallery()、openCamera() 。 what class objects and variable should i have to make in order to use this functions in other file .
为了在其他文件中使用这个函数,我应该制作什么类对象和变量。
how can i set new image to Image Widgets .how can update the state.如何将新图像设置为图像小部件。如何更新状态。
imagepicker.dart图像选择器.dart
import 'dart:ffi';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
class CameraWidget extends StatefulWidget {
@override
State createState() {
// TODO: implement createState
return CameraWidgetState();
}
}
class CameraWidgetState extends State {
PickedFile? imageFile = null;
Future<void> showChoiceDialog(BuildContext context) {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(
"Choose option",
style: TextStyle(color: Colors.blue),
),
content: SingleChildScrollView(
child: ListBody(
children: [
Divider(
height: 1,
color: Colors.blue,
),
ListTile(
onTap: () {
_openGallery(context);
},
title: Text("Gallery"),
leading: Icon(
Icons.account_box,
color: Colors.blue,
),
),
Divider(
height: 1,
color: Colors.blue,
),
ListTile(
onTap: () {
_openCamera(context);
},
title: Text("Camera"),
leading: Icon(
Icons.camera,
color: Colors.blue,
),
),
],
),
),
);
},
);
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("Pick Image Camera"),
backgroundColor: Colors.green,
),
body: Center(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Card(
child: (imageFile == null)
? Text("Choose Image")
: Image.file(File(imageFile!.path)),
),
Image.asset('images/img1.jpg'),
MaterialButton(
textColor: Colors.white,
color: Colors.pink,
onPressed: () {
showChoiceDialog(context);
},
child: Text("Select Image"),
)
],
),
),
),
);
}
void _openGallery(BuildContext context) async {
final pickedFile = await ImagePicker().getImage(
source: ImageSource.gallery,
);
setState(() {
imageFile = pickedFile!;
});
Navigator.pop(context);
}
void _openCamera(BuildContext context) async {
final pickedFile = await ImagePicker().getImage(
source: ImageSource.camera,
);
setState(() {
imageFile = pickedFile!;
});
Navigator.pop(context);
}
}
You cannot access their properties as they are marked as private.您无法访问它们的属性,因为它们被标记为私有。 To mark a function/method as private, you put an underscore before its name.
要将函数/方法标记为私有,请在其名称前添加下划线。 For example,
checkUpdates()
is a public method, and _checkUpdates()
is a public method.例如,
checkUpdates()
是公共方法, _checkUpdates()
是公共方法。
I would recommend putting these in another file if you plan on reusing them.如果您打算重用它们,我建议将它们放在另一个文件中。
A very basic method of doing this is to create a new file, image_service.dart
一个非常基本的方法是创建一个新文件
image_service.dart
Make a new class创建一个新班级
class ImageService {
static void openGallery(BuildContext context) async {
final pickedFile = await ImagePicker().getImage(
source: ImageSource.gallery,
);
setState(() {
imageFile = pickedFile!;
});
Navigator.pop(context);
}
static void openCamera(BuildContext context) async {
final pickedFile = await ImagePicker().getImage(
source: ImageSource.camera,
);
setState(() {
imageFile = pickedFile!;
});
Navigator.pop(context);
}
}
And then you can use it as follows.然后您可以按如下方式使用它。
ImageService.openGallery(context);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.