Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

api - How to search dropdown with data from a local json

I used a package to add dropdown with search feature unfortunately it doesn't search my json data

i managed to load my json to the dropdown, the only issue is it cant search my local json file i

should i remove the dependency altogether and create my own custom dropdown widget or change the dependency code and make it search i don't know which path is easier, as this dependency i could not change it to make it search my local json

Help will be much appreciated

I created a small simple app to demonstrate the dropdown with my local json

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

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

class SearchableDropdownApp extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<SearchableDropdownApp> {
  Map<String, String> selectedValueMap = Map();
  List<Map> _areas;

  @override
  void initState() {
    selectedValueMap["local"] = null;
    super.initState();
    loadLocalJsonData();
  }
  @override
  Widget build(BuildContext context) {

    final items = _areas.map((county) {
      return DropdownMenuItem<String>(
        child: Text(county["name"].toString()),
      );
    }).toList();

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Searchable Dropdown Example App'),
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Center(
              child: Text('Dropdown with local data : ',
                style: TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold
                ),
              ),
            ),
            SearchableDropdown(
              items: items,
              value: selectedValueMap["local"],
              isCaseSensitiveSearch: false,
              hint: Text('Select County'),
              searchHint: Text('Counties'),
              onChanged: (value) {
                setState(() {
                  selectedValueMap["local"] = value;
                });
              },
            ),
          ],
        ),
      ),
    );
  }

  Future loadLocalJsonData() async {
    String jsonCounties = await rootBundle.loadString("assets/counties.json");
    setState(() {
      _areas = List<Map>.from(jsonDecode(jsonCounties) as List);
    });
  }
}
question from:https://stackoverflow.com/questions/65892470/how-to-search-dropdown-with-data-from-a-local-json

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...