select_department.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:eitc_erm_app/utils/Component.dart';
  4. import 'package:eitc_erm_app/utils/Constants.dart';
  5. import 'package:eitc_erm_app/utils/logger.dart';
  6. import 'package:eitc_erm_app/widget/loading.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:http/http.dart' as http;
  9. import 'bean/department.dart';
  10. void main() {
  11. WidgetsFlutterBinding.ensureInitialized();
  12. runApp(SelectDepartment());
  13. }
  14. class SelectDepartment extends StatefulWidget {
  15. @override
  16. State<StatefulWidget> createState() => SelectDepartmentState();
  17. }
  18. class SelectDepartmentState extends State<SelectDepartment> {
  19. ValueNotifier<dynamic> result = ValueNotifier(null);
  20. late Future<Department?> _future;
  21. TextEditingController mController = TextEditingController();
  22. String searchDeptName = "";
  23. @override
  24. void initState() {
  25. super.initState();
  26. _future = fetchData();
  27. }
  28. Future<Department?> fetchData() async {
  29. logd(Global.token);
  30. final response = await http.get(
  31. Uri.parse('${Global.BaseUrl}sys-dept/list?deptName=$searchDeptName'),
  32. headers: jsonHeaders(withToken: true));
  33. if (response.statusCode == 200) {
  34. final json = decodeBodyToJson(response.bodyBytes);
  35. logd("搜索结果=$json");
  36. Department mDepartment = Department.fromJson(json);
  37. return mDepartment;
  38. } else {
  39. Component.toast("出错了,请稍后再试!", 0);
  40. }
  41. }
  42. @override
  43. Widget build(BuildContext context) {
  44. return Scaffold(
  45. appBar: AppBar(
  46. backgroundColor: Global.StatusBarColor,
  47. title: Container(
  48. decoration: BoxDecoration(
  49. color: Colors.white, borderRadius: BorderRadius.circular(90)),
  50. child: TextField(
  51. maxLines: 1,
  52. controller: mController,
  53. style: const TextStyle(fontSize: 12),
  54. textInputAction: TextInputAction.search,
  55. onSubmitted: (_) => _doSearch(),
  56. decoration: const InputDecoration(
  57. isDense: true,
  58. hintText: "请输入要搜索的科室名称",
  59. hintStyle: TextStyle(color: Colors.grey, fontSize: 12),
  60. contentPadding:
  61. EdgeInsets.symmetric(horizontal: 10, vertical: 8),
  62. border: OutlineInputBorder(
  63. borderSide: BorderSide.none,
  64. ),
  65. ),
  66. ),
  67. ),
  68. titleSpacing: 0,
  69. leading: IconButton(
  70. color: Colors.white,
  71. onPressed: () => Navigator.pop(context),
  72. icon: const Icon(Icons.arrow_back_ios)),
  73. actions: [
  74. IconButton(
  75. color: Colors.white,
  76. onPressed: _doSearch,
  77. icon: const Icon(Icons.search))
  78. ],
  79. ),
  80. body: FutureBuilder<Department?>(
  81. future: _future,
  82. builder: (context, snapshot) {
  83. if (snapshot.hasData) {
  84. Department? data = snapshot.data;
  85. return ListView.separated(
  86. shrinkWrap: true,
  87. itemCount: data?.data?.length ?? 0,
  88. itemBuilder: (context, index) {
  89. return InkWell(
  90. onTap: () =>
  91. Navigator.pop(context, data.data?[index].deptName),
  92. child: Padding(
  93. padding: const EdgeInsets.symmetric(
  94. horizontal: 10, vertical: 10),
  95. child: Text('${data!.data?[index].deptName}'),
  96. ),
  97. );
  98. },
  99. separatorBuilder: (BuildContext context, int index) =>
  100. const Divider(),
  101. );
  102. } else if (snapshot.hasError) {
  103. return Text('Error: ${snapshot.error}');
  104. }
  105. return const ColorLoader();
  106. },
  107. ));
  108. }
  109. void _doSearch() {
  110. setState(() {
  111. searchDeptName = mController.text;
  112. _future = fetchData();
  113. });
  114. }
  115. }