如何使用FutureBuilder过滤ListView?

时间:2020-11-11 15:19:36

标签: flutter mobile-application flutter-listview flutter-futurebuilder

我是Flutter编程的初学者。 我调用了一个Web API来获取ListView项目。我需要对此ListView进行过滤。 当我在搜索框中输入任何文本时,将显示“正在等待结果”世界,然后再次呈现列表,而没有任何过滤结果。我在搜索框中输入的字母都会调用Web API 我厌倦了许多解决方案,但没有一个对我有用。 代码是:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:soft_ta/Models/EmployeeExceptionModel.dart';
import 'package:soft_ta/Widget/sharedWidget.dart' as shared;
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:soft_ta/HelperClass.dart';
import 'package:soft_ta/Remote/WepAPI.dart';

class EmployeesExceptios extends StatefulWidget {
 @override
 State<StatefulWidget> createState() {
return new _EmployeesExceptiosState();
  }
}

class _EmployeesExceptiosState extends State<EmployeesExceptios> {
  TextEditingController _textController = TextEditingController();

  List<EmployeeExceptionModel> employeeExceptionModelList =
      new List<EmployeeExceptionModel>();

  final _scaffoldKey = GlobalKey<ScaffoldState>();
  HelperClass helper = new HelperClass();

  List<EmployeeExceptionModel> newDataList = new List<EmployeeExceptionModel>();

  onItemChanged(String value) {
    setState(() {
      newDataList = employeeExceptionModelList
          .where((a) =>
              a.EmployeeId.toString() == (value.toString().toLowerCase()))
          .toList();
    });
  }

  @override
  Widget build(BuildContext context) {
    final height = MediaQuery.of(context).size.height;

    return FutureBuilder(
        future: Future.wait([_getEmployeeException()]),
        builder: (BuildContext context, AsyncSnapshot<List<dynamic>> snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.none:
              return new Text('Press button to start');
            case ConnectionState.waiting:
              return new Text('Awaiting result...');
            default:
              if (snapshot.hasError)
                return new Text('Error: ${snapshot.error}');
              else {
                employeeExceptionModelList = snapshot.data[0];
                newDataList = employeeExceptionModelList;

                return Scaffold(
                    key: _scaffoldKey,
                    body: Container(
                      decoration: BoxDecoration(
                          gradient: LinearGradient(
                              begin: Alignment.topRight,
                              end: Alignment.bottomLeft,
                              colors: [Colors.white, Colors.blueGrey])),
                      height: height,
                      child: Stack(
                        children: [
                          Container(
                            padding: EdgeInsets.only(top: 20),
                            child: Column(
                              children: [
                                shared.divider(
                                    value:
                                        "EmployeeExceptions".tr().toString()),
                              ],
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(top: height * .10),
                            child: TextField(
                              controller: _textController,
                              decoration: InputDecoration(
                                hintText: 'Search Here by EmployeeId...',
                              ),
                              onChanged: onItemChanged,
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(top: height * .20),
                            child: Container(
                              // height: height,
                              padding: EdgeInsets.symmetric(horizontal: 20),
                              child: Container(
                                child: ListView.separated(
                                    separatorBuilder: (context, index) =>
                                        Divider(
                                          color: Colors.black,
                                        ),
                                    itemCount: newDataList.length,
                                    itemBuilder: (context, int index) {
                                      return Slidable(
                                        actions: <Widget>[
                                          IconSlideAction(
                                              icon: Icons.more,
                                              caption: 'MORE',
                                              color: Colors.blue,
                                              onTap: () {
                                                print(
                                                "More ${newDataList[index]} is Clicked");
                                              }),
                                        ],
                                        secondaryActions: <Widget>[
                                          IconSlideAction(
                                              icon: Icons.clear,
                                              color: Colors.red,
                                              caption: 'Cancel',
                                              onTap: () {
                                                print(
                                                    "Cancel ${newDataList[index]} is Clicked");
                                              })
                                        ],
                                        child: ListTile(
                                          // leading: Icon(Icons.message),
                                          title: Text(
                                            "${newDataList[index].EmployeeId}",
                                            style: TextStyle(
                                                fontSize: 16.0,
                                                fontWeight: FontWeight.bold,
                                                fontFamily: 'Arial'),
                                          ),
                                         subtitle: Column(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: [
                                              Text(
                                                "${newDataList[index].FromDT}",
                                                style: TextStyle(
                                                    color: Colors.blueGrey,
                                                    fontSize: 13.0),
                                              ),
                                              Text(
                                                "${newDataList[index].ExceptionName}",
                                                style: TextStyle(
                                                    color: Colors.blueGrey,
                                                    fontSize: 13.0),
                                              ),
                                            ],
                                          ),
                                          trailing: Icon(Icons.arrow_back),
                                        ),
                                        actionPane: SlidableDrawerActionPane(),
                                      );
                                    }),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ));
              }
          }
        });
  }

  Future<List<EmployeeExceptionModel>> _getEmployeeException() async {
    var _wepAPI = new WebAPI();
    List<EmployeeExceptionModel> lst = new List<EmployeeExceptionModel>();
    EmployeeExceptionModel objEmployeeExceptionModel;
    List value =
        await _wepAPI.getEmployeesExceptions(helper.getCurrentUserId());
    value.forEach((element) {
      objEmployeeExceptionModel = new EmployeeExceptionModel();
      objEmployeeExceptionModel.AssignmentId = element["AssignmentId"];
      objEmployeeExceptionModel.EmployeeId = element["EmployeeId"];
      objEmployeeExceptionModel.EmployeeName = element["EmployeeName"];
      objEmployeeExceptionModel.AprvStsId = element["AprvStsId"];
      objEmployeeExceptionModel.ExceptionName = element["ExceptionName"];
      objEmployeeExceptionModel.ExceptionTypeName =
          element["ExceptionTypeName"];
      objEmployeeExceptionModel.FromDT = element["FromDT"];
      objEmployeeExceptionModel.ToDT = element["ToDT"];
      lst.add(objEmployeeExceptionModel);
    });

    return lst;
  }
}

0 个答案:

没有答案