Advertisement
Guest User

Untitled

a guest
Jan 28th, 2022
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. class _WorkingScreenState extends State<WorkingScreen> {
  2. int pageIndex = 0;
  3.  
  4. String? chooseLocation;
  5.  
  6. late Future<Response> futureData;
  7. late Future<Response> futureDataForAccount;
  8. late Future<Response> futureDataForStatus;
  9. Timer? timer;
  10.  
  11.  
  12. List<WorkingLocationData>? workingData;
  13. List<AccountData>? accountData;
  14. bool shouldPop = true;
  15.  
  16. bool _flag = true;
  17. Response? response;
  18.  
  19. Future<Response?> postLocationId(String? id) async {
  20. SharedPreferences prefs = await SharedPreferences.getInstance();
  21. String? authorization = prefs.getString('authorization');
  22. var url = 'https://dev.api.wurk.skyver.co/api/$id/workingTimeframes';
  23. try {
  24. response = await http.post(
  25. Uri.parse(url),
  26. headers: <String, String>{
  27. 'authorization': authorization ?? basicAuth.toString(),
  28. "Content-Type": "application/json"
  29. },
  30. );
  31. print(id);
  32. print(response!.statusCode);
  33. } catch (er) {}
  34.  
  35. return response;
  36. }
  37.  
  38. final _formKey = GlobalKey<FormState>();
  39.  
  40. @override
  41. void initState() {
  42. super.initState();
  43. futureData = fetchWorkingLocationData();
  44. futureDataForAccount = fetchAccountData();
  45. }
  46.  
  47. @override
  48. Widget build(BuildContext context) {
  49. var width = MediaQuery.of(context).size.width;
  50. var height = MediaQuery.of(context).size.height;
  51. return Form(
  52. key: _formKey,
  53. child: WillPopScope(
  54. onWillPop: () async {
  55. return shouldPop;
  56. },
  57. child: SafeArea(
  58. child: Scaffold(
  59. body: FutureBuilder<List<Response>>(
  60. future: Future.wait([futureData, futureDataForAccount]),
  61. builder: (context, snapshot) {
  62. if (snapshot.hasData) {
  63. List<WorkingLocationData> data1 =
  64. parsedData(snapshot.data![0].body);
  65. workingData = data1;
  66.  
  67. AccountData data3 =
  68. AccountData.fromJson(json.decode(snapshot.data![1].body));
  69.  
  70. //WorkingLocationStatus statusData = WorkingLocationStatus.fromJson(json.decode(snapshot.data![2].body));
  71.  
  72. return Column(
  73. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  74. children: [
  75. Container(
  76. width: width,
  77. height: 50,
  78. decoration: BoxDecoration(
  79. border: Border.all(
  80. color: Colors.black,
  81. width: 4,
  82. ),
  83. ),
  84. child: Center(
  85. child: Text(
  86. 'Welcome ' + data3.firstName + ' ${data3.lastName}',
  87. style: const TextStyle(fontSize: 18),
  88. ),
  89. ),
  90. ),
  91. const Text(
  92. 'Choose working location',
  93. style: TextStyle(fontSize: 23),
  94. ),
  95. Container(
  96. width: width * 0.860,
  97. padding: const EdgeInsets.symmetric(
  98. horizontal: 12, vertical: 4),
  99. decoration: BoxDecoration(
  100. borderRadius: BorderRadius.circular(12),
  101. border: Border.all(color: Colors.black, width: 4),
  102. ),
  103. child: DropdownButtonFormField<String>(
  104. decoration: const InputDecoration(
  105. enabledBorder: UnderlineInputBorder(
  106. borderSide: BorderSide(color: Colors.transparent),
  107. ),
  108. ),
  109. value: chooseLocation,
  110. hint: const Text('Select a location'),
  111. isExpanded: true,
  112. items: workingData!.map((some) {
  113. return DropdownMenuItem(
  114. child: Text(some.name + ' (${some.location})'),
  115. value: some.id,
  116. );
  117. }).toList(),
  118. onChanged: (String? displayedValue) {
  119. setState(
  120. () {
  121. chooseLocation = displayedValue!;
  122. },
  123. );
  124. },
  125. validator: (value) {
  126. if (value == '') {
  127. return 'Please select a working location';
  128. }
  129. return null;
  130. },
  131. ),
  132. ),
  133. SizedBox(height: height * 0.150),
  134. Column(
  135. children: [
  136. Container(
  137. width: width / 2.5,
  138. height: height / 10,
  139. child: ElevatedButton(
  140. onPressed: () async {
  141. final WorkingLocationData locationData =
  142. workingData!.firstWhere(
  143. (some) => some.id == chooseLocation);
  144. await postLocationId(locationData.id);
  145. setState(() {
  146. _flag = !_flag;
  147. });
  148. },
  149. child: Text(
  150. _flag ? 'Start Work' : 'Stop Work',
  151. style: const TextStyle(fontSize: 20),
  152. ),
  153. style: ElevatedButton.styleFrom(
  154. primary: _flag ? Colors.teal : Colors.red,
  155. ),
  156. ),
  157. ),
  158. ],
  159. ),
  160. SizedBox(
  161. height: height * 0.01,
  162. )
  163. ],
  164. );
  165. } else if (snapshot.hasError) {
  166. return Text('${snapshot.error}');
  167. }
  168. return const Center(child: CircularProgressIndicator());
  169. },
  170. ),
  171. bottomNavigationBar: bottomNavigationBar1(context),
  172. ),
  173. ),
  174. ),
  175. );
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement