【发布时间】:2021-01-18 13:30:41
【问题描述】:
我正在尝试在颤动活动中添加图像。该图像是从我本地移动设备的图库中选择的。但是,一旦我选择图像,应用程序就会崩溃并显示以下错误:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=69, result=0, data=null} to activity {com.example.MainActivity}: java.lang.IllegalStateException: Reply already submitted
我正在使用 ImagePicker 和cropPicker,但似乎 imagepicker 无法正常工作。一旦我选择了图像,我希望图像显示在 CircleAvatar 中,但这并没有发生。 以下是我的代码:
class Signup extends StatefulWidget {
@override
_SignupState createState() => _SignupState();
}
class _SignupState extends State<Signup> {
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SizedBox(
width: double.infinity,
child: Padding(
padding:
EdgeInsets.symmetric(horizontal: getProportionateScreenWidth(20)),
child: SingleChildScrollView(
child: Column(
children: [
SizedBox(height: SizeConfig.screenHeight * 0.04), // 4%
SizedBox(height: SizeConfig.screenHeight * 0.08),
Stack(
alignment: Alignment.bottomRight,
children: <Widget>[
CircleAvatar(
radius: 40,
child: _pickedImage == null ? Icon(Icons.person) : null,
backgroundImage: _pickedImage != null ? FileImage(_pickedImage) : null,
),
Padding(
padding: EdgeInsets.all(0.0),
child: GestureDetector(
onTap: () {
_showPickOptionsDialog(context);
},
child: Icon(
Icons.camera_alt_outlined,
)
),
),
]
),
],
),
),
),
),
),
);
}
_loadPicker(ImageSource source) async {
File picked = await ImagePicker.pickImage(source: source);
if (picked != null) {
_cropImage(picked);
}
Navigator.pop(context);
}
_cropImage(File picked) async {
File cropped = await ImageCropper.cropImage(
androidUiSettings: AndroidUiSettings(
statusBarColor: Colors.red,
toolbarColor: Colors.red,
toolbarTitle: "Crop Image",
toolbarWidgetColor: Colors.white,
),
sourcePath: picked.path,
aspectRatioPresets: [
CropAspectRatioPreset.original,
CropAspectRatioPreset.ratio16x9,
CropAspectRatioPreset.ratio4x3,
],
maxWidth: 800,
);
if (cropped != null) {
setState(() {
_pickedImage = cropped;
});
}
}
void _showPickOptionsDialog(BuildContext context) {
showDialog(context: context,
builder: (context) => AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
title: Text('Pick from Gallery'),
onTap: (){
_loadPicker(ImageSource.gallery);
},
),
ListTile(
title: Text('Take a Picture'),
onTap: (){
_loadPicker(ImageSource.camera);
},
),
],
),
)
);
}
}
有人可以帮我解决这个问题吗?
【问题讨论】: