Guest User

Untitled

a guest
Aug 22nd, 2019
69
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import "package:flutter/material.dart";
  2. import "../services/graphqlConf.dart";
  3. import "../services/queryMutation.dart";
  4. import "package:graphql_flutter/graphql_flutter.dart";
  5. import "package:example/components/person.dart";
  6.  
  7. class AlertDialogWindow extends StatefulWidget {
  8. final Person person;
  9. final bool isAdd;
  10.  
  11. AlertDialogWindow({Key key, this.person, this.isAdd}) : super(key: key);
  12.  
  13. @override
  14. State<StatefulWidget> createState() =>
  15. _AlertDialogWindow(this.person, this.isAdd);
  16. }
  17.  
  18. class _AlertDialogWindow extends State<AlertDialogWindow> {
  19. TextEditingController txtId = TextEditingController();
  20. TextEditingController txtName = TextEditingController();
  21. TextEditingController txtLastName = TextEditingController();
  22. TextEditingController txtAge = TextEditingController();
  23. GraphQLConfiguration graphQLConfiguration = GraphQLConfiguration();
  24. QueryMutation addMutation = QueryMutation();
  25.  
  26. final Person person;
  27. final bool isAdd;
  28.  
  29. _AlertDialogWindow(this.person, this.isAdd);
  30.  
  31. @override
  32. void initState() {
  33. super.initState();
  34. if (!this.isAdd) {
  35. txtId.text = person.getId();
  36. txtName.text = person.getName();
  37. txtLastName.text = person.getLastName();
  38. txtAge.text = person.getAge().toString();
  39. }
  40. }
  41.  
  42. @override
  43. Widget build(BuildContext context) {
  44. // TODO: implement build
  45. return AlertDialog(
  46. title: Text(this.isAdd ? "Add" : "Edit or Delete"),
  47. content: Container(
  48. child: SingleChildScrollView(
  49. child: ConstrainedBox(
  50. constraints: BoxConstraints(
  51. maxWidth: MediaQuery.of(context).size.width,
  52. ),
  53. child: Stack(
  54. children: <Widget>[
  55. Container(
  56. child: TextField(
  57. maxLength: 5,
  58. controller: txtId,
  59. enabled: this.isAdd,
  60. decoration: InputDecoration(
  61. icon: Icon(Icons.perm_identity),
  62. labelText: "ID",
  63. ),
  64. ),
  65. ),
  66. Container(
  67. padding: EdgeInsets.only(top: 80.0),
  68. child: TextField(
  69. maxLength: 40,
  70. controller: txtName,
  71. decoration: InputDecoration(
  72. icon: Icon(Icons.text_format),
  73. labelText: "Name",
  74. ),
  75. ),
  76. ),
  77. Container(
  78. padding: EdgeInsets.only(top: 160.0),
  79. child: TextField(
  80. maxLength: 40,
  81. controller: txtLastName,
  82. decoration: InputDecoration(
  83. icon: Icon(Icons.text_rotate_vertical),
  84. labelText: "Last name",
  85. ),
  86. ),
  87. ),
  88. Container(
  89. padding: EdgeInsets.only(top: 240.0),
  90. child: TextField(
  91. maxLength: 2,
  92. controller: txtAge,
  93. keyboardType: TextInputType.number,
  94. decoration: InputDecoration(
  95. labelText: "Age", icon: Icon(Icons.calendar_today)),
  96. ),
  97. ),
  98. ],
  99. ),
  100. ),
  101. ),
  102. ),
  103. actions: <Widget>[
  104. FlatButton(
  105. child: Text("Close"),
  106. onPressed: () => Navigator.of(context).pop(),
  107. ),
  108. !this.isAdd
  109. ? FlatButton(
  110. child: Text("Delete"),
  111. onPressed: () async {
  112. GraphQLClient _client = graphQLConfiguration.clientToQuery();
  113. QueryResult result = await _client.mutate(
  114. MutationOptions(
  115. document: addMutation.deletePerson(txtId.text),
  116. ),
  117. );
  118. if (!result.hasErrors) Navigator.of(context).pop();
  119. },
  120. )
  121. : null,
  122. FlatButton(
  123. child: Text(this.isAdd ? "Add" : "Edit"),
  124. onPressed: () async {
  125. if (txtId.text.isNotEmpty &&
  126. txtName.text.isNotEmpty &&
  127. txtLastName.text.isNotEmpty &&
  128. txtAge.text.isNotEmpty) {
  129. if (this.isAdd) {
  130. GraphQLClient _client = graphQLConfiguration.clientToQuery();
  131. QueryResult result = await _client.mutate(
  132. MutationOptions(
  133. document: addMutation.addPerson(
  134. txtId.text,
  135. txtName.text,
  136. txtLastName.text,
  137. int.parse(txtAge.text),
  138. ),
  139. ),
  140. );
  141. if (!result.hasErrors) {
  142. txtId.clear();
  143. txtName.clear();
  144. txtLastName.clear();
  145. txtAge.clear();
  146. Navigator.of(context).pop();
  147. }
  148. } else {
  149. GraphQLClient _client = graphQLConfiguration.clientToQuery();
  150. QueryResult result = await _client.mutate(
  151. MutationOptions(
  152. document: addMutation.editPerson(
  153. txtId.text,
  154. txtName.text,
  155. txtLastName.text,
  156. int.parse(txtAge.text),
  157. ),
  158. ),
  159. );
  160. if (!result.hasErrors) {
  161. txtId.clear();
  162. txtName.clear();
  163. txtLastName.clear();
  164. txtAge.clear();
  165. Navigator.of(context).pop();
  166. }
  167. }
  168. }
  169. },
  170. )
  171. ],
  172. );
  173. }
  174. }
RAW Paste Data