-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperience.dart
189 lines (183 loc) · 6.42 KB
/
experience.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:otter_library/NewExperience.dart';
import 'ExperienceClass.dart';
import 'ExperienceDetail.dart';
import 'styles.dart';
import 'main.dart';
import 'NewExperience.dart';
import 'auth.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
class experience extends StatefulWidget {
experience({Key key}) : super(key: key);
@override
_experienceState createState() => _experienceState();
}
class _experienceState extends State<experience> {
List<ExperienceClass> experienceList = [];
bool signedin = supabase.auth.currentUser != null;
// Get all the experiences of this user
Future<PostgrestResponse> getExperiences() async {
if (signedin) {
experienceList.clear();
String id = supabase.auth.currentUser.id;
final response = await supabase
.from('experiences')
.select()
.eq('userid', id)
.execute();
final data = response.data;
List<ExperienceClass> temp = [];
for (var i = 0; i < response.data.length; i++) {
// Convert the data to experienc objects
temp.add(new ExperienceClass(
response.data[i]['title'],
response.data[i]['desc'],
response.data[i]['id'].toString(),
response.data[i]['imageUrl'].toString()));
}
setState(() {
experienceList = temp;
});
final error = response.error;
return response;
} else
return null;
}
@override
void initState() {
super.initState();
if (signedin) {
getExperiences();
}
supabase.auth.onAuthStateChange((event, session) {
if (event.toString() == "AuthChangeEvent.signedIn" ||
event.toString() == "AuthChangeEvent.signedUp") {
setState(() {
signedin = true;
});
getExperiences();
}
});
}
@override
Widget build(BuildContext context) {
// Display the login and signup buttons
// if the user is not authenticated
if (!signedin) {
return Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"Please",
style: title,
),
Padding(
padding: const EdgeInsets.all(10.0),
child: ElevatedButton(
key: Key("tologinbtn"),
child: Text('Login'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Login()),
);
}),
),
Text(
"or",
style: title,
),
Padding(
padding: const EdgeInsets.all(10.0),
child: ElevatedButton(
child: Text('Signup'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Signup()),
);
}),
),
Text("to view your experiences", style: title),
],
),
),
);
} else {
return RefreshIndicator(
onRefresh: getExperiences,
child: Column(children: <Widget>[
Expanded(
child: ListView.builder(
physics: const AlwaysScrollableScrollPhysics(),
itemCount: experienceList.length,
padding: const EdgeInsets.all(8.0),
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
onTap: () {
// Open the detailed page for the experience
Navigator.push(
context,
MaterialPageRoute(
builder: (_) =>
ExperienceDetail(experienceList[index])));
},
child: Container(
margin: EdgeInsets.all(8.0),
decoration: boxdeco,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
experienceList[index].title,
style: TextStyle(
fontSize: 20, fontWeight: FontWeight.bold),
textAlign: TextAlign.start,
),
experienceList[index].imageUrl == null
? Text("")
: Padding(
padding: const EdgeInsets.all(8.0),
child: Image.network(
experienceList[index].imageUrl,
height: 180,
),
),
Text(
experienceList[index].description,
style: text,
textAlign: TextAlign.start,
),
],
),
),
),
);
}),
),
Container(
child: FloatingActionButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => (newExperience())),
);
},
child: const Icon(Icons.add),
backgroundColor: Colors.lightBlue,
tooltip: "New",
),
alignment: Alignment.bottomRight,
padding: const EdgeInsets.only(right: 20, bottom: 20),
),
]));
}
}
}