简体   繁体   English

知道应用程序内的信息作为列表存在于另一个 class 中,我如何使用 flutter 中的搜索

[英]How can i use the search in flutter knowing that the information inside the application is present as a list in another class

In this application I have a list of the names of books found inside the application How can I create a search function and call the list within it and then go to the results when I click on the search results在这个应用程序中,我有一个在应用程序中找到的书籍名称列表如何创建搜索 function 并在其中调用列表,然后在单击搜索结果时调用 go 到结果

class Book extends StatelessWidget { class 书扩展 StatelessWidget {

  var MainList =[
    {
      'image' : 'images/a_dot_ham.jpeg',
      'name': 'book 1',
      'writer': 'ِmark',
      'date' : '2007',
    },
{
      'image' : 'images/a_dot_ham.jpeg',
      'name': 'book 2',
      'writer': 'ِjon',
      'date' : '2003',
    },
{
      'image' : 'images/a_dot_ham.jpeg',
      'name': 'book 3',
      'writer': 'ِalex',
      'date' : '1997',
    },
  ];

  @override
  Widget build(BuildContext context) {
    return 
    Scaffold(
      appBar: AppBar(
        title: Text('Books'),
        centerTitle: true,
      ),
      body: ListView.builder(
        itemCount: MainList .length,
        itemBuilder: (context , i){
          return
            BookList (
              image :MainList [i]['image'],
              name :MainList [i]['name'] ,
              writer: MainList [i]['writer'],
              date: MainList [i]['date'],
              );},
      ),
    );}
}

Next class is where the book list will appear接下来 class 是书单出现的地方

class BookList extends StatelessWidget {
  final  image ;
  final name;
  final writer;
  final date;
  BookList({this.name,this.writer,this.image,this.time,this.date});

  @override
  Widget build(BuildContext context) {
    return InkWell(child: Container(
      height: 100,
      width: 100,
      child: Card(
        child: Row(children: <Widget>[
          Expanded( flex: 1,child: Image.network(image),),
          Expanded(flex: 2 ,child: Container(padding: EdgeInsets.only(right: 4),
              alignment: Alignment.topRight,
              child: Column(crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[
                Text(name,style: TextStyle(fontSize: 18)),
                Row(children: <Widget>[
                  Expanded(child: Row(children: <Widget>[Text('date: ') , Text(date,style: TextStyle(color: Colors.grey),)] ),),
                  Expanded(child: Row(children: <Widget>[Text('writer: ') , Text(writer,style: TextStyle(color: Colors.grey),)] ))
                ],),
                Row(children: <Widget>[
                  Expanded(child:Row(children: <Widget>[Text('name: ') , Text(name,style: TextStyle(color: Colors.grey),)] ) ),
                 
                ],),
              ],)),)
        ],),
      ),),
      onTap: (){
        Navigator.of(context).push(MaterialPageRoute(builder: (context){
          return BookDetails(name: name , writer: writer , date: date ,image: image,);
        }
        ));
      },);
  }}

So how do I call this list in the search bar and when clicking on a specific book moves to its details.那么如何在搜索栏中调用此列表以及单击特定书籍时会转到其详细信息。

When searching I found that the explanations speak of communication from an external database and the data is brought by url to the search function while I need to bring it from within the application搜索时,我发现解释说的是来自外部数据库的通信,数据是由 url 带到搜索 function 而我需要从应用程序中带来的

add the following code:添加以下代码:

TextEditingController _textController = TextEditingController();
List<String> newMainList= List.from(MainList);
onItemChanged(String value) {

setState(() {
  newMainList = MainList
      .where((string) => string.toLowerCase().contains(value.toLowerCase()))
      .toList();

    
 });
}

Add this where you want your searchbar to appear:将此添加到您希望搜索栏出现的位置:

   Column(
    children: <Widget>[
      Padding(
        padding: const EdgeInsets.all(12.0),
        child: TextField(
          controller: _textController,
          decoration: InputDecoration(
            hintText: 'Search Here...',
          ),
          onChanged: onItemChanged,
        ),
      ),
      Expanded(
        child: ListView(
          padding: EdgeInsets.all(12.0),
          children: newSightsList.map((data) {
            return ListTile(
              title: Text(data),
              onTap: (){
              //add what to do onTap here
             },
            );
          }).toList(),
        ),
      );
    ],
  ),

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用列表<data>并将其传递给另一个类 - How can i use List<data> and pass it to another class 应用程序不在开发人员列表和搜索中 - Application not present in developer list and in search "如何在我的自定义非活动类中使用 findViewById for android 应用程序?" - How can I use findViewById inside my Custom Non-Actitvity Class for android application? 如何在应用程序 class 中启动 TextToSpeech,然后使用 Kotlin 在 android 的另一个活动中使用它? - How can I start the TextToSpeech in application class and then use it in another activity in android using Kotlin? 如何导出 OpenStreetMap 的瓦片并在 Flutter 应用程序中使用? - How can I export tiles of OpenStreetMap and use it in Flutter Application? 如何在Fragment类中使用Intent重定向到另一个类? - How can i use intent in Fragment class for redirecting to another class? 如何在不知道其类名或程序包名的情况下启动另一个应用程序? - How to launch another application without knowing its class name or package name? 如何从颤振中获取当前用户的信息以在我的导航栏中使用它 - How can I get current user's information from flutter to use it in my navbar 我如何在 DefaultClusterRenderer class 中加载 url 图像列表 - how can i load list of url image inside DefaultClusterRenderer class 如何在现有类中调用的OnClickListener类中使用导入的类FragmentManager? - How can I use the imported class FragmentManager inside an OnClickListener class called inside my existing class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM