flutter - 如何从“可搜索的下拉列表”中按名称而不是按ID搜索项目?

标签 flutter

如何从“可搜索的下拉列表”中按名称而不是按ID搜索项目?

SearchableDropdown(items:_vagasDisponiveis.map((item) {
            return new DropdownMenuItem(
              child: Text(item.v_n),
              value: item.v_id
            );
          }
        ).toList(),
    isExpanded: true,
    value: vaga_id,
    isCaseSensitiveSearch: true,
    searchHint: new Text('Select ', style: new TextStyle(fontSize: 20)),
    onChanged: (value) {
      setState(() {
        vaga_id = value;
    });
  },
)
示例:https://pub.dev/packages/searchable_dropdown

最佳答案

您必须覆盖类的toString(),以便它可以搜索ID和名称
因为SearchableDropdown的源代码使用item.value.toString()当您传递类名称时,它将变为I/flutter ( 7352): Instance of 'VagasDisponivei'您可以在下面复制粘贴运行完整代码

代码段

class VagasDisponivei {
  String v_n;
  String v_id;

  VagasDisponivei({this.v_n, this.v_id});

  @override
  String toString() {
    return '${v_n} ${v_id}';
  }
}

SearchableDropdown(
              items: _vagasDisponiveis.map((item) {
                return new DropdownMenuItem<VagasDisponivei>(
                    child: Text(item.v_n), value: item);
              }).toList(),
              isExpanded: true,
              value: selectedValue,
              isCaseSensitiveSearch: true,
              searchHint: new Text(
                'Select ',
                style: new TextStyle(fontSize: 20),
              ),
              onChanged: (value) {
                setState(() {
                  selectedValue = value;
                  print(selectedValue);
                });
              },
            ),

工作演示

enter image description here

完整的代码
import 'package:flutter/material.dart';
import 'package:searchable_dropdown/searchable_dropdown.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class VagasDisponivei {
  String v_n;
  String v_id;

  VagasDisponivei({this.v_n, this.v_id});

  @override
  String toString() {
    return '${v_n} ${v_id}';
  }
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  List<VagasDisponivei> _vagasDisponiveis;
  String vaga_name;
  VagasDisponivei selectedValue;
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _vagasDisponiveis = [
      VagasDisponivei(v_id: "1", v_n: "abc"),
      VagasDisponivei(v_id: "2", v_n: "def"),
      VagasDisponivei(v_id: "3", v_n: "dgg"),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            SearchableDropdown(
              items: _vagasDisponiveis.map((item) {
                return new DropdownMenuItem<VagasDisponivei>(
                    child: Text(item.v_n), value: item);
              }).toList(),
              isExpanded: true,
              value: selectedValue,
              isCaseSensitiveSearch: true,
              searchHint: new Text(
                'Select ',
                style: new TextStyle(fontSize: 20),
              ),
              onChanged: (value) {
                setState(() {
                  selectedValue = value;
                  print(selectedValue);
                });
              },
            ),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

SearchableDropdown的代码段
if(widget.isCaseSensitiveSearch){
        print(item.value.toString());
        isContains = item.value.toString().contains(keyword);
      }

关于flutter - 如何从“可搜索的下拉列表”中按名称而不是按ID搜索项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59381692/

相关文章:

flutter - 使用 File Picker 将 PlatformFile 转换为 Flutter 中的文件

flutter - 如何在flutter中找到map的最大值的key?

flutter - 如何使用 flutter compass 找到方向?

Flutter 如何处理 Image.network 错误(如 404 或错误的 url)

flutter - Navigator.pop之后,StreamBuilder不会重建

flutter - Flutter转换货币格式

android-studio - 为什么 Android Studio 在运行 flutter 应用程序时找不到 x86 模拟器?

Flutter Dio : How to Upload Image?

android - 如何通过 NFC 打开应用程序并在同一操作中传递嵌入数据?

flutter - 从Firestore数据库获取最新数据到Flutter App