Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I search item from Searchable Dropdown per Name instead of per Id?

Tags:

flutter

How can I search item from Searchable Dropdown per Name instead of per 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;
    });
  },
)

Example: https://pub.dev/packages/searchable_dropdown

like image 874
Ryan Yang Avatar asked Dec 17 '19 20:12

Ryan Yang


People also ask

How do I add a search option to a drop down list?

Use any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS.

How do you search data from API list in flutter searchable dropdown list?

If you're using a List<Object> from an API response, you can map the keywords that you need to filter from to a List<String> to be used in the Autocomplete widget. The index can be used as a reference on the List<Object> to fetch the Object.


1 Answers

You have to overwrite your class's toString() so it can search both id and name
because SearchableDropdown's source code use item.value.toString()
when you pass class name it will become I/flutter ( 7352): Instance of 'VagasDisponivei'
you can copy paste run full code below

code snippet

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

working demo

enter image description here

full code

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

code snippet of SearchableDropdown

if(widget.isCaseSensitiveSearch){
        print(item.value.toString());
        isContains = item.value.toString().contains(keyword);
      }
like image 193
chunhunghan Avatar answered Oct 10 '22 18:10

chunhunghan