gpt4 book ai didi

firebase - 在 Flutter StreamBuilder 中添加一个三元运算符或 if 运算符来检测输出文本中的条件?

转载 作者:行者123 更新时间:2023-12-05 04:34:26 30 4
gpt4 key购买 nike

下面的代码是一个 StreamBuilder,它从一个流中打印多个字段。是否可以向文本小部件添加三元运算符或 if 运算符以检查结果是否大于零,如果是,则向输出添加 + 前缀?如果高于零,是否也可以将文本颜色更改为绿色,如果低于零,是否也可以将文本颜色更改为红色?

                                  Text(
data['pointsfrom'].toString(),
style: GoogleFonts.poppins(
color: Colors.white70,
fontSize: 27,
),
),

完整代码

import 'package:appforpoints/dashboardPages/adminPage/listCards/admin_notif_card.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:google_fonts/google_fonts.dart';

class PointsHistory extends StatefulWidget {
final String username;
const PointsHistory({Key? key, required this.username}) : super(key: key);

@override
_PointsHistoryState createState() => _PointsHistoryState();
}

class _PointsHistoryState extends State<PointsHistory> {
@override
void initState() {
// TODO: implement initState
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xff2F3439),
body: SizedBox(
height: double.infinity,
child: Padding(
padding: const EdgeInsets.only(),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expanded(
child: ListView(
scrollDirection: Axis.vertical,
children: [
Padding(
padding: const EdgeInsets.only(
top: 80,
left: 23,
),
child: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection("pointslogs")
.where("uid",
isEqualTo:
FirebaseAuth.instance.currentUser!.uid)
.where('username', isEqualTo: widget.username)
.orderBy('timestamp', descending: true)
.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return const Text('Something went wrong');
}

if (snapshot.connectionState ==
ConnectionState.waiting) {
return const Text("Loading");
}
return Column(
children: snapshot.data!.docs.map(
(DocumentSnapshot document) {
Map<String, dynamic> data =
document.data()! as Map<String, dynamic>;
return Column(
children: [
Row(
crossAxisAlignment:
CrossAxisAlignment.center,
children: [
Text(
data['pointsfrom'].toString(),
style: GoogleFonts.poppins(
color: Colors.white70,
fontSize: 27,
),
),
Padding(
padding: const EdgeInsets.only(
left: 12, right: 12),
child: Text(
'>',
style: GoogleFonts.poppins(
color: Colors.white70,
fontSize: 27,
),
),
),
Text(
data['pointsto'].toString(),
style: GoogleFonts.poppins(
color: Colors.white70,
fontSize: 27,
),
),
Padding(
padding:
const EdgeInsets.only(left: 20),
child: Text(
data['pointschange'].toString() +
" ",
style: GoogleFonts.poppins(
color: Colors.white70,
fontSize: 27,
),
),
),
],
),
Row(
children: [
Text(
data['date'].toString(),
),
Text(
data['time'].toString(),
),
],
)
],
);
},
).toList(),
);
},
),
),
],
),
),
],
),
),
),
);

/*Scaffold(
backgroundColor: const Color(0xff1e272c),
body: ListView(
scrollDirection: Axis.horizontal,
children: [
StreamBuilder<QuerySnapshot>(
stream: userLog,
builder:
(BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return const Text('Something went wrong');
}

if (snapshot.connectionState == ConnectionState.waiting) {
return const Text("Loading");
}
return Row(
children: snapshot.data!.docs.map(
(DocumentSnapshot document) {
Map<String, dynamic> data =
document.data()! as Map<String, dynamic>;
return Column(
children: [
Text(data['pointschange']),
Text(data['pointsto']),
],
);
},
).toList(),
);
},
),
],
),
);*/
}
}

最佳答案

你的意思是这样的:

Text(
(data['pointsfrom'] >= 0 ? '+' : '') + data['pointsfrom'].toString(),
style: GoogleFonts.poppins(
color: data['pointsfrom'] >= 0 ? Colors.green : Colors.red,
fontSize: 27,
),
),

建议:我注意到您一遍又一遍地重复样式 (GoogleFonts.poppins);您可以在构建方法的顶部设置一次,也可以创建一个单独的实用程序类,您可以在其中拥有所有这些值。您可以根据条件创建样式,您也可以将其提取为单独的方法,根据所需条件为您生成适当的样式。

您还可以将前缀逻辑移动到一个单独的方法中,这样三元操作看起来不会太乱。

关于firebase - 在 Flutter StreamBuilder 中添加一个三元运算符或 if 运算符来检测输出文本中的条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71229358/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com