首页 > 解决方案 > Flutter-如何制作多个 notifyListeners()?

问题描述

在这里,我试图将一些图像和视频传递给纸质课程,但我想一次更新一门课程。当我调用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();
  }
}

标签: androiddartflutter

解决方案


推荐阅读