簡體   English   中英

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

[英]Flutter- How to make multiple notifyListeners()?

在這里,我試圖將一些圖像和視頻傳遞給紙張 class,但我想一次更新一個 class。 當我打電話給notifyListeners(); 它正在更新所有 class 但我想為不同的 class 制作單獨的 Model。例如:如果我想更新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();
  }
}

看來您正在使用scoped_model插件。 您可能需要考慮創建單獨的模型以用於需要單獨重建的小部件,具體取決於您的用例。 調用特定Model的notifyListeners()會通知Model的所有監聽器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM