-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_goal_screen.dart
148 lines (138 loc) · 4.42 KB
/
add_goal_screen.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import 'package:flutter/material.dart';
import 'package:futsta/screens/select_player_screen.dart';
import 'package:futsta/utils/helpers.dart';
import '../data/models/matches.dart';
import '../data/models/players.dart';
import '../data/repositories/goals.dart';
class AddGoalScreen extends StatefulWidget {
final List<Player> players;
final FutsalMatch match;
final int nrGoals;
const AddGoalScreen(
{Key? key,
required this.players,
required this.match,
required this.nrGoals})
: super(key: key);
@override
AddGoalScreenState createState() => AddGoalScreenState();
}
class AddGoalScreenState extends State<AddGoalScreen> {
static Player defaultPlayer = Player("Select player");
final GoalRepository goalRepository = GoalRepository();
Player? _scoredBy = Player("Select player");
Player? _assistedBy;
Future<void> setScoredBy(Player? player) async {
setState(() {
_scoredBy = player;
});
}
Future<void> setAssistedBy(Player? player) async {
setState(() {
_assistedBy = player;
});
}
@override
Widget build(BuildContext context) {
String nextOrdinalGoal = toOrdinal(widget.nrGoals + 1);
return Scaffold(
appBar: AppBar(
title: const Text('Add goal'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Who scored the $nextOrdinalGoal goal?',
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
const SizedBox(height: 16),
_playerButton(setScoredBy, _scoredBy),
const SizedBox(height: 16),
_opponentButton(setScoredBy, _scoredBy, "Opponent", Colors.red),
const SizedBox(height: 32),
const Text(
'Who assisted the goal?',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
const SizedBox(height: 16),
_playerButton(setAssistedBy, _assistedBy),
const SizedBox(height: 16),
_opponentButton(
setAssistedBy, _assistedBy, "No assist", Colors.blueGrey),
const SizedBox(height: 64),
ElevatedButton(
onPressed: () async {
if (_scoredBy == defaultPlayer) {
showError(
Exception("Please select a player or select 'Opponnent'"),
context);
return;
}
try {
await goalRepository.add(
match: widget.match,
scoredBy: _scoredBy,
assistedBy: _assistedBy);
} on Exception catch (error) {
showError(error, context);
return;
}
// ignore: use_build_context_synchronously
Navigator.of(context).pop(true);
},
child: const Text(
"SAVE",
style: TextStyle(fontSize: 20),
),
),
],
),
),
);
}
Widget _opponentButton(Future<void> Function(Player?) selectPlayer,
Player? currentSelectedPlayer, String buttonText, Color selectionColor) {
final isSelected = currentSelectedPlayer == null;
return ElevatedButton(
onPressed: () {
selectPlayer(null);
},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
isSelected ? selectionColor : Colors.grey,
),
),
child: Text(buttonText),
);
}
Widget _playerButton(Future<void> Function(Player?) selectPlayer,
Player? currentSelectedPlayer) {
final isSelected = currentSelectedPlayer == defaultPlayer ||
widget.players.contains(currentSelectedPlayer);
return ElevatedButton(
onPressed: () async {
final Player? result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SelectPlayerScreen(players: widget.players),
),
);
selectPlayer(result);
},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
isSelected ? Colors.green : Colors.grey,
),
),
child: Text(currentSelectedPlayer?.name ?? "Select player"),
);
}
}