Flutter-如何制作多个notifyListeners()?

时间:2018-08-06 15:41:22

标签: android dart flutter

在这里,我试图将一些图像和视频传递给纸上课,但是我想一次更新一堂课。当我打电话给notifyListeners();时,它正在更新 所有课程,但我想为不同的课程制作单独的模型。 例如:如果我要更新ImgeScalling,则notifyListeners();仅更新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();
  }
}

0 个答案:

没有答案