当前,我正在学习使用 Flutter + Firebase 进行应用程序开发。
其中,从 Cloud Firestore 带来的数据反映在 Datatable 中,
如果您查看数据表的checkbox
日志,则在点击True
时它将变为selected
。
我正在回应,但是在 UI 上检查checkbox
时遇到问题。
因此,如果选择然后按删除按钮,则相应的数据将被删除。
但是,由于没有对 UI 进行检查,因此处于不知道是否选择了该状态的状态。
如果有人知道如何解决,请回答。
操作示例: enter image description here
插件版本: cloud_firestore:^ 0.13.7
下面实现的代码
import'dart:math';
import'package:card_manager/add_deck_page.dart';
import'package:card_manager/add_record_page.dart';
import'package:card_manager/card_page.dart';
import'package:card_manager/main.dart';
import'package:card_manager/page_manager.dart';
import'package:cloud_firestore/cloud_firestore.dart';
import'package:flutter/cupertino.dart';
import'package:flutter/material.dart';
import'package:firebase_auth/firebase_auth.dart';
import'package:provider/provider.dart';
import'login_page.dart';
class RecordPage extends StatefulWidget {
final String email;
final String name;
BattleRecordPage(this.name, this.email);
@override
_RecordPage createState() => _RecordPage();
}
class _RecordPage extends State<RecordPage> {
List<DocumentSnapshot> selectedRecords;
List<DocumentSnapshot> documents;
@override
void initState() {
// TODO: implement initState
super.initState();
selectedRecords = [];
}
onSelectedRow(bool selected, DocumentSnapshot document) async {
setState(() {
if (selected) {
selectedRecords.add(record);
} else {
selectedRecords.remove(document);
}
});
}
deleteSelected() async {
setState(() {
if (selectedRecords.isNotEmpty) {
List<DocumentSnapshot> temp = [];
temp.addAll(selectedRecords);
for (DocumentSnapshot record in temp) {
Firestore.instance
.collection("Records")
.document(record.documentId)
.delete();
selectedRecords.remove(record);
}
}
});
}
Widget recordData(documents) =>
DataTable(
showCheckboxColumn: true,
columns: const <DataColumn>[
DataColumn(
label: Text(
"email",
style: TextStyle(fontStyle: FontStyle.italic),
),
numeric: false),
DataColumn(
label: Text(
"name",
style: TextStyle(fontStyle: FontStyle.italic),
),
numeric: false),
],
rows: documents
.map<DataRow>((document) =>
DataRow(
selected: selectedRecords.contains(document),
onSelectChanged: (b) {
onSelectedRow(b, document);
},
cells: <DataCell>[
DataCell(Text(document["email"]),
showEditIcon: false, placeholder: true),
DataCell(Text(document["name"]),
showEditIcon: false, placeholder: false),
]))
.toList());
@override
Widget build(BuildContext context) {
// user information
final UserState userState = Provider.of<UserState>(context);
final FirebaseUser user = userState.user;
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("Record"),
actions: <Widget>[
IconButton(
icon: Icon(Icons.add),
onPressed: () async {
Navigator.pushNamed(context, AddRecord.routeName,
arguments: AddRecord(widget.email, widget.name));
},
),
IconButton(
icon: Icon(Icons.delete),
onPressed: () async {
await deleteSelected();
},
)
],
),
body: Column(
children: <Widget>[
Expanded(
// Create a widget based on the result of asynchronous processing
child: StreamBuilder<QuerySnapshot>(
// Asynchronous processing result → future
stream: Firestore.instance
.collection("Records")
.where("uid", isEqualTo: user.uid)
.where("name", isEqualTo: widget.name)
.orderBy("date")
.snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
documents =
snapshot.data.documents;
return SingleChildScrollView(
scrollDirection: Axis.vertical,
child: FittedBox(child: recordData(documents)));
}
return Center(
child: Text("Loading..."),
);
}),
)
],
),
);
}
}