import 'dart:async'; import 'dart:io'; import 'package:eitc_erm_app/utils/Component.dart'; import 'package:eitc_erm_app/utils/Constants.dart'; import 'package:eitc_erm_app/utils/logger.dart'; import 'package:eitc_erm_app/widget/loading.dart'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'bean/department.dart'; void main() { WidgetsFlutterBinding.ensureInitialized(); runApp(SelectDepartment()); } class SelectDepartment extends StatefulWidget { @override State createState() => SelectDepartmentState(); } class SelectDepartmentState extends State { ValueNotifier result = ValueNotifier(null); late Future _future; TextEditingController mController = TextEditingController(); String searchDeptName = ""; @override void initState() { super.initState(); _future = fetchData(); } Future fetchData() async { logd(Global.token); final response = await http.get( Uri.parse('${Global.BaseUrl}sys-dept/list?deptName=$searchDeptName'), headers: jsonHeaders(withToken: true)); if (response.statusCode == 200) { final json = decodeBodyToJson(response.bodyBytes); logd("搜索结果=$json"); Department mDepartment = Department.fromJson(json); return mDepartment; } else { Component.toast("出错了,请稍后再试!", 0); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Global.StatusBarColor, title: Container( decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(90)), child: TextField( maxLines: 1, controller: mController, style: const TextStyle(fontSize: 12), textInputAction: TextInputAction.search, onSubmitted: (_) => _doSearch(), decoration: const InputDecoration( isDense: true, hintText: "请输入要搜索的科室名称", hintStyle: TextStyle(color: Colors.grey, fontSize: 12), contentPadding: EdgeInsets.symmetric(horizontal: 10, vertical: 8), border: OutlineInputBorder( borderSide: BorderSide.none, ), ), ), ), titleSpacing: 0, leading: IconButton( color: Colors.white, onPressed: () => Navigator.pop(context), icon: const Icon(Icons.arrow_back_ios)), actions: [ IconButton( color: Colors.white, onPressed: _doSearch, icon: const Icon(Icons.search)) ], ), body: FutureBuilder( future: _future, builder: (context, snapshot) { if (snapshot.hasData) { Department? data = snapshot.data; return ListView.separated( shrinkWrap: true, itemCount: data?.data?.length ?? 0, itemBuilder: (context, index) { return InkWell( onTap: () => Navigator.pop(context, data.data?[index].deptName), child: Padding( padding: const EdgeInsets.symmetric( horizontal: 10, vertical: 10), child: Text('${data!.data?[index].deptName}'), ), ); }, separatorBuilder: (BuildContext context, int index) => const Divider(), ); } else if (snapshot.hasError) { return Text('Error: ${snapshot.error}'); } return const ColorLoader(); }, )); } void _doSearch() { setState(() { searchDeptName = mController.text; _future = fetchData(); }); } }