模型:为类别制作了这个模型。
import 'package:cloud_firestore/cloud_firestore.dart';
class CatagoryModel {
String id;
String catagoryname;
Timestamp date;
bool status;
String user;
CatagoryModel(this.id, this.catagoryname, this.date, this.user, this.status);
CatagoryModel.fromDocumentSnapshot(DocumentSnapshot documentSnapshot) {
id = documentSnapshot.id;
catagoryname = documentSnapshot.data()['catagoryname'];
date = documentSnapshot.data()['date'];
user = documentSnapshot.data()['user'];
status = documentSnapshot.data()['status'];
}
}
服务:此代码处理模型和 Firestore 数据并将其传递给 Getx 控制器。
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:get/get.dart';
import 'package:talhatraders/app/data/catagorymodel.dart';
class Database {
final FirebaseFirestore _firebase = FirebaseFirestore.instance;
Future<void> catagoryAdd(String catagoryname,bool status) async {
try {
await _firebase.collection('catagorydb').add({
'date': Timestamp.now(),
'status': status,
'catagoryname': catagoryname,
'user':'user1'
});
} catch (e) {
print(e);
}
}
Future<void> catagoryStatus(CatagoryModel catagorymodel)async{
try {
await _firebase.collection('catagorydb').doc(catagorymodel.id).update({'status':!catagorymodel.status});
} catch (e) {
print(e);
}
}
Future<void> catagoryDelete(CatagoryModel catagorymodel)async{
try {
await _firebase.collection('catagorydb').doc(catagorymodel.id).delete();
} catch (e) {
}
}
Stream<List<CatagoryModel>> catagoryStream() {
return _firebase
.collection('catagorydb')
.snapshots()
.map((QuerySnapshot querysnapshot) {
List<CatagoryModel> todoData= List();
querysnapshot.docs.forEach((element) {
todoData.add(CatagoryModel.fromDocumentSnapshot(element));
});
return todoData;
});
}
}
Getx 控制器:这里我使用了 obs 字符串,该字符串在从 Dropbox 中选择新值时会发生变化
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:talhatraders/app/data/catagorymodel.dart';
import 'package:talhatraders/app/modules/catagory/controllers/catagoryservice.dart';
class ProductentryController extends GetxController {
Rx<List<CatagoryModel>> catagoryList = Rx<List<CatagoryModel>>();
List<CatagoryModel> get catagories => catagoryList.value;
var selectedItem = "Select Catagory..".obs;
void changeSelect(String select) {
selectedItem.value = select;
}
@override
void onInit() {
catagoryList.bindStream(Database().catagoryStream());
}
}
GetxViewPaege:在 ViewPage UI 中工作正常,但从下拉列表中选择新值时值没有改变
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter/services.dart';
import 'package:talhatraders/app/data/catagorymodel.dart';
import 'package:talhatraders/app/modules/catagory/controllers/catagory_controller.dart';
import 'package:talhatraders/app/modules/productentry/controllers/productentry_controller.dart';
class ProductentryView extends GetView<ProductentryController> {
ProductentryController productController = Get.put(ProductentryController());
final FirebaseFirestore _firebase = FirebaseFirestore.instance;
//Color currentColor =Colors.limeAccent;
String catagory;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('New Item'),
actions: [],
),
body: Column(children: [
GetX<ProductentryController>(
init: Get.put<ProductentryController>(ProductentryController()),
builder: (ProductentryController productentryController) {
return Expanded(
child: Container(
child: Padding(
padding: const EdgeInsets.only(
bottom: 12,top:40, left: 10.0, right: 15.0),
child: InputDecorator(
decoration: InputDecoration(
fillColor: Colors.transparent,
hintStyle: Theme.of(context).textTheme.bodyText2,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(15),
borderSide: BorderSide(
width: 10,
style: BorderStyle.none,
),
),
filled: true,
contentPadding: EdgeInsets.all(16),
),
child: DropdownButtonHideUnderline(
child: DropdownButton(
isExpanded: true,
isDense:
true, // Reduces the dropdowns height by +/- 50%
icon: Icon(Icons.keyboard_arrow_down),
hint: Text(
'${productentryController.selectedItem.toString()}'),
value: catagory,
items: productentryController.catagories
.map((CatagoryModel item) {
return DropdownMenuItem(
value: item.catagoryname,
child: Text(item.catagoryname),
);
}).toList(),
onChanged: (selectedItem) {
productentryController.changeSelect(Item);
catagory=productController.selectedItem.value;
}),
)),
),
),
);
},
),
]),
);
}
}