BlocBuilder不能根据蓝牙状态构建视图
创始人
2024-12-22 19:30:19
0

在BLoC中添加一个StreamController来处理蓝牙状态,然后在BLoC中使用StreamBuilder进行视图构建。以下是一个示例代码:

class MyBloc extends Bloc {
  final BluetoothController _bluetoothController = BluetoothController();

  final _bluetoothStateController = StreamController();

  Stream get bluetoothState => _bluetoothStateController.stream;

  @override
  Stream mapEventToState(SomeEvent event) async* {
    if (event is BluetoothChangedEvent) {
      final currentState = state;
      yield currentState.copyWith(
        bluetoothState: event.bluetoothState,
      );
      _bluetoothStateController.add(event.bluetoothState);
    }
  }

  @override
  Future close() {
    _bluetoothStateController?.close();
    return super.close();
  }
}

class BluetoothWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: context.watch().bluetoothState,
      builder: (context, snapshot) {
        final bluetoothState = snapshot.data;

        if (bluetoothState == BluetoothState.connected) {
          return Text('蓝牙已连接');
        } else if (bluetoothState == BluetoothState.disconnected) {
          return Text('蓝牙已断开');
        } else {
          return CircularProgressIndicator();
        }
      },
    );
  }
}

相关内容

热门资讯

透视脚本!wepoker可以免... 透视脚本!wepoker可以免费玩吗,广东插件智能插件安装包免费(总是是有挂)1、让任何用户在无需广...
透视ai代打!wpk有作弊吗,... 透视ai代打!wpk有作弊吗,欢聚水鱼智能辅助(原来存在有挂);1、很好的工具软件,可以解锁游戏的欢...
透视挂!wepokerplus... 透视挂!wepokerplus辅助,微信小程序辅助有哪些(原来是真的有挂)微信小程序辅助有哪些辅助器...
辅助透视!wepoker私局代... 辅助透视!wepoker私局代打,微信小程序微乐房间怎么开挂(本来是有挂)1、下载好微信小程序微乐房...
透视工具!wepoker私人局... 透视工具!wepoker私人局透视插件,约战沙城辅助脚本(总是真的有挂);1、每一步都需要思考,不同...
透视免费!哈糖大菠萝可以开挂吗... 透视免费!哈糖大菠萝可以开挂吗,九九联盟辅助在(竟然是真的有挂)1、哈糖大菠萝可以开挂吗系统规律教程...
透视脚本!wepoker底牌透... 透视脚本!wepoker底牌透视,朋友圈辅助软件(确实有挂)运朋友圈辅助软件辅助工具,进入游戏界面。...
透视好友房!aapoker怎么... 透视好友房!aapoker怎么设置抽水,微信小程序家乡大贰破解版(总是是有挂);1、起透看视 微信小...
透视插件!wepoker破解器... 透视插件!wepoker破解器,永胜联盟金花辅助(原来存在有挂)透视插件!wepoker破解器,永胜...
透视中牌率!德普之星透视辅助软... 透视中牌率!德普之星透视辅助软件激活码,jj斗地主外开挂(一贯是有挂);1、操作简单,无需注册,只需...