1
0
Fork 0
mirror of https://github.com/Nioux/AideDeJeu.git synced 2025-10-29 06:26:02 +00:00
This commit is contained in:
Yan Maniez 2020-03-12 15:57:57 +01:00
parent 0bb1ce43b1
commit b758deab1a
4 changed files with 383 additions and 76 deletions

View file

@ -0,0 +1,228 @@
import 'package:aidedejeu_flutter/database.dart';
import 'package:aidedejeu_flutter/models/items.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/cupertino.dart';
class PlayerCharacterState {
final BuildContext context;
PlayerCharacterState({
this.context,
this.race,
this.races,
this.subRace,
this.subRaces,
this.background,
this.backgrounds,
this.subBackground,
this.subBackgrounds,
}) {}
PlayerCharacterState copyWith({
BuildContext context,
RaceItem race,
List<RaceItem> races,
SubRaceItem subRace,
List<SubRaceItem> subRaces,
BackgroundItem background,
List<BackgroundItem> backgrounds,
SubBackgroundItem subBackground,
List<SubBackgroundItem> subBackgrounds,
}) {
return PlayerCharacterState(
context: context ?? this.context,
race: race ?? this.race,
races: races ?? this.races,
subRace: subRace ?? this.subRace,
subRaces: subRaces ?? this.subRaces,
background: background ?? this.background,
backgrounds: backgrounds ?? this.backgrounds,
subBackground: subBackground ?? this.subBackground,
subBackgrounds: subBackgrounds ?? this.subBackgrounds,
);
}
RaceItem race;
SubRaceItem subRace;
List<RaceItem> races;
List<SubRaceItem> subRaces;
BackgroundItem background;
SubBackgroundItem subBackground;
List<BackgroundItem> backgrounds;
List<SubBackgroundItem> subBackgrounds;
@override
List<Object> get props => [
race,
subRace,
races,
subRaces,
background,
subBackground,
backgrounds,
subBackgrounds
];
Future<void> initRaces() async {
print("initRaces");
var races = await loadRaces(context);
this.races = races.toList();
}
Future<void> initSubRaces(RaceItem race) async {
print("initSubRaces");
var subRaces = await loadSubRaces(context, race);
this.subRaces = subRaces;
}
Future<void> initBackgrounds() async {
print("initBackgrounds");
var backgrounds = await loadBackgrounds(context);
this.backgrounds = backgrounds;
}
Future<void> initSubBackgrounds(BackgroundItem background) async {
print("initSubBackgrounds");
var subBackgrounds = await loadSubBackgrounds(context, background);
this.subBackgrounds = subBackgrounds;
}
// setters
Future<void> setRace(RaceItem race) async {
this.race = race;
this.subRace = null;
this.subRaces = null;
await initSubRaces(race);
}
Future<void> setSubRace(SubRaceItem subRace) async {
this.subRace = subRace;
}
Future<void> setBackground(BackgroundItem background) async {
this.background = background;
this.subBackground = null;
this.subBackgrounds = null;
await initSubBackgrounds(background);
}
Future<void> setSubBackground(SubBackgroundItem subBackground) async {
this.subBackground = subBackground;
}
}
abstract class PlayerCharacterEvent extends Equatable {}
/*
class RacesEvent extends PlayerCharacterEvent {
List<RaceItem> races;
@override
List<Object> get props => [races];
}
*/
class RaceEvent extends PlayerCharacterEvent {
RaceItem race;
@override
List<Object> get props => [race];
RaceEvent(RaceItem race) {
this.race = race;
}
}
class SubRaceEvent extends PlayerCharacterEvent {
SubRaceItem subRace;
@override
List<Object> get props => [subRace];
SubRaceEvent(SubRaceItem subRace) {
this.subRace = subRace;
}
}
class SetItemEvent<T> extends PlayerCharacterEvent {
T item;
@override
List<Object> get props => [item];
SetItemEvent(T item) {
this.item = item;
}
}
class BackgroundEvent extends SetItemEvent<BackgroundItem> {
BackgroundEvent(BackgroundItem item) : super(item);
}
class SubBackgroundEvent extends SetItemEvent<SubBackgroundItem> {
SubBackgroundEvent(SubBackgroundItem item) : super(item);
}
class LoadEvent extends PlayerCharacterEvent {
@override
List<Object> get props => [];
}
//enum PlayerCharacterEvent { setRace, setSubRace, }
class PlayerCharacterBloc
extends Bloc<PlayerCharacterEvent, PlayerCharacterState> {
BuildContext context;
PlayerCharacterBloc(BuildContext context) {
this.context = context;
}
@override
PlayerCharacterState get initialState => PlayerCharacterState(context: context);
@override
Stream<PlayerCharacterState> mapEventToState(
PlayerCharacterEvent event) async* {
if (event is RaceEvent) {
yield* _mapRaceEventToState(event);
} else if (event is SubRaceEvent) {
yield* _mapSubRaceEventToState(event);
} else if (event is BackgroundEvent) {
yield* _mapBackgroundEventToState(event);
} else if (event is SubBackgroundEvent) {
yield* _mapSubBackgroundEventToState(event);
} else if (event is LoadEvent) {
final currentState = state.copyWith(); // state;
await currentState.initRaces();
await currentState.initBackgrounds();
yield currentState;
}
}
Stream<PlayerCharacterState> _mapRaceEventToState(
RaceEvent event) async* {
final currentState = state.copyWith();
await currentState.setRace(event.race);
yield currentState;
}
Stream<PlayerCharacterState> _mapSubRaceEventToState(
SubRaceEvent event) async* {
final currentState = state.copyWith();
await currentState.setSubRace(event.subRace);
yield currentState;
}
Stream<PlayerCharacterState> _mapBackgroundEventToState(
BackgroundEvent event) async* {
final currentState = state.copyWith();
await currentState.setBackground(event.item);
yield currentState;
}
Stream<PlayerCharacterState> _mapSubBackgroundEventToState(
SubBackgroundEvent event) async* {
final currentState = state.copyWith();
await currentState.setSubBackground(event.item);
yield currentState;
}
}

View file

@ -1,21 +1,24 @@
import 'package:aidedejeu_flutter/blocs/player_character/player_character_bloc.dart';
import 'package:aidedejeu_flutter/database.dart';
import 'package:aidedejeu_flutter/localization.dart';
import 'package:aidedejeu_flutter/models/items.dart';
import 'package:aidedejeu_flutter/theme.dart';
import 'package:aidedejeu_flutter/widgets/library.dart';
import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:equatable/equatable.dart';
class PCEditorPage extends StatefulWidget {
PCEditorPage({Key key}) : super(key: key);
class PCEditorPage extends StatelessWidget {
// StatefulWidget {
/* PCEditorPage({Key key}) : super(key: key);
@override
State<StatefulWidget> createState() => _PCEditorPageState();
}
class _PCEditorPageState extends State<PCEditorPage> {
MarkdownStyleSheet styleSheet;
}*/
/*
class PCEditorViewModel {
RaceItem _race;
SubRaceItem _subRace;
List<RaceItem> _races;
@ -26,55 +29,45 @@ class _PCEditorPageState extends State<PCEditorPage> {
List<BackgroundItem> _backgrounds;
List<SubBackgroundItem> _subBackgrounds;
// inits
@override
void initState() {
super.initState();
_initRaces();
_initBackgrounds();
}
@protected
@mustCallSuper
void didChangeDependencies() {
super.didChangeDependencies();
styleSheet = mainMarkdownStyleSheet(context);
_PCEditorPageState state;
BuildContext context;
PCEditorViewModel(_PCEditorPageState state, BuildContext context) {
this.state = state;
this.context = context;
}
void _initRaces() async {
var races = await loadRaces(context);
setState(() {
state.setState(() {
_races = races.toList();
});
}
void _initSubRaces(RaceItem race) async {
var subRaces = await loadSubRaces(context, race);
setState(() {
state.setState(() {
_subRaces = subRaces;
});
}
void _initBackgrounds() async {
var backgrounds = await loadBackgrounds(context);
setState(() {
state.setState(() {
_backgrounds = backgrounds;
});
}
void _initSubBackgrounds(BackgroundItem background) async {
var subBackgrounds = await loadSubBackgrounds(context, background);
setState(() {
_subBackgrounds =
subBackgrounds;
state.setState(() {
_subBackgrounds = subBackgrounds;
});
}
// setters
void _setRace(RaceItem race) {
setState(() {
state.setState(() {
this._race = race;
this._subRace = null;
this._subRaces = null;
@ -83,13 +76,13 @@ class _PCEditorPageState extends State<PCEditorPage> {
}
void _setSubRace(SubRaceItem subRace) {
setState(() {
state.setState(() {
this._subRace = subRace;
});
}
void _setBackground(BackgroundItem background) {
setState(() {
state.setState(() {
this._background = background;
this._subBackground = null;
this._subBackgrounds = null;
@ -98,14 +91,41 @@ class _PCEditorPageState extends State<PCEditorPage> {
}
void _setSubBackground(SubBackgroundItem subBackground) {
setState(() {
state.setState(() {
this._subBackground = subBackground;
});
}
}
*/
//class _PCEditorPageState extends State<PCEditorPage> {
MarkdownStyleSheet styleSheet;
//PCEditorViewModel vm;
// inits
/*
@override
void initState() {
super.initState();
//vm = PCEditorViewModel(this, context);
//vm._initRaces();
//vm._initBackgrounds();
}
@protected
@mustCallSuper
void didChangeDependencies() {
super.didChangeDependencies();
styleSheet = mainMarkdownStyleSheet(context);
}
*/
// widgets generics
Widget _buildMarkdown(String markdown) {
Widget _buildMarkdown(
BuildContext context, PlayerCharacterState state, String markdown) {
return MarkdownBody(
data: markdown ?? "",
styleSheet: styleSheet,
@ -116,7 +136,8 @@ class _PCEditorPageState extends State<PCEditorPage> {
);
}
Widget _buildSubTitle(String title) {
Widget _buildSubTitle(
BuildContext context, PlayerCharacterState state, String title) {
return Text(title,
style: TextStyle(
fontSize: 16,
@ -125,6 +146,7 @@ class _PCEditorPageState extends State<PCEditorPage> {
}
Widget _buildItemsWidget<T extends Item>(
BuildContext context, PlayerCharacterState state,
{String hintText,
List<T> items,
T selectedItem,
@ -148,80 +170,111 @@ class _PCEditorPageState extends State<PCEditorPage> {
// widgets specifics
Widget _buildRacesWidget() {
Widget _buildRacesWidget(BuildContext context, PlayerCharacterState state) {
return _buildItemsWidget<RaceItem>(
context,
state,
hintText: "Race",
items: _races,
selectedItem: _race,
items: state.races,
selectedItem: state.race,
onChanged: (value) {
_setRace(value);
//state.setRace(value);
BlocProvider.of<PlayerCharacterBloc>(context).add(RaceEvent(value));
},
);
}
Widget _buildSubRacesWidget() {
return _subRaces != null
Widget _buildSubRacesWidget(
BuildContext context, PlayerCharacterState state) {
return state.subRaces != null
? _buildItemsWidget<SubRaceItem>(
context,
state,
hintText: "Variante",
items: _subRaces,
selectedItem: _subRace,
items: state.subRaces,
selectedItem: state.subRace,
onChanged: (value) {
_setSubRace(value);
//state.setSubRace(value);
BlocProvider.of<PlayerCharacterBloc>(context)
.add(SubRaceEvent(value));
},
)
: SizedBox.shrink();
}
Widget _buildRaceDetailsWidget() {
return _race != null
Widget _buildRaceDetailsWidget(
BuildContext context, PlayerCharacterState state) {
return state.race != null
? Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildSubTitle(AppLocalizations.of(context).raceAbilityScoreIncrease),
_buildMarkdown(_race?.abilityScoreIncrease),
_buildMarkdown(_subRace?.abilityScoreIncrease),
_buildSubTitle(context, state,
AppLocalizations.of(context).raceAbilityScoreIncrease),
_buildMarkdown(context, state, state.race?.abilityScoreIncrease),
_buildMarkdown(
context, state, state.subRace?.abilityScoreIncrease),
Text(""),
_buildSubTitle(AppLocalizations.of(context).raceAge),
_buildMarkdown(_race?.age),
_buildSubTitle(
context, state, AppLocalizations.of(context).raceAge),
_buildMarkdown(context, state, state.race?.age),
Text(""),
_buildSubTitle(AppLocalizations.of(context).raceAlignment),
_buildMarkdown(_race?.alignment),
_buildSubTitle(
context, state, AppLocalizations.of(context).raceAlignment),
_buildMarkdown(context, state, state.race?.alignment),
Text(""),
_buildSubTitle(AppLocalizations.of(context).raceSize),
_buildMarkdown(_race?.size),
_buildSubTitle(
context, state, AppLocalizations.of(context).raceSize),
_buildMarkdown(context, state, state.race?.size),
Text(""),
_buildSubTitle(AppLocalizations.of(context).raceSpeed),
_buildMarkdown(_race?.speed),
_race?.darkvision != null ? Text("") : SizedBox.shrink(),
_race?.darkvision != null ? _buildSubTitle(AppLocalizations.of(context).raceDarkvision) : SizedBox.shrink(),
_race?.darkvision != null ? _buildMarkdown(_race?.darkvision) : SizedBox.shrink(),
_buildSubTitle(
context, state, AppLocalizations.of(context).raceSpeed),
_buildMarkdown(context, state, state.race?.speed),
state.race?.darkvision != null ? Text("") : SizedBox.shrink(),
state.race?.darkvision != null
? _buildSubTitle(context, state,
AppLocalizations.of(context).raceDarkvision)
: SizedBox.shrink(),
state.race?.darkvision != null
? _buildMarkdown(context, state, state.race?.darkvision)
: SizedBox.shrink(),
Text(""),
_buildSubTitle(AppLocalizations.of(context).raceLanguages),
_buildMarkdown(_race?.languages),
_buildSubTitle(
context, state, AppLocalizations.of(context).raceLanguages),
_buildMarkdown(context, state, state.race?.languages),
],
)
: SizedBox.shrink();
}
Widget _buildBackgroundsWidget() {
Widget _buildBackgroundsWidget(
BuildContext context, PlayerCharacterState state) {
return _buildItemsWidget<BackgroundItem>(
context,
state,
hintText: "Historique",
items: _backgrounds,
selectedItem: _background,
items: state.backgrounds,
selectedItem: state.background,
onChanged: (value) {
_setBackground(value);
//state.setBackground(value);
BlocProvider.of<PlayerCharacterBloc>(context)
.add(BackgroundEvent(value));
},
);
}
Widget _buildSubBackgroundsWidget() {
return _subBackgrounds != null
Widget _buildSubBackgroundsWidget(
BuildContext context, PlayerCharacterState state) {
return state.subBackgrounds != null
? _buildItemsWidget<SubBackgroundItem>(
context,
state,
hintText: "Variante",
items: _subBackgrounds,
selectedItem: _subBackground,
items: state.subBackgrounds,
selectedItem: state.subBackground,
onChanged: (value) {
_setSubBackground(value);
//state.setSubBackground(value);
BlocProvider.of<PlayerCharacterBloc>(context)
.add(SubBackgroundEvent(value));
},
)
: SizedBox.shrink();
@ -229,6 +282,26 @@ class _PCEditorPageState extends State<PCEditorPage> {
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<PlayerCharacterBloc>(
create: (context) {
return PlayerCharacterBloc(context)
..add(
LoadEvent(),
);
},
),
],
child: BlocBuilder<PlayerCharacterBloc, PlayerCharacterState>(
builder: (context, state) {
return buildUI(context, state);
},
),
);
}
Widget buildUI(BuildContext context, PlayerCharacterState state) {
return DefaultTabController(
length: 5,
child: Scaffold(
@ -285,9 +358,9 @@ class _PCEditorPageState extends State<PCEditorPage> {
margin: EdgeInsets.all(10.0),
child: ListView(
children: <Widget>[
_buildRacesWidget(),
_buildSubRacesWidget(),
_buildRaceDetailsWidget(),
_buildRacesWidget(context, state),
_buildSubRacesWidget(context, state),
_buildRaceDetailsWidget(context, state),
],
),
),
@ -295,8 +368,8 @@ class _PCEditorPageState extends State<PCEditorPage> {
margin: EdgeInsets.all(10.0),
child: ListView(
children: <Widget>[
_buildBackgroundsWidget(),
_buildSubBackgroundsWidget(),
_buildBackgroundsWidget(context, state),
_buildSubBackgroundsWidget(context, state),
],
),
),

View file

@ -99,6 +99,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.3"
equatable:
dependency: "direct main"
description:
name: equatable
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
flutter:
dependency: "direct main"
description: flutter

View file

@ -30,9 +30,8 @@ dependencies:
flutter_svg:
bloc:
flutter_bloc:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
equatable: ^1.0.0
dev_dependencies:
flutter_test: