searchable_dropdown不适用于班级列表 [英] searchable_dropdown dont work with class list

查看:212
本文介绍了searchable_dropdown不适用于班级列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用searchable_dropdown 1.1.0包实现了flutter下拉菜单, 我用班级列表进行了测试,发现搜索TextField框不适用于班级列表. 我希望当我在列表中的两个字符串/整数中搜索值时,下拉搜索框都可以工作.

I Implemented a flutter dropdown menu using searchable_dropdown 1.1.0 package,, I tested with my class list, and I found that the search TextField box dont work with my list of class. I want that my dropdown search box works when I search for a value in both strings/intgers in my list.

例如:我希望当我输入数字1或键1时,它应该向我显示我搜索的项目

for example: I want that when I type number 1 or key 1 it should show me the item I searched for

我想知道如何解决此问题? 感谢您的帮助

I wonder how to fix this problem? Thanks for help

这是我的代码.

import 'package:flutter/material.dart';
import 'package:searchable_dropdown/searchable_dropdown.dart';

get_list(){
  List<KeyValueModel> datas = [
    KeyValueModel(key: "Key 1", value: "Value 1"),
    KeyValueModel(key: "Key 2", value: "Value 2"),
    KeyValueModel(key: "Key 3", value: "Value 3"),
    KeyValueModel(key: "Key 4", value: "Value 4"),
    KeyValueModel(key: "Key 5", value: "Value 5"),
  ];
  return datas;
}
//Create a Model class to hold key-value pair data
class KeyValueModel {
  String key;
  String value;

  KeyValueModel({this.key, this.value});
}


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

  List listan = get_list();

  KeyValueModel _selectedValue = KeyValueModel(key: "0", value: "value");

  @override
  Widget build(BuildContext context) {


    return Scaffold(
      appBar: AppBar(
        title: Text('Key value Pair - DropdownButton'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: <Widget>[
            SearchableDropdown<KeyValueModel>(
        isCaseSensitiveSearch: true,
              items: listan
                  .map((data) => DropdownMenuItem<KeyValueModel>(
                child: Text(data.key),
                value: data,
              )).toList(),
              onChanged: (KeyValueModel value) {
                setState(() => _selectedValue = value);
              },
              hint: Text('Select Key'),
            ),
            SizedBox(
              height: 25.0,
            ),
            Text(_selectedValue.value),
          ],
        ),
      ),
    );
  }
}


推荐答案

以下是该程序包中的一些源代码.

Here is a bit source code from the package.

...

if(widget.isCaseSensitiveSearch){
   isContains = item.value.toString().contains(keyword);
}
else{
   isContains = item.value.toString().toLowerCase().contains(keyword.toLowerCase());
}

...

它在value上调用toString().在您的情况下,valueKeyValueModel的类型,并且没有覆盖的toString()方法.因此它返回Instance of KeyValueModel而不是Key 1左右.

It calls toString() on the value. In your case value is type of KeyValueModel and doesn't have an overriden toString() method. So it returns Instance of KeyValueModel instead of Key 1 or so.

这是调用toString()方法时返回<​​c7>的类.

Here is the class that returns the key when toString() method called.

class KeyValueModel {
  String key;
  String value;

  KeyValueModel({this.key, this.value});

  @override
  String toString() {
    return this.key;
  }
}

这篇关于searchable_dropdown不适用于班级列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆