1

我是一个颤振初学者,我目前正在开发一个应用程序,它可以让我拍照,然后更新 ListView 以在下面显示带有一点文字描述的照片。两者都放置在卡片内。我已经设法让它正常工作。最近我深入研究了 BLoC 模式,并尝试在这个应用程序上实现它。问题是即使快照包含添加的数据,ListView 也不显示流中的内容。

这是我的主要内容:

class MyApp extends StatelessWidget {
  final LandscapeBloc _landscapeBloc = LandscapeBloc();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: <String,WidgetBuilder>{
        '/CameraPage': (BuildContext context)=> CameraScreen(cameras,_landscapeBloc),
      },
      initialRoute: '/',
      home: HomePage(_landscapeBloc)
    );
  }
} 

这是我的 HomePage 代码,其中包含暴露给 streamOutput 的 ListView

class HomePage extends StatefulWidget {
  final LandscapeBloc _landscapeBloc;

  @override
  _HomePageState createState() {
    return _HomePageState();
  }

  HomePage(this._landscapeBloc);
}

class _HomePageState extends State<HomePage> {
  List<LandscapeCard> list = [];



  @override
    void dispose() {
      widget._landscapeBloc.dispose();
      super.dispose();
    }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
            body: StreamBuilder(
            stream: widget._landscapeBloc.landscapeCardStream,
            initialData: list,
            builder: (BuildContext context,
                AsyncSnapshot<List<LandscapeCard>> snapshot) {
              print("[snaphot]" + snapshot.data.toString());
              return (Column(children: <Widget>[
                Flexible(
                  child: ListView.builder(
                      physics: const AlwaysScrollableScrollPhysics(),
                      itemCount: snapshot.data.length,
                      itemBuilder: (BuildContext context, int index) {
                        final LandscapeCard item = snapshot.data[index];
                        return Dismissible(
                            key: Key(item.landscapeImagePath),
                            onDismissed: (direction) {
                              widget._landscapeBloc.landscapeEvent
                                  .add(DeleteEvent(index));
                              Scaffold.of(context).showSnackBar(
                                  SnackBar(content: Text("item suprrimé")));
                            },
                            child: item);
                      },
                      padding: const EdgeInsets.all(10.0)),
                ),
              ]));
            }));
  }
  } 

我知道当我打印(快照)包含添加的照片的列表时,StreamBuilder 会收到数据。

我在这里输入数据;

class CardBuilder extends StatefulWidget {

  final String _path;
  final LandscapeBloc landscapeBloc;

  CardBuilder(this._path, this._landscapeBloc);

@override
  _CardBuilderState createState() {
    // TODO: implement createState
    return _CardBuilderState();
  }


}


class _CardBuilderState extends State<CardBuilder> {
  //TODO: stateful widget pour mis a jour UI quand on tape un texte


  final TextEditingController descController = TextEditingController();


  @override
    void dispose() {
      widget.landscapeBloc.dispose();
      descController.dispose();
      super.dispose();
    }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        body: SingleChildScrollView(
          child: Column(children: <Widget>[
            Align(
              alignment: Alignment.centerLeft,
              child: IconButton(
                  icon: Icon(
                    Icons.arrow_back,
                    color: Colors.black,
                  ),
                  iconSize: 35.00,
                  onPressed: widget._onPressedBack),
            ),
            DisplayMedia(widget._path),
            Divider(
              color: Colors.black,
              height: 5,
            ),
            TextField(
              autofocus: false,
              controller: descController,
              maxLines: 5,
              decoration: InputDecoration(
                  filled: false,
                  fillColor: Colors.black12,
                  border: InputBorder.none,
                  hintText: "put a description"),
            ),
            Divider(
              color: Colors.black,
              height: 5,
            ),
            IconButton(
                icon: Icon(Icons.check_circle, color: Colors.black),
                iconSize: 35.00,
                onPressed: () {
                  final LandscapeCard landscapeCard= LandscapeCard(descController.text,widget._path);
                  widget._landscapeBloc.landscapeEvent.add(AddEvent(landscapeCard));
                  Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(
                    builder: (BuildContext context) => HomePage(widget._landscapeBloc)
                  ),(Route route)=> route==null);
                })
          ]),
        ));
  }
}

这是集团:

class LandscapeBloc  {
  List<LandscapeCard> list = [];
  final StreamController _listLandscapeStateController = StreamController<List<LandscapeCard>>.broadcast();


  StreamSink<List<LandscapeCard>> get _landscapeSink => _listLandscapeStateController.sink;


  Stream<List<LandscapeCard>> get landscapeCardStream => _listLandscapeStateController.stream;

  final StreamController<ManagingListEvent> _listLandscapeEventController = StreamController<ManagingListEvent>.broadcast();


  StreamSink<ManagingListEvent> get landscapeEvent => _listLandscapeEventController.sink;

  LandscapeBloc(){
    _listLandscapeEventController.stream.listen(onManageEvent);
  }

  void onManageEvent(ManagingListEvent event){
    if(event is DeleteEvent){
      list.removeAt(event.indexDelete);
    }
    else{
      list.add(event.landscapeCardAdd);
    }
    _landscapeSink.add(list);
    print("[onManageEvent]"+event.landscapeCardAdd.landscapeImagePath+"   "+event.landscapeCardAdd.landscapeDesc);
  }

  void dispose(){
    _listLandscapeEventController.close();
    _listLandscapeStateController.close();
  }
} 

最后是 ManageListEvent 类

abstract class ManagingListEvent{
  int _indexS;
  LandscapeCard _landscapeCardS;;

  int get indexDelete => _indexS;

  LandscapeCard get landscapeCardAdd  => _landscapeCardS;
}

class DeleteEvent extends ManagingListEvent{

  DeleteEvent(int index){
    super._indexS=index;
  }
}

class AddEvent extends ManagingListEvent{  

  AddEvent(LandscapeCard landscape){
    super._landscapeCardS=landscape;
  }
} 

基本上我想导航到一个新页面,在那里我有我刚刚拍摄的照片,添加一些描述,将它放在接收器中,然后导航回主页。但是 ListView 不会使用新的小部件重新呈现,即使它通过快照接收它也是如此。提前感谢您的回答和您的时间

4

0 回答 0