Flutter-将搜索委托结果字符串返回到文本字段

问题描述 投票:0回答:1

我有一个带有文本字段的表单,该表单应具有医生姓名,单击该表单时它将调用ShowSearch()来搜索医生姓名

我已经实现了几乎所有东西,当用户单击其中一个建议时,我无法做的是,我希望这个建议(单个文本)能够返回上一个小部件中的Textfield。

我的T​​extField:

TextFormField(

           onTap: (){showSearch(context: context, delegate: DataSearch());},
                                style: TextStyle(
                                        color: Colors.white,
                                    fontWeight: FontWeight.w600),
                                decoration: new InputDecoration(
                                    enabledBorder: OutlineInputBorder(
                                      borderSide: const BorderSide(
                                          color: Colors.white, width: 2.0),
                                      borderRadius:
                                          BorderRadius.circular(8.0),
                                    ),
                                    labelText: "Doctor name",
                                    labelStyle: MyFontStyles.textFieldsLabelStyle(context),

                                 ),

                              ),

我的ShowSearch():

class DataSearch extends SearchDelegate<String> {
  @override
  List<Widget> buildActions(BuildContext context) {
    return [IconButton(icon:Icon(Icons.clear),onPressed: (){query
    ="";},)];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
      icon: AnimatedIcon(
        icon: AnimatedIcons.menu_arrow,
        progress: transitionAnimation,
      ),
      onPressed: () {close(context, "search result");},
    );
  }

  @override
  Widget buildResults(BuildContext context) {
    // TODO: implement buildResults
    throw UnimplementedError();
  }

  //     ! CALLS SYNC FUNCTIONS WITHOUT AWAIT !
  @override
  Widget buildSuggestions(BuildContext context) {
    DoctorsController.fetchAllDoctors();
    List doctorsList = DoctorsController.getDoctorsNamesAsList();
    final List<String>suggestionList =query.isEmpty? doctorsList :
        doctorsList.where((element) => element.contains(query)).toList();

    return ListView.builder(
      itemBuilder: (context, index) {
        String sugText = suggestionList[index];
        return ListTile(

          title:
          RichText(
            text: TextSpan(
              text:sugText.substring(0, sugText.indexOf(query)),
              style:
                  TextStyle(color: Colors.grey),
              children: [
                TextSpan(
                  text: sugText.substring(sugText.indexOf(query), sugText.indexOf(query)+query.length),
                  style: TextStyle(color: Colors.black,fontWeight: FontWeight.bold),
                ),TextSpan(
                  text: sugText.substring(sugText.indexOf(query)+query.length, sugText.length),
                  style: TextStyle(color: Colors.grey),
                )
              ],
            ),
          )

      );},
      itemCount: suggestionList.length,
    );
  }
}
flutter search
1个回答
0
投票

使用TextEditingController作为TextField控制器。将其传递给您的searchDelegate。

© www.soinside.com 2019 - 2024. All rights reserved.