select_department.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:eitc_erm_app/utils/Component.dart';
  5. import 'package:eitc_erm_app/utils/Constants.dart';
  6. import 'package:eitc_erm_app/widget/loading.dart';
  7. import 'package:flutter/cupertino.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:http/http.dart' as http;
  10. import 'bean/department.dart';
  11. void main() {
  12. WidgetsFlutterBinding.ensureInitialized();
  13. runApp(SelectDepartment());
  14. }
  15. class SelectDepartment extends StatefulWidget {
  16. @override
  17. State<StatefulWidget> createState() => SelectDepartmentState();
  18. }
  19. class SelectDepartmentState extends State<SelectDepartment> {
  20. ValueNotifier<dynamic> result = ValueNotifier(null);
  21. late Future<Department?> _future;
  22. TextEditingController mController = TextEditingController();
  23. String searchDeptName = "";
  24. @override
  25. void initState() {
  26. super.initState();
  27. _future = fetchData();
  28. }
  29. Future<Department?> fetchData() async {
  30. print(Global.token);
  31. final response = await http.get(
  32. Uri.parse('${Global.BaseUrl}sys-dept/list?deptName=' + searchDeptName),
  33. headers: {
  34. HttpHeaders.contentTypeHeader: "application/json; charset=utf-8",
  35. 'token': '${Global.token}',
  36. });
  37. if (response.statusCode == 200) {
  38. final jsonString = utf8.decode(response.bodyBytes);
  39. final jsonResponse = jsonDecode(jsonString);
  40. print(jsonResponse);
  41. Department mDepartment = new Department.fromJson(jsonResponse);
  42. return mDepartment;
  43. } else {
  44. Component.toast("出错了,请稍后再试!", 0);
  45. }
  46. }
  47. @override
  48. Widget build(BuildContext context) {
  49. return /*WillPopScope(
  50. // 使用 onWillPop 回调来决定是否允许退出
  51. onWillPop: () async {
  52. return false;
  53. },
  54. child: */Scaffold(
  55. appBar: new AppBar(title: new Text(' '), centerTitle: true),
  56. body: Container(
  57. padding: EdgeInsets.all(20),
  58. child: SingleChildScrollView(
  59. child: Column(
  60. crossAxisAlignment:
  61. CrossAxisAlignment.center, //纵向对齐方式
  62. children: <Widget>[
  63. Padding(
  64. padding: EdgeInsets.fromLTRB(5, 5, 5, 5),
  65. child: SearchBar(
  66. hintText: "请输入科室",
  67. leading: Icon(Icons.earbuds),
  68. controller: mController,
  69. trailing: [
  70. Icon(Icons.mic),
  71. GestureDetector(
  72. child: Icon(Icons.search_rounded),
  73. onTap: () {
  74. setState(() {
  75. searchDeptName = mController.text;
  76. _future = fetchData();
  77. });
  78. }),
  79. ]),
  80. ),
  81. Padding(
  82. padding: EdgeInsets.fromLTRB(5, 5, 5, 5),
  83. child: Row(children: [
  84. Expanded(
  85. child: FutureBuilder<Department?>(
  86. future: _future,
  87. builder: (context, snapshot) {
  88. if (snapshot.hasData) {
  89. Department? data = snapshot.data;
  90. return ListView.builder(
  91. shrinkWrap: true,
  92. itemCount: data?.data?.length,
  93. itemBuilder: (context, index) {
  94. return ListTile(
  95. title: Text(
  96. '${data!.data?[index].deptName}'),
  97. onTap: () {
  98. Navigator.pop(context, data!.data?[index].deptName);
  99. // Navigator.push(
  100. // context,
  101. // MaterialPageRoute(builder: (context) => NfcDetail()),
  102. // );
  103. },
  104. );
  105. },
  106. );
  107. } else if (snapshot.hasError) {
  108. return Text('Error: ${snapshot.error}');
  109. }
  110. return ColorLoader();
  111. },
  112. ),
  113. ),
  114. ]),
  115. )
  116. ])),
  117. ));
  118. }
  119. }