-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmanageCOC.dart
161 lines (153 loc) · 5.57 KB
/
manageCOC.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
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:google_fonts/google_fonts.dart';
import 'addGuidelines.dart';
class ManageCommunityGuidelinesScreen extends StatefulWidget {
@override
_ManageCommunityGuidelinesScreenState createState() => _ManageCommunityGuidelinesScreenState();
}
class _ManageCommunityGuidelinesScreenState extends State<ManageCommunityGuidelinesScreen> {
List<Widget> _listOfGuidelines = List();
List<Map> _updatedGuidelinesList = List();
_fetchGuidelines() async {
var details = await Firestore.instance.collection("details").document("details").get();
for (var item in details["coc"]) {
_listOfGuidelines.add(
ListTile(
title: Text(item["name"]),
subtitle: Text(item["des"]),
),
);
Map<String, dynamic> tempDetails = {"name": item["name"], "des": item["des"]};
_updatedGuidelinesList.add(tempDetails);
setState(() {});
}
}
Future<bool> _confirmDismiss(DismissDirection direction) async {
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text("Do you want to delete this guideline ?"),
actions: <Widget>[
FlatButton(
child: Text("Yes"),
onPressed: () => Navigator.pop(context, true),
),
FlatButton(
child: Text("No"),
onPressed: () => Navigator.pop(context, false),
),
],
),
) ??
false;
}
Future<bool> _deleteGuideline(index) async {
try {
var ref = [];
ref.add(_updatedGuidelinesList[index]);
await Firestore.instance.collection("details").document('details').updateData({"coc": FieldValue.arrayRemove(ref)});
return true;
} catch (e) {
return false;
}
}
void _clearGuidelines() {
setState(() {
_updatedGuidelinesList.clear();
_listOfGuidelines.clear();
_fetchGuidelines();
});
}
@override
void initState() {
_fetchGuidelines();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Manage Community Guidelines"),
centerTitle: true,
),
body: _listOfGuidelines.length == 0
? Container(
padding: EdgeInsets.only(top: 30),
alignment: Alignment.topCenter,
child: Text(
"Add Guidelines",
style: GoogleFonts.lato(
textStyle: TextStyle(
fontSize: 24,
fontWeight: FontWeight.w300,
),
),
),
)
: ListView.builder(
padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 20.0),
physics: BouncingScrollPhysics(),
itemCount: _listOfGuidelines.length,
itemBuilder: (BuildContext context, int index) {
return Card(
clipBehavior: Clip.antiAliasWithSaveLayer,
child: Dismissible(
key: Key(index.toString()),
direction: DismissDirection.endToStart,
background: Container(
padding: EdgeInsets.only(right: 25.0),
alignment: Alignment.centerRight,
color: Colors.red,
child: Icon(
Icons.delete_forever,
size: 30.0,
color: Colors.white,
),
),
confirmDismiss: (direction) => _confirmDismiss(direction),
onDismissed: (direction) async {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text(await _deleteGuideline(index) ? "Guideline deleted successfully !" : "An error occured while deleting guideline !"),
),
);
_clearGuidelines();
},
child: InkWell(
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => AddGuidelines(
pageTitle: "Edit Guidelines",
title: _updatedGuidelinesList[index]["name"],
description: _updatedGuidelinesList[index]["des"],
listOfGuidelines: _updatedGuidelinesList,
index: index,
),
),
).then((onValue) => _clearGuidelines()),
child: _listOfGuidelines[index])),
);
},
),
floatingActionButton: FloatingActionButton(
tooltip: "Add Guideline",
child: Icon(Icons.add),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => AddGuidelines(
pageTitle: "Add Guidelines",
title: "",
description: "",
listOfGuidelines: _updatedGuidelinesList,
),
),
).then((onValue) => _clearGuidelines());
},
),
);
}
}