BlocBuilder не обновляется, если я перехожу на новую страницу и возвращаюсь на предыдущую страницу - PullRequest
0 голосов
/ 03 февраля 2020

Я создаю приложение, в котором есть значок уведомления. Пока что я создал значок отображения значка уведомления, и когда я щелкаю значок, значок меняется на «Icons.notifications_active». Но он только изменит или превратит значок уведомления в активный, если я перейду на новый экран и вернусь к предыдущему экрану, и если я добавлю setstate () в нажатой функции, он изменится на значок активного уведомления. Ниже приведен код

HomePage.dart

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();}
class _HomePageState extends State<HomePage> {
NotificationBloc notificationBloc;
  @override
  void initState() {
    super.initState();
    notificationBloc = BlocProvider.of<NotificationBloc>(context);}
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
Column(
                                  children: <Widget>[
                                    BlocListener<NotificationBloc,
                                        NotificationState>(
                                      listener: (context, state) {},
                                      child: BlocBuilder<NotificationBloc,
                                          NotificationState>(
                                        builder: (context, state) {
                                          if (state is InitialNotificationState) {
                                            return buildLoading();
                                          } else if (state is NotificationLoadedState) {
                                            return NotificationIconBuild(state.notification);
                                          } else if (state is NotificationErrorState) {
                                            return buildErrorUi(state.message1);}},),),],)]
  Widget NotificationIconBuild(NotificationModle notification) {
    return Column(
      children: <Widget>[
        notificationIcon("fajr", notification),],);}
  Widget notificationIcon(String notification, NotificationModle notificationModle){
        if(notificationModle.fajr == false)
          return IconButton(
            icon: Icon(Icons.notifications),
            onPressed: (){
              notificationModle.fajr = true;
                notificationBloc.add(SelectNotificationEvent(notificationModle));},);
        else return IconButton(
          icon: Icon(Icons.notifications_active),
          onPressed: (){
            notificationModle.fajr = false;
              notificationBloc.add(SelectNotificationEvent(notificationModle));},);

Notification_state.dart

class NotificationLoadedState extends NotificationState {
  NotificationModle notification;
  NotificationLoadedState({@required this.notification});
  @override
  List<Object> get props => [notification];
}

NotificationEvent .dart

class SelectNotificationEvent extends NotificationEvent {
  NotificationModle notificationModle;
  SelectNotificationEvent(this.notificationModle);
  @override
  List<Object> get props => null;
}

NotificationBlo c .dart

class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
  NotificationsRepositoryImp notificationsRepository;
  NotificationBloc({@required this.notificationsRepository});
  @override
  NotificationState get initialState => InitialNotificationState();
  @override
  Stream<NotificationState> mapEventToState(
    NotificationEvent event,
  ) async* {if(event is SelectNotificationEvent){
      await notificationsRepository.saveNotifications(event.notificationModle);}}}

NotificationModle.dart

class NotificationModle{
  bool fajr;
  NotificationModle(this.fajr);}

NotificationsRepository.dart

class NotificationsRepositoryImp{
  @override
  Future<void> saveNotifications(NotificationModle notification) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setBool('n_fajr', notification.fajr); }}
...