Невозможно изменить состояние в FlareActor и блоке флаттера - PullRequest
0 голосов
/ 30 мая 2020

Итак, проблема в том, что у меня необычное поведение с blo c, которое я сделал, я пытаюсь реализовать эту анимацию с помощью FlareActor, но проблема в том, что у меня есть три состояния, которые NightState, DayState и SwitchingState, но по какой-то причине состояние всегда возвращается в DayState, и, к сожалению, обратный вызов FlareActor запускается только в DayState, я что-то упустил?

State class :

import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.dart';

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

class InitialRiveActorState extends RiveActorState {
  @override
  List<Object> get props => [];
}

class NightState extends RiveActorState {
  final String animationName;

  NightState({@required this.animationName});

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

class DayState extends RiveActorState {
  final String animationName;

  DayState({@required this.animationName});

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

class SwitchingState extends RiveActorState {
final String animationName;

SwitchingState({@required this.animationName});

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

Blo c класс:

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

class RiveActorBloc extends Bloc<RiveActorEvent, RiveActorState> {
 @override
 RiveActorState get initialState => InitialRiveActorState();

 @override
Stream<RiveActorState> mapEventToState(
RiveActorEvent event,
) async* {
if (event is SwitchingEvent) {
  print('switch state is yielded !');

  yield SwitchingState(animationName: event.animationName);
} else if (event is NightEvent) {
  print('night state is yielded !');

  yield NightState(animationName: event.animationName);
} else if (event is DayEvent) {
  print('day state is yielded !');
  yield DayState(animationName: event.animationName);
 }
}
}

Класс события:

    import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.dart';

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

class SwitchingEvent extends RiveActorEvent {
  final String animationName;

  SwitchingEvent({@required this.animationName});
  @override
  List<Object> get props => [];
}

class NightEvent extends RiveActorEvent {
  final String animationName;

  NightEvent({@required this.animationName});
  @override
  List<Object> get props => [];
}

class DayEvent extends RiveActorEvent {
  final String animationName;

  DayEvent({@required this.animationName});
  @override
  List<Object> get props => [];
}

Код пользовательского интерфейса:

CustomListTile(
                  /// TODO : Make the switch work
                  padding: EdgeInsets.symmetric(horizontal: 10),
                  onTap: () {
                    print('button is tapped');
                    final currentState = BlocProvider.of<RiveActorBloc>(ctx).state;
                    print('current state is $currentState');
                    if (currentState is NightState) {
                      BlocProvider.of<RiveActorBloc>(ctx)
                          .add(SwitchingEvent(animationName: switchDay));
                    } else if (currentState is DayState) {
                      BlocProvider.of<RiveActorBloc>(ctx)
                          .add(SwitchingEvent(animationName: switchNight));
                    }
                  },
                  trailingWidget:
                      BlocBuilder<RiveActorBloc, RiveActorState>(builder: (ctx, state) {
                    if (state is DayState) {
                      return Container(
                        height: 50,
                        width: 50,
                        child: FlareActor(
                          'assets/rive/switch.flr',
                          animation: state.animationName,
                          callback: (string) {
                            print('day state is hereeee ${state.animationName}');
                          },
                        ),
                      );
                    } else if (state is NightState) {
                      return Container(
                        height: 50,
                        width: 50,
                        child: FlareActor(
                          'assets/rive/switch.flr',
                          animation: state.animationName,
                          callback: (text) => print(text),
                        ),
                      );
                    } else if (state is SwitchingState) {
                      return Container(
                        height: 50,
                        width: 50,
                        child: FlareActor(
                          'assets/rive/switch.flr',
                          callback: (text) => print(text),
                          animation: state.animationName,
                        ),
                      );
                    } else {
                      return Container(
                        height: 0,
                        width: 0,
                      );
                    }
                  }),
                  leadingWidget:
                      Icon(Icons.brightness_medium, color: Theme.of(ctx).buttonColor),
                  title: 'Theme mode',
                ),

CustomListTile :

    import 'package:flutter/material.dart';

class CustomListTile extends StatelessWidget {
  final String title;
  final Widget leadingWidget;
  final Widget trailingWidget;
  final Function onTap;
  final EdgeInsetsGeometry padding;
  const CustomListTile(
      {@required this.title,
      @required this.padding,
      @required this.leadingWidget,
      @required this.trailingWidget,
      @required this.onTap});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
          padding: padding,
          margin: EdgeInsets.symmetric(horizontal: 5),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(25),
            color: Colors.white,
          ),
          child: Row(
            children: <Widget>[
              leadingWidget,
              SizedBox(
                width: 20,
              ),
              Text(title, style: Theme.of(context).textTheme.headline2),
              Spacer(),
              trailingWidget
            ],
          )),
    );
  }
}
...