我有一个需要保留的数据列表,我不知道从哪里开始,我只是在做些什么背景。一个通过http请求控制树莓派gpio引脚的应用程序,尽管目前我只是在ui上工作,实际上还没有开始使Web服务器处理http请求。
以下是即时通讯用于开发的模型以及我要保存的模型。
class Pin {
int index;
int pinNum;
String name;
bool isOn;
Pin (int pinNum, String name, bool isOn, int index) {
this.pinNum = pinNum;
this.name = name;
this.isOn = isOn;
this.index = index;
}
}
Pin controller1 = Pin(18, "light", true, 0);
Pin controller2 = Pin(20, "tv", false, 1);
Pin controller3 = Pin(24, "blender", true, 2);
Pin controller4 = Pin(34, "television", false, 3);
List <Pin>controllers = [
controller1,
controller2,
controller3,
controller4,
];
这是我的列表视图构建器所使用的代码(对不起,我仍在学习格式不佳的问题)
import 'package:flutter/material.dart';
import "package:gpio_control/models/pin.dart";
import 'package:slide_popup_dialog/slide_popup_dialog.dart' as slideDialog;
class GpioScreen extends StatefulWidget {
@override
_GpioScreenState createState() => _GpioScreenState();
}
class _GpioScreenState extends State<GpioScreen> {
@override
Widget build(BuildContext context) {
int selectedIndex = 0;
return Scaffold(
backgroundColor: Colors.grey[850],
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
appBar: AppBar(
title: Text(
"Gpio Control",
style: TextStyle(
color: Colors.yellow[200]
),
),
backgroundColor: Colors.black26,
centerTitle: true,
elevation: 1,
actions: <Widget>[
IconButton(
icon: Icon(
Icons.settings,
color: Colors.yellow[200],
),
iconSize: 40,
onPressed: () {
Navigator.pushNamed(context, '/settings');
}, //settings
)
],
),
floatingActionButton: SizedBox(
width: 200,
height: 40,
child: FloatingActionButton(
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(45)
),
backgroundColor: Colors.yellow[200],
onPressed: () {
Navigator.pushNamed(context, '/addPin');
},
child: Icon(
Icons.add,
color: Colors.black54,
size: 30,
),
),
),
body: Container(
child: Padding(
padding: const EdgeInsets.only(bottom: 80),
child: ListView.builder(
itemCount: controllers.length,
itemBuilder: (context, i) {
void _showDialog() {
slideDialog.showSlideDialog(
context: context,
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(20,80,5,10),
child: Text(
"Are you sure you want to delete this configuration?",
style: TextStyle(
color: Colors.yellow[200],
fontSize: 20,
fontWeight: FontWeight.bold
),
),
),
Text(
"(Any deleted configurations can not be recoved)",
style: TextStyle(
color: Colors.yellow[200],
fontSize: 10,
fontWeight: FontWeight.bold
),
),
Padding(
padding: const EdgeInsets.only(top: 0),
child: ButtonTheme(
minWidth: 300,
child: FlatButton(
onPressed: () {
setState(() {
selectedIndex = i;
controllers.remove(controllers.removeAt(selectedIndex));
Navigator.of(context).pop();
});
},
color: Colors.yellow[200],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
side: BorderSide(color: Colors.yellow[200])
),
child: Text(
"Delete"
),
),
),
),
],
),
barrierColor: Colors.yellow[100].withOpacity(0.7),
pillColor: Colors.yellow[200],
backgroundColor: Colors.black54,
);
}
return Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
color: Colors.yellow[200],
child: Padding(
padding: const EdgeInsets.fromLTRB(25, 0, 0, 0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(6, 6, 0, 0),
child: Text("pin"),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(bottom: 15),
child: Text(
controllers[i].pinNum.toString(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 25
),
),
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment
.spaceEvenly,
children: <Widget>[
Center(
child: Padding(
padding: const EdgeInsets.fromLTRB(
25, 0, 0, 25),
child: Text(
controllers[i].name,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w400
),
),
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment
.spaceEvenly,
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(
0, 0, 10, 20),
child: IconButton(
onPressed: () {
setState(() {
if (controllers[i].isOn) {
controllers[i].isOn = false;
print(controllers[i].isOn);
}
else {
controllers[i].isOn = true;
}
});
},
icon: Icon(
Icons.power_settings_new,
color: controllers[i].isOn == true
? Colors.green[600]
: Colors.red[600]
),
color: Colors.yellow[150],
),
),
Padding(
padding: const EdgeInsets.fromLTRB(
0, 0, 0, 20),
child: GestureDetector(
onTap: () {
setState(() {
selectedIndex = i;
print("new index");
});
},
child: IconButton(
onPressed: () {
_showDialog();
},
icon: Icon(Icons.delete),
iconSize: 30,
),
),
)
],
)
],
),
]),
),
),
);
}
),
),
),
);
}
}
如果您需要更多背景信息,请随时与我联系。
答案 0 :(得分:0)
如果您不打算拥有成百上千的控制器,我建议您使用Shared Preferences而不是SQLite,它会更容易并且几乎不存在性能损失。
您可以为每个控制器手动声明每个key:value对,但这并不是一个好的解决方案,尤其是如果将来您最终需要更多的控制器时。相反,我建议您修改Pin类,使其包含json到/来自映射的映射,并再修改一个类以存储所有pin,如下所示:
var arr = [1, 2, 3];
var max = Math.max(...arr);
然后创建用于从共享首选项加载/保存/删除AllPins的方法,如下所示:
class test {
number: x
other_fields
}
var arr = [test1, test2, test3];
var max = Math.max(...arr);
最后,这是如何在应用程序中的某处使用上面的代码:
class AllPins {
List<Pin> allPins;
AllPins(this.allPins);
AllPins.fromJson(Map<String, dynamic> json) {
if (json['AllPins'] != null) {
allPins = new List<Pin>();
json['AllPins'].forEach((v) {
allPins.add(new Pin.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.allPins != null) {
data['AllPins'] = this.allPins.map((v) => v.toJson()).toList();
}
return data;
}
}
class Pin {
int index;
int pinNum;
String name;
bool isOn;
Pin (int index, int pinNum, String name, bool isOn) {
this.pinNum = pinNum;
this.name = name;
this.isOn = isOn;
this.index = index;
}
Pin.fromJson(Map<String, dynamic> json) {
index = json['index'];
pinNum = json['pinNum'];
name = json['name'];
isOn = json['isOn'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['index'] = this.index;
data['pinNum'] = this.pinNum;
data['name'] = this.name;
data['isOn'] = this.isOn;
return data;
}
}
另外,您可以稍后使用已经制作好的json映射,以便稍后从Web服务器发布/获取数据。