在 SQLite 数据库 flutter 中搜索

HDi*_*ond 1 sqlite dart flutter

如何使用 TextField 小部件作为搜索栏来查询本地 SQLite 数据库资产的结果?

还有比使用文本字段小部件更好的选择吗?例子将非常感激。

用代码编辑:

 padding: EdgeInsets.all(10.0),
        child: Column(
          children: <Widget>[
            Container(
              padding: const EdgeInsets.all(8.0),
              child: new Container(
                  height: 70.0,
                  color: Theme.CompanyColors.iBlue,
                  child: new Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: new Card(
                      child: new Container(
                        child: new Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            new Icon(Icons.search),
                            new Container(
                              width: MediaQuery.of(context).size.width - 100.0,
                              child: new TextField(
                                decoration: InputDecoration(
                                  hintText: 'Search',
                                  //onChanged:
                                  //onSearchTextChanged,
                                ),
                              ),
                            ),
Run Code Online (Sandbox Code Playgroud)

这是搜索栏的代码

我的数据库有一个附有paragraph_id的表句子,所以我想通过这样的查询进行搜索

select * from sentences where title or body = userinput%
Run Code Online (Sandbox Code Playgroud)

Yam*_*min 5

假设你正在使用这个插件。您应该创建并填充数据库(插件的文档)。

然后,在搜索小部件中,使用以下示例:

class _MyHomePageState extends State<MyHomePage> {
  Database database;

  @override
  void initState() {
    // open the database
    openDatabase('pathToDb', version: 1,
        onCreate: (Database db, int version) async {
      database = db;
      // When creating the db, create the table

    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (database == null) {
      return CircularProgressIndicator();
    }
    return Container(
        padding: EdgeInsets.all(10.0),
        child: Column(children: <Widget>[
          Container(
              padding: const EdgeInsets.all(8.0),
              child: new Container(
                  height: 70.0,
                  child: new Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: new Card(
                          child: new Container(
                              child: new Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: <Widget>[
                            new Icon(Icons.search),
                            new Container(
                              width: MediaQuery.of(context).size.width - 100.0,
                              child: new TextField(
                                  decoration: InputDecoration(
                                    hintText: 'Search',
                                    //onSearchTextChanged,
                                  ),
                                  onChanged: (String text) async {
                                    List<Map> res = await database.rawQuery(
                                        "SELECT * FROM sentences WHERE title LIKE '%${text}%' OR  body LIKE '%${text}%'");
                                    print(res);
                                  }),
                            )
                          ]))))))
        ]));
  }
}
Run Code Online (Sandbox Code Playgroud)

注意:如果多个小部件可以访问数据库,请使用 SQL 帮助程序。阅读文档的“SQL 帮助程序”部分。这个例子只是为了你的例子。