【问题标题】:State's build() method keep get called when I am using image_picker and image_crop library in iOS device only当我仅在 iOS 设备中使用 image_picker 和 image_crop 库时,State 的 build() 方法会被调用
【发布时间】:2018-09-05 03:16:54
【问题描述】:

当我从图库中选择图像并想要执行进一步操作时,我正在使用这两个库从图库或相机(image_pickerimage_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);

}

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    小部件的build 方法是为了经常调用。我建议您重新构建事物,以便根据 Flutter 框架的要求调用您的构建方法。

    注意:最好将“状态”尽可能地推到树的下方(朝向叶小部件),以尽量减少由于状态变化而导致小部件重建的影响。

    在您的情况下,您可能需要考虑从 initState 方法中删除 _getAndScanImage()。让您的渲染流影响您的交互不是一个好的模式。

    您可以尝试让_getAndScanImage() 方法由按下按钮 或其他用户触发操作而不是initState 中的呈现生命周期来触发吗?

    【讨论】:

      猜你喜欢
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多