enter image description here
我尝试使用singlescrollview或flexible。它不起作用。我使用expanded,但它仍然呈现那样。圆形的图像配置文件和用户名波纹管无法看到。它看起来很好,在开始,但突然成为一个渲染,我没有一个线索。也许有一些错误,在我的代码?
这是密码
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:instagram/resources/auth_methods.dart';
import 'package:instagram/resources/firestore_methods.dart';
import 'package:instagram/screens/login_screen.dart';
import 'package:instagram/utils/colors.dart';
import 'package:instagram/utils/utils.dart';
import 'package:instagram/widgets/follow_button.dart';
class ProfileScreen extends StatefulWidget {
final String uid;
const ProfileScreen({super.key, required this.uid});
@override
State<ProfileScreen> createState() => _ProfileScreenState();
}
class _ProfileScreenState extends State<ProfileScreen> {
var userData = {};
int postLen = 0;
int followers = 0;
int following = 0;
bool isFollowing = false;
bool isLoading = false;
@override
void initState() {
// TODO: implement initState
super.initState();
getData();
}
getData() async {
setState(() {
isLoading = true;
});
try {
var userSnap = await FirebaseFirestore.instance
.collection('users')
.doc(widget.uid)
.get();
//get post length
var postSnap = await FirebaseFirestore.instance
.collection('posts')
.where('uid', isEqualTo: FirebaseAuth.instance.currentUser!.uid)
.get();
postLen = postSnap.docs.length;
userData = userSnap.data()!;
followers = userSnap.data()!['followers'].length;
following = userSnap.data()!['following'].length;
isFollowing = userSnap
.data()!['followers']
.contains(FirebaseAuth.instance.currentUser!.uid);
setState(() {});
} catch (e) {
var snackbar = SnackBar(content: Text(e.toString()));
ScaffoldMessenger.of(context).showSnackBar(snackbar);
}
setState(() {
isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return isLoading
? const Center(
child: CircularProgressIndicator(),
)
: Scaffold(
appBar: AppBar(
backgroundColor: mobileBackgroundColor,
title: Text(
userData['username'],
),
centerTitle: false,
),
body: ListView(
children: [
Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
Row(
children: [
CircleAvatar(
backgroundColor: Colors.grey,
backgroundImage: NetworkImage(
userData['photoUrl'],
),
radius: 40,
),
Expanded(
child: Column(
children: [
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
children: [
buildStateColumn(postLen, "posts"),
buildStateColumn(followers, "followers"),
buildStateColumn(following, "following"),
],
),
Row(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
children: [
FirebaseAuth.instance.currentUser!.uid ==
widget.uid
? FollowButton(
text: 'sign out',
backgroundColor:
mobileBackgroundColor,
textColor: primaryColor,
borderColor: Colors.grey,
function: () async {
await AuthMethods().signOut();
Navigator.of(context)
.pushReplacement(
MaterialPageRoute(
builder: (context) =>
const LoginScreen(),
),
);
},
)
: isFollowing
? FollowButton(
text: 'unfollow',
backgroundColor: Colors.white,
textColor: Colors.black,
borderColor: Colors.grey,
function: () async {
await FirestoreMethods()
.followUser(
FirebaseAuth.instance
.currentUser!.uid,
userData['uid'],
);
setState(() {
isFollowing = false;
followers--;
});
},
)
: FollowButton(
text: 'follow',
backgroundColor: Colors.blue,
textColor: Colors.white,
borderColor: Colors.blue,
function: () async {
await FirestoreMethods()
.followUser(
FirebaseAuth.instance
.currentUser!.uid,
userData['uid'],
);
setState(() {
isFollowing = true;
followers++;
});
},
)
],
),
],
),
),
],
),
Container(
alignment: Alignment.centerLeft,
padding: const EdgeInsets.only(
top: 15,
),
child: Text(
userData['username'],
style: const TextStyle(
fontWeight: FontWeight.bold,
),
),
),
Container(
alignment: Alignment.centerLeft,
padding: const EdgeInsets.only(
top: 1,
),
child: Text(
userData['bio'],
),
),
],
),
),
const Divider(),
FutureBuilder(
future: FirebaseFirestore.instance
.collection('posts')
.where('uid', isEqualTo: widget.uid)
.get(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
return GridView.builder(
shrinkWrap: true,
itemCount: (snapshot.data! as dynamic).docs.length,
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
crossAxisSpacing: 5,
mainAxisSpacing: 1.5,
childAspectRatio: 1,
),
itemBuilder: (context, index) {
DocumentSnapshot snap =
(snapshot.data! as dynamic).docs[index];
return Container(
child: Image(
image: NetworkImage(
snap['postUrl'],
),
fit: BoxFit.cover,
),
);
},
);
},
),
],
),
);
}
Column buildStateColumn(int num, String label) {
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
num.toString(),
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
Container(
margin: const EdgeInsets.only(top: 4),
child: Text(
label,
style: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.w400,
color: Colors.grey,
),
),
),
],
);
}
}
`
1条答案
按热度按时间ar7v8xwq1#
**您应该提供minimal reproducible example。**尝试模拟您希望在应用中执行的操作,以便我们提供帮助!
我试着根据你的代码来做,我删除了与firebase相关的代码,用
ElevatedButton
替换了FollowButton
,只是为了能够运行它,它运行得很好(没有错误)。下面是完整的代码(你可以复制粘贴它在一个新的flutter项目中运行,实验一下,如果你有一个可以重现的问题示例,请回来):