在使用bloc listener和multi bloc listener时,需要注意以下几点:
1.在使用bloc listener时,必须使用SingleChildScrollView或ListView等可滚动的组件包裹整个布局,否则当键盘弹出时,UI界面的大小不会改变,导致键盘无法弹出。
2.在使用multi bloc listener时,使用add方法添加listener时,需要给每个listener添加tag,并且在页面消失时需要手动移除listener,否则会导致内存泄漏。
代码示例:
// bloc listener
SingleChildScrollView(
child: BlocListener
// multi bloc listener
class _MyPageState extends State
@override void initState() { super.initState(); bloc1.addListener(_blocListener1, tag: "bloc1"); bloc2.addListener(_blocListener2, tag: "bloc2"); }
@override void dispose() { bloc1.removeListenerByTag("bloc1"); bloc2.removeListenerByTag("bloc2"); super.dispose(); }
void _blocListener1() { // do something }
void _blocListener2() { // do something }
@override Widget build(BuildContext context) { return SingleChildScrollView( child: Column( children: [ // your widgets ], ), ); } }