用sqlite下拉-颤振 [英] Drop Down with sqlite - Flutter

查看:72
本文介绍了用sqlite下拉-颤振的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据库sqlite数据,我想在下拉列表中显示我的数据,并更改表中行的ID,因为将来我想创建另一个下拉列表以更改为第一个下拉列表的值,有人可以提供帮助吗?

I have database sqlite data and i want to show my data in drop down with changed id of my rows in table because in future i want to create another drop down to change to value of first drop down anyone can help ?

推荐答案

在Flutter上使用SQLite

要从SQLite数据库中收集数据,可以使用 sqflite插件(如果是iOS或Android设备).您必须将依赖项添加到pubspec.yaml.

Working with SQLite on flutter

To gather the data from a SQLite database you could use the sqflite plugin (independently if is an iOS or Android device). You have to add the dependency to your pubspec.yaml.

dependencies:
  ...
  sqflite: any

要使用sqflite时,必须导入库.

When you want to use sqflite you have to import the library.

import 'package:sqflite/sqflite.dart';

接下来,您必须打开与SQLite的连接,在这里我们创建一个表,以防您没有表

Next, you have to open a connection to SQLite, here we create a table in case you didn't have one

Database database = await openDatabase(path, version: 1,
    onCreate: (Database db, int version) async {
  await db.execute(
      'CREATE TABLE Test (id INTEGER PRIMARY KEY, name TEXT, value INTEGER, num REAL)');
});

您可以使用database.rawQuery插入或检索数据.

You can insert or retrieve data using database.rawQuery.

插入:

int primaryKeyInsertedRow = await database.rawQuery('INSERT INTO Test(name, value, num) VALUES("some name", 1234, 456.789)');

选择:

List<Map> list = await database.rawQuery('SELECT * FROM Test');

完成操作后,请记住要关闭数据库.

Remember to close the database when you are done with it.

await database.close();

显示下拉菜单列表

要显示您首先检索的数据,您必须创建一个扩展StatefulWidget的类,覆盖createState()方法并设置自己的状态(在本示例中为SettingWidgetState)

Displaying a drop down menu list

For displaying the data you retrieve first you have to create a class that extends StatefulWidget, override the createState() method, and set your own state (in this example, SettingWidgetState)

@override
_SettingsWidgetState createState() => new _SettingsWidgetState();

第二,您应该为其定义一个状态,定义一个扩展State<NameOfYourWidget>的类.在该类中,您应该有一个DropdownMenuItem<String>列表和当前所选元素的字符串成员.

Second you should define a state for it, defining a class that extends State<NameOfYourWidget>. In that class you should have a list of DropdownMenuItem<String> and a string member of the current selected element.

为方便起见,在本示例中,我们将使用静态城市列表:

For the sake of convenience, in this example we are going to use a static list of cities:

List _cities = [
  "Cluj-Napoca",
  "Bucuresti",
  "Timisoara",
  "Brasov",
  "Constanta"
];

接下来,我们覆盖initState(),将DropDownMenuItem的列表设置为列表和当前选定的列表元素.之后,我们应该呼叫super.initState().

Next, we override initState() setting our list of DropDownMenuItem to our list and the currently selected list element. After that we should call super.initState().

此外,我们需要重写build()方法.目标是返回包含DropDownButtonContainer,并且DropDownButton分配了项目列表(在类中定义),所选元素(也在类中定义)和事件处理程序. onChanged:属性(此处还插入了其他小部件,以使其看起来更漂亮)

Also, we need to override the build() method. The goal is to return a Container that contains a DropDownButton, and that DropDownButton has assigned the list of items (defined in the class), the selected element (also defined in the class) and a event handler for the onChanged: property (here also are inserted additional widgets with the purpose of making it look nice)

@override
Widget build(BuildContext context) {
  return new Container(
    color: Colors.white,
    child: new Center(
        child: new Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        new Text("Please choose your city: "),
        new Container(
          padding: new EdgeInsets.all(16.0),
        ),
        new DropdownButton(
          value: _currentCity,
          items: _dropDownMenuItems,
          onChanged: changedDropDownItem,
        )
      ],
    )),
  );
}

最后,我们定义了从列表中选择一个新项时将要调用的方法(在我们的示例中为changeDropDownItem(string selectedCity)).

Lastly we define the method that is going to be called when a new item is selected from the list (changeDropDownItem(string selectedCity) in our example).

  void changedDropDownItem(String selectedCity) {
    setState(() {
      _currentCity = selectedCity;
    });
  }
}

链接,其中我基于下拉列表的答案.您也可以开始使用sqflite插件

Link where I based my answer for drop down list. You can check out too getting started with sqflite plugin

这篇关于用sqlite下拉-颤振的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆