我希望条件语句将集合中“dropdowncompany”字段数据的 2 个函数分类为“pr_details”,因此它们是该字段中的两个值
-----示例图片--------- --------示例图片---------
我真正需要的是条件语句函数
---------概述Purchase()函数------------
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class OverviewPurchase extends StatefulWidget {
const OverviewPurchase({super.key});
@override
State<OverviewPurchase> createState() => _OverviewPurchaseState();
}
class _OverviewPurchaseState extends State<OverviewPurchase> {
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
body: StreamBuilder(
stream:
FirebaseFirestore.instance.collection("pr_details").snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
return Padding(
padding: EdgeInsets.all(60.0),
child: Container(
width: size.width * 0.2,
child: ListView(
children: snapshot.data!.docs.map((document) {
return Column(
children: [
SizedBox(
height: 30,
),
Card(
borderOnForeground: true,
color: Colors.lightBlueAccent[100],
child: ListTile(
leading: IconButton(
onPressed: () {},
alignment: Alignment.topLeft,
icon: Icon(Icons.list_alt_outlined),
iconSize: 30,
),
title: Column(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Chip(
label: Text("ใบขอซื้อ Synatic"),
backgroundColor: Colors.deepPurpleAccent,
labelStyle: TextStyle(color: Colors.white),
),
SizedBox(
height: 20,
),
],
),
subtitle: Column(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(document["purchase_req"]),
Text("บริษัท : " +
document["dropdowncompany"]),
Text("แผนก : " + document["dropdowndep"]),
Text("ประเภทงาน : " +
document["dropdownwork"]),
Text(
"ชื่อ-นามสกุล : " + document["fsname"]),
//Text("แผนก : " + document["department"]),
Text(
"วันที่เขียน : " + document["date_pr"]),
Text(
"จำนวนรายการ : ${int.parse((document['number1']).length.toString()) + int.parse((document['number2']).length.toString()) + int.parse((document['number3']).length.toString()) + int.parse((document['number4']).length.toString()) + int.parse((document['number5']).length.toString()) + int.parse((document['number6']).length.toString()) + int.parse((document['number7']).length.toString()) + int.parse((document['number8']).length.toString()) + int.parse((document['number9']).length.toString()) + (int.parse((document['number10']).length.toString()) - 1)}")
],
))),
],
);
}).toList()),
),
);
}),
);
}
}
---------概述Purchase()函数------------
----------概述Purchase1()函数------------
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class OverviewPurchase1 extends StatefulWidget {
const OverviewPurchase1({super.key});
@override
State<OverviewPurchase1> createState() => _OverviewPurchase1State();
}
class _OverviewPurchase1State extends State<OverviewPurchase1> {
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
body: StreamBuilder(
stream:
FirebaseFirestore.instance.collection("pr_details").snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
return Padding(
padding: EdgeInsets.all(60.0),
child: Container(
width: size.width * 0.2,
child: ListView(
children: snapshot.data!.docs.map((document) {
return Column(
children: [
SizedBox(
height: 30,
),
Card(
borderOnForeground: true,
color: Colors.lightGreenAccent[100],
child: ListTile(
leading: IconButton(
onPressed: () {},
alignment: Alignment.topLeft,
icon: Icon(Icons.list_alt_outlined),
iconSize: 30,
),
title: Column(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Chip(
label: Text("ใบขอซื้อ Synovic"),
backgroundColor: Colors.deepPurpleAccent,
labelStyle: TextStyle(color: Colors.white),
),
SizedBox(
height: 20,
),
],
),
subtitle: Column(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(document["purchase_req"]),
Text("บริษัท : " +
document["dropdowncompany"]),
Text("แผนก : " + document["dropdowndep"]),
Text("ประเภทงาน : " +
document["dropdownwork"]),
Text(
"ชื่อ-นามสกุล : " + document["fsname"]),
Text(
"วันที่เขียน : " + document["date_pr"]),
Text(
"จำนวนรายการ : ${int.parse((document['number1']).length.toString()) + int.parse((document['number2']).length.toString()) + int.parse((document['number3']).length.toString()) + int.parse((document['number4']).length.toString()) + int.parse((document['number5']).length.toString()) + int.parse((document['number6']).length.toString()) + int.parse((document['number7']).length.toString()) + int.parse((document['number8']).length.toString()) + int.parse((document['number9']).length.toString()) + (int.parse((document['number10']).length.toString()) - 1)}")
],
))),
],
);
}).toList()),
),
);
}),
);
}
}
----------概述Purchase1()函数------------
最后输出的应该是这张公会图片
很难理解你的问题。根据我的理解,如果companyName = 'A',则在蓝色容器中显示数据,如果companyName = 'B',则在绿色容器中显示数据。 我在这两个文件中运行了差异检查器,唯一的区别是颜色和标题。
color: document["dropdowncompany"] == 'ซินเนติก' ? Colors.lightBlueAccent[100] : Colors.lightGreenAccent[100],
这是要做的改变。您可以对标题进行类似的更改。另外,您不需要制作 2 个小部件。只需要1个
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class OverviewPurchase extends StatefulWidget {
const OverviewPurchase({super.key});
@override
State<OverviewPurchase> createState() => _OverviewPurchaseState();
}
class _OverviewPurchaseState extends State<OverviewPurchase> {
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
body: StreamBuilder(
stream:
FirebaseFirestore.instance.collection("pr_details").snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
return Padding(
padding: EdgeInsets.all(60.0),
child: Container(
width: size.width * 0.2,
child: ListView(
children: snapshot.data!.docs.map((document) {
return Column(
children: [
SizedBox(
height: 30,
),
Card(
borderOnForeground: true,
color: document["dropdowncompany"] == 'ซินเนติก' ? Colors.lightBlueAccent[100] : Colors.lightGreenAccent[100],
child: ListTile(
leading: IconButton(
onPressed: () {},
alignment: Alignment.topLeft,
icon: Icon(Icons.list_alt_outlined),
iconSize: 30,
),
title: Column(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Chip(
label: Text("ใบขอซื้อ Synatic"),
backgroundColor: Colors.deepPurpleAccent,
labelStyle: TextStyle(color: Colors.white),
),
SizedBox(
height: 20,
),
],
),
subtitle: Column(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(document["purchase_req"]),
Text("บริษัท : " +
document["dropdowncompany"]),
Text("แผนก : " + document["dropdowndep"]),
Text("ประเภทงาน : " +
document["dropdownwork"]),
Text(
"ชื่อ-นามสกุล : " + document["fsname"]),
//Text("แผนก : " + document["department"]),
Text(
"วันที่เขียน : " + document["date_pr"]),
Text(
"จำนวนรายการ : ${int.parse((document['number1']).length.toString()) + int.parse((document['number2']).length.toString()) + int.parse((document['number3']).length.toString()) + int.parse((document['number4']).length.toString()) + int.parse((document['number5']).length.toString()) + int.parse((document['number6']).length.toString()) + int.parse((document['number7']).length.toString()) + int.parse((document['number8']).length.toString()) + int.parse((document['number9']).length.toString()) + (int.parse((document['number10']).length.toString()) - 1)}")
],
))),
],
);
}).toList()),
),
);
}),
);
}
}