123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- 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<StatefulWidget> createState() => SelectDepartmentState();
- }
- class SelectDepartmentState extends State<SelectDepartment> {
- ValueNotifier<dynamic> result = ValueNotifier(null);
- late Future<Department?> _future;
- TextEditingController mController = TextEditingController();
- String searchDeptName = "";
- @override
- void initState() {
- super.initState();
- _future = fetchData();
- }
- Future<Department?> 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<Department?>(
- 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();
- });
- }
- }
|