无法在FlareActor和颤振块中更改状态

时间:2020-05-30 10:41:28

标签: flutter dart flutter-animation

所以问题是我对制作的方块有异常的行为,我正在尝试使用FlareActor实现this动画,但问题是我有三个状态,分别是NightState,{ {1}}和DayState,但是由于某种原因,状态总是返回到DayState,但是不幸的是,仅在DayState中触发了FlareActor的回调,我是否丢失了某些内容?

状态类:

SwitchingState

集团类别:

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 => [];
}

事件类:

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);
 }
}
}

UI代码:

    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:

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',
                ),

0 个答案:

没有答案
相关问题