mirror of https://github.com/flutter/pinball.git
parent
0dc8851e3b
commit
3d9bc99c2a
@ -0,0 +1,59 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:pinball_theme/pinball_theme.dart';
|
||||
|
||||
part 'leaderboard_event.dart';
|
||||
part 'leaderboard_state.dart';
|
||||
|
||||
class LeaderboardBloc extends Bloc<LeaderboardEvent, LeaderboardState> {
|
||||
LeaderboardBloc() : super(const LeaderboardState()) {
|
||||
on<LeaderboardRequested>(_onLeaderboardRequested);
|
||||
}
|
||||
|
||||
FutureOr<void> _onLeaderboardRequested(
|
||||
LeaderboardRequested event,
|
||||
Emitter<LeaderboardState> emit,
|
||||
) {
|
||||
emit(state.copyWith(status: LeaderboardStatus.loading));
|
||||
try {
|
||||
const ranking = <Competitor>[
|
||||
Competitor(
|
||||
rank: 1,
|
||||
characterTheme: DashTheme(),
|
||||
initials: 'ABC',
|
||||
score: 100,
|
||||
),
|
||||
Competitor(
|
||||
rank: 2,
|
||||
characterTheme: SparkyTheme(),
|
||||
initials: 'DEF',
|
||||
score: 200,
|
||||
),
|
||||
Competitor(
|
||||
rank: 3,
|
||||
characterTheme: AndroidTheme(),
|
||||
initials: 'GHI',
|
||||
score: 300,
|
||||
),
|
||||
Competitor(
|
||||
rank: 4,
|
||||
characterTheme: DinoTheme(),
|
||||
initials: 'JKL',
|
||||
score: 400,
|
||||
),
|
||||
];
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: LeaderboardStatus.success,
|
||||
ranking: ranking,
|
||||
),
|
||||
);
|
||||
} catch (error, _) {
|
||||
emit(state.copyWith(status: LeaderboardStatus.error));
|
||||
addError(error);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
part of 'leaderboard_bloc.dart';
|
||||
|
||||
abstract class LeaderboardEvent extends Equatable {
|
||||
const LeaderboardEvent();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class LeaderboardRequested extends LeaderboardEvent {}
|
@ -0,0 +1,43 @@
|
||||
part of 'leaderboard_bloc.dart';
|
||||
|
||||
enum LeaderboardStatus { loading, success, error }
|
||||
|
||||
class LeaderboardState extends Equatable {
|
||||
const LeaderboardState({
|
||||
this.status = LeaderboardStatus.loading,
|
||||
this.ranking = const [],
|
||||
});
|
||||
|
||||
final LeaderboardStatus status;
|
||||
final List<Competitor> ranking;
|
||||
|
||||
@override
|
||||
List<Object> get props => [status, ranking];
|
||||
|
||||
LeaderboardState copyWith({
|
||||
LeaderboardStatus? status,
|
||||
List<Competitor>? ranking,
|
||||
}) {
|
||||
return LeaderboardState(
|
||||
status: status ?? this.status,
|
||||
ranking: ranking ?? this.ranking,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Competitor extends Equatable {
|
||||
const Competitor({
|
||||
required this.rank,
|
||||
required this.characterTheme,
|
||||
required this.initials,
|
||||
required this.score,
|
||||
});
|
||||
|
||||
final int rank;
|
||||
final CharacterTheme characterTheme;
|
||||
final String initials;
|
||||
final int score;
|
||||
|
||||
@override
|
||||
List<Object?> get props => [rank, characterTheme, initials, score];
|
||||
}
|
@ -0,0 +1 @@
|
||||
export 'bloc/leaderboard_bloc.dart';
|
Loading…
Reference in new issue