-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings_form.dart
120 lines (112 loc) · 4.13 KB
/
settings_form.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
import 'package:brew_crew/models/user.dart';
import 'package:brew_crew/services/database.dart';
import 'package:brew_crew/shared/loading.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class SettingsForm extends StatefulWidget {
@override
_SettingsFormState createState() => _SettingsFormState();
}
class _SettingsFormState extends State<SettingsForm> {
final _formKey = GlobalKey<FormState>();
final List<String> sugars = ['0', '1', '2', '3', '4'];
final List<int> strengths = [100, 200, 300, 400, 500, 600, 700, 800, 900];
String _currentName;
String _currentSugars;
int _currentStrength;
@override
Widget build(BuildContext context) {
final User user = Provider.of<User>(context);
return StreamBuilder<UserData>(
stream: DBService(uid: user.uid).userData,
builder: (context, snapshot) {
print('Snapshot has data: ${snapshot.hasData}');
if (snapshot.hasData) {
UserData userData = snapshot.data;
_currentName = userData.name;
_currentSugars = userData.sugars;
_currentStrength = userData.strength;
return Form(
key: _formKey,
child: Column(
children: <Widget>[
Text(
'Update your Brew Preferences',
style: TextStyle(fontSize: 18.0),
),
SizedBox(
height: 20.0,
),
TextFormField(
initialValue: userData.name,
decoration: InputDecoration(),
validator: (val) =>
val.isEmpty ? 'Please enter a name' : null,
onChanged: (val) {
setState(() {
_currentName = val;
});
},
),
SizedBox(
height: 20.0,
),
//dropdown
DropdownButtonFormField(
isExpanded: true,
value: _currentSugars, // ?? userData.sugars,
items: sugars.map((sugar) {
return DropdownMenuItem(
value: sugar,
child: Text('$sugar sugars'),
);
}).toList(),
onChanged: (val) {
setState(() {
_currentSugars = val;
});
},
),
//slider
Slider(
value: (_currentStrength ?? 200)
.toDouble(), // ?? userData.strength
min: 100,
divisions: 8,
max: 900,
activeColor: Colors.brown[_currentStrength ?? 100],
inactiveColor: Colors.brown,
onChanged: (val) {
setState(() {
_currentStrength = val.toInt();
});
},
),
SizedBox(
height: 20.0,
),
RaisedButton(
color: Colors.indigo,
child: Text(
'Update',
style: TextStyle(color: Colors.white),
),
onPressed: () async {
if (_formKey.currentState.validate()) {
await DBService(uid: user.uid).updateUserData(
_currentSugars, _currentName, _currentStrength);
}
print(_currentName);
print(_currentStrength);
print(_currentSugars);
},
)
],
),
);
} else {
return Loading();
}
});
}
}