简体   繁体   中英

Flutter- How to make multiple notifyListeners()?

Here, I am trying to pass some image and video to paper class but I want to update one class at one time. When I am calling notifyListeners(); it's updating all class but I want to make separate Model for the different class. eg: If I want to update ImgeScalling then notifyListeners(); update only ImgeScalling .

 Widget build(BuildContext context) {
    return  ScopedModelDescendant<ActivityModel>(
            builder: (context, child, model) => Stack(
      children: <Widget>[
        ImageTemplate(),
        Drawing(model.controller),
        ImageScaling(imagePath:model.getImagePath),
        // TODO: Undo and Clear button added fo temp , later need to remove
        Align(
          alignment: Alignment.bottomRight,
          heightFactor: 100.0,
          child:
              Row(mainAxisAlignment: MainAxisAlignment.end, children: <Widget>[
            IconButton(
                icon: Icon(Icons.undo),
                iconSize: 40.0,
                color: Colors.red,
                onPressed: () => model.controller.undo()),
            IconButton(
                icon: Icon(Icons.clear),
                iconSize: 40.0,
                color: Colors.red,
                onPressed: () => model.controller.clear()),
          ]),
        )
      ],
    ));
  }

return ScopedModelDescendant<ActivityModel>(
        builder: (context, child, model) => PopupGridView(
              side: side,
              onUserPress: (text) {
                print(text);
                switch (text) {
                  // TODO: later change static image base code into index base
                  case 'assets/stickers/drawing/pencil.png':
                    model.controller.thickness = 5.0;
                    break;
                  case 'assets/stickers/drawing/brush.png':
                    model.controller.thickness = 10.0;
                    break;
                  case 'assets/stickers/drawing/brush1.png':
                    model.controller.thickness = 20.0;
                    break;
                  case 'assets/stickers/mic/stop.png':
                    if (!recorder.isRecording) {
                      recorder.start();
                    } else {
                      recorder.stop();
                    }
                    break;
                  case 'assets/stickers/mic/play.png':
                    if (recorder.isRecorded) {
                      recorder.playAudio();
                    } else {
                      recorder.stopAudio();
                    }
                    break;
                    case 'assets/stickers/camera/camera1.png':
                    new Camera().openCamera().then((p) {
                      if(p!=null)
                      model.setImagePath(p);
                    });
                    break;
                  case 'assets/stickers/camera/gallery.png':
                    new Camera().pickImage().then((p) {
                      if(p!=null)
                      model.setImagePath(p);
                    });
                    break;
                    case  'assets/stickers/camera/video.png':
                    new Camera().vidoeRecorder().then((p){
                      //model.setVideoPath(p);
                    });
                    break;
                }
              },
              bottomItems: bottomStickers,
              topItems: topStickers,
              itemCrossAxisCount: 2,
              buildItem: buildItem,
              buildIndexItem: buildIndexItem,
            ));

class ActivityModel extends Model {

  PainterController _controller;

  ActivityModel() {
    _controller = new PainterController();
  }

  PainterController get controller => this._controller;
  List<String> _imagePath=[];
  File _videoPath;
  List<String> get getImagePath => _imagePath;
  File get getVideoPath => _videoPath;
  void setImagePath(String str) {
    _imagePath.add(str);
    print('list of images::$_imagePath');
    notifyListeners();
  }

  void setVideoPath(File str) {
    _videoPath=str;
    notifyListeners();
  }
}

It seems that you're using scoped_model plugin. You may want to consider creating separate Models to be used for Widgets that you need to rebuild individually, depending on your use case. Calling notifyListeners() of the specific Model will notify all listeners of the Model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM