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

use*_*142 0 search flutter

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

我已经实现了几乎所有内容,我不能做的是当用户单击其中一个建议时,我希望该建议(单个文本)返回到上一个小部件中的文本字段。

我的文本字段:

   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),

                                 ),

                              ),
Run Code Online (Sandbox Code Playgroud)

我的 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,
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

Pra*_*oni 6

根据文档

应使用所选结果作为参数来调用 close。这将关闭搜索页面并将结果返回给 [showSearch] 的初始调用者。

例如

class CustomSearch extends SearchDelegate<String> {
  CustomSearch({String hintText});


  @override
  Widget buildSuggestions(BuildContext context) {
  ...
    return ListView.builder(
      itemCount: data.length,
      itemBuilder: (context, i) {
        return ListTile(
          title: Text(data[i]),
          onTap: () {close(context, data[i])},
        )
  ...
}
Run Code Online (Sandbox Code Playgroud)

并将结果返回给 [showSearch] 的初始调用者

onTap: () async {
  final result = await showSearch(
    context: context, 
    delegate: CustomSearch(hintText: "My Custom Search"),
  );
}
Run Code Online (Sandbox Code Playgroud)

  • 完美的!谢谢。 (2认同)