flutter 如何在Bloc中更改状态?

5ktev3wc  于 2023-01-10  发布在  Flutter
关注(0)|答案(2)|浏览(161)

我正在学习Bloc,遇到了问题
如何从加载状态变为加载状态?
我的代码:
test. dart寻求大家的帮助

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:helloapp/profile/bloc/popup/popup_bloc.dart';

class TestScreen extends StatelessWidget {
  const TestScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) => PopupBloc(),
      child: BlocBuilder<PopupBloc, PopupState>(
        builder: (context, state) {
          if (state is PopupLoadingState) {
            return Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Container(
                  margin: EdgeInsets.symmetric(vertical: 50),
                    child: CircularProgressIndicator()
                ),
              ],
            );
          }
          if (state is PopupLoadedState) {
            return Text(state.avatar + state.phone + state.name);
          }
          return Text('123');
        },
      ),
    );
  }
}

popup_bloc. dart向所有人寻求帮助

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';

part 'popup_event.dart';
part 'popup_state.dart';

class PopupBloc extends Bloc<PopupEvent, PopupState> {
  PopupBloc() : super(PopupLoadingState()) {
    on<PopupEvent>((event, emit) {
      emit(PopupLoadingState());
      emit(PopupLoadedState(avatar: '123', name: 'name', phone: 'phone'));
    });
  }
}

弹出事件. dart

part of 'popup_bloc.dart';

abstract class PopupEvent extends Equatable {
  const PopupEvent();

  @override
  List<Object?> get props => [];
}

class PopupLoadingEvent extends PopupEvent{}

class PopupLoadedEvent extends PopupEvent{}

class PopupErrorEvent extends PopupEvent{}

popup_state. dart向所有人寻求帮助

part of 'popup_bloc.dart';

abstract class PopupState extends Equatable {
  const PopupState();
}

class PopupLoadingState extends PopupState {
  @override
  // TODO: implement props
  List<Object?> get props => [];

}

class PopupLoadedState extends PopupState {
  final String avatar;
  final String name;
  final String phone;

  PopupLoadedState({
    required this.avatar,
    required this.name,
    required this.phone
  });

  @override
  // TODO: implement props
  List<Object?> get props => [avatar, name, phone];

}

class PopupErrorState extends PopupState {
  @override
  // TODO: implement props
  List<Object?> get props => [];

}
tgabmvqs

tgabmvqs1#

试试这个

return BlocProvider(
          create: (_) => PopupBloc()..add(PopupEvent.PopupLoadingEvent())..add(PopupEvent.PopupLoadedEvent()),
         ...
    );
xzlaal3s

xzlaal3s2#

应该为特定事件注册事件处理程序

class PopupBloc extends Bloc<PopupEvent, PopupState> {
  PopupBloc() : super(PopupLoadingState()) {
    on<PopupLoadingEvent>((event, emit) async {
      emit(PopupLoadingState());
      /// ... do some async operation and finally emit a loaded or error state
      emit(PopupLoadedState(avatar: '123', name: 'name', phone: 'phone'));
    });
  }
}

在集团成立时触发这个

return BlocProvider(
      create: (_) => PopupBloc()..add(PopupLoadingEvent()),
     ...
);

相关问题