【发布时间】:2018-09-05 03:16:54
【问题描述】:
当我从图库中选择图像并想要执行进一步操作时,我正在使用这两个库从图库或相机(image_picker 和 image_cropper)中挑选和裁剪图像,然后自动调用 build 方法并更改我的代码流程。
在 android 设备中,此代码工作正常,build 方法只调用一次,但在 iOS 设备中,当我从图库中选择图像并裁剪此图像后,会调用 build。
在 iOS 设备中,当设备的照片库打开时,会调用 build 方法,当调用裁剪时,会再次调用 Drawer 类的 build 方法。
此问题仅在 Drawer 中发生,如果我将我的 TextRobo 类称为 Navigator.of(context).pushReplacementNamed('/textRobo');,那么它可以正常工作。
抽屉类
_getDrawerItemWidget(int pos, String title) {
switch (pos) {
case 0:
if(title.contains("From Gallery"))
return new TextRobo();
if(title.contains("From Camera"))
return new TextRoboCamera();
else if(widget.fragment_class.contains("Translate"))
return new TranslateLangue(widget.textToTranslate);
else
return new TranslateLangue("");
break;
case 1:
if(title.contains("From Gallery"))
return new BarCodeRobo();
else
return new BarCodeQuick();
break;
case 2:
return new TranslateLangue("");
//default:
//return new TranslateLangue("");
}
}
@override
Widget build(BuildContext context) {
print('Building widget');
return new Scaffold(
appBar: new AppBar(
iconTheme: new IconThemeData(color: Colors.white),
title: new Text("RoboScan",
style: new TextStyle(color: Colors.white),),
),
drawer: new Drawer(
child: new ListView(
children: <Widget>[
new Container( height: 140.0, color: Colors.orange,
child: new Center(child:
new Text('RoboScan', style: new TextStyle(color: Colors.white,
fontSize:25.0, fontWeight: FontWeight.bold),
),
),
),
new Column(
children: drawerOptions)
],
),
),
body: _getDrawerItemWidget(_selectedDrawerIndex, widget.fragment_class ),
);
}
图像选择器和裁剪类(TextRobo)
File _imageFile;
List<VisionText> _currentTextLabels = <VisionText>[];
FirebaseVisionTextDetector textDetector =
FirebaseVisionTextDetector.instance;
@override
void initState() {
// TODO: implement initState
//scanImage();
super.initState();
_getAndScanImage();
}
Future<void> _getAndScanImage() async {
setState(() {
_imageFile = null;
// _imageSize = null;
});
final File imageFile =
await ImagePicker.pickImage(source: ImageSource.gallery);
_cropImage(imageFile);
}
【问题讨论】: