【发布时间】:2021-05-22 02:59:02
【问题描述】:
好的,这个很奇怪。我的 4 个小部件列中只有一个小部件的 FittedBox 宽度 > 0 != true。当我删除该小部件(文本小部件)时,错误不会显示。奇怪的是,其他小部件也是fittedBoxes 中的文本小部件。字面意思是一样的。这是代码。我主页的正文是带有标题的NestedScrollView
headerSliverBuilder: (_,__)=>[
SliverToBoxAdapter(
child: ProfileHeader(uid: widget.profileId),
),
SliverPersistentHeader(
pinned: true,
delegate: ProfileScreenPersistentHeader(
tabBar: ProfileTabbar(tabController: _tabController)
)
),
],
问题出在ProfileHeader(uid: widget.profileId),。在这里我有一个构建:
return Container(
//color: Colors.pink[100],
padding: EdgeInsets.symmetric(horizontal: 16),
child: Column(
// ignore: missing_required_param
children: [
SizedBox(height:8),
Heading( fullName: _fullName, userName: _userName,lastSeen: _lastActive, photoUrl: _photoUrl, ratingsList: _ratingsList, ),
SizedBox(height:16),
FollowAndMessageButtons(profileUid: widget.userModel.uid, ),
NumbersSection(followerCount: _followerCount, followingCount: _followingCount, itemsSold: _itemsSold,),
Description(description: _description, website: _website,)
]
//have an if to check wether to show edit button or followbutton
)
);
Heading() 是更具体的问题所在,这里是标题:
final String photoUrl;
final String fullName;
final String userName;
final List<double> ratingsList;
final String lastSeen;
const Heading({Key key, @required this.photoUrl, @required this.fullName,
@required this.userName, @required this.ratingsList, @required this.lastSeen}) : super(key: key);
@override
Widget build(BuildContext context) {
final double x = SizeConfig.blockSizeHorizontal * 100;
final double y = SizeConfig.blockSizeVertical * 100;
final cardColor = Theme.of(context).cardColor;
final activeColor = Theme.of(context).accentColor;
final highlightColor = Theme.of(context).highlightColor;
final mainTextColor = Theme.of(context).primaryColor;
return Container(
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
padding: EdgeInsets.only(right: 16),
child: CircleAvatar(
radius: 0.1*x,
backgroundColor: cardColor,
backgroundImage: (photoUrl == null || photoUrl.isEmpty)
?AssetImage('lib/images/PP.png')
:CachedNetworkImageProvider(photoUrl),
),
),
Expanded(
//flex: 3,
child:
Column(
//mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
FittedBox(
fit: BoxFit.fitWidth,
child: Text(
"$fullName",
style: TextStyle(fontFamily: "Montserrat", fontWeight: FontWeight.w700, color: mainTextColor, fontSize: 16)
),
),
FittedBox(
fit: BoxFit.fitWidth,
child: Text(
"@$userName",
style: TextStyle(fontFamily: "Lekton", fontWeight: FontWeight.w600, color: mainTextColor, fontSize: 16)
),
),
FittedBox(
fit: BoxFit.fitWidth,
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Icon(Icons.remove_red_eye, color: highlightColor, size: 11,),
SizedBox(width:1),
Text(
"Active $lastSeen",
style: TextStyle(fontFamily: "Montserrat", fontWeight: FontWeight.w500, color: highlightColor, fontSize: 10)
),
],
),
),
FittedBox(
fit: BoxFit.fitWidth,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
//STARS HERE
StarsReadOnly(size: 12, rating: Services().average(ratingsList), spacing: 1),
SizedBox(width: 2),
Text("(${ratingsList.length})",
style: TextStyle(fontFamily: "Montserrat", fontWeight: FontWeight.w500, color: activeColor, fontSize: 12)
),
],
),
//stars
),
],
),
),
],
),
);
}
}
给出错误的列中的子项是
FittedBox(
fit: BoxFit.fitWidth,
child: Text(
"$fullName",
style: TextStyle(fontFamily: "Montserrat", fontWeight: FontWeight.w700, color: mainTextColor, fontSize: 16)
),
),
这是第一个元素。当我把它拿出来时,错误消失了。我什至尝试减小字体大小,但仍然没有。但是当我对文本进行硬编码时,错误消失了。我正在从一个用户模型初始化文本,小部件从树的更远的未来构建器接收到该文本。一个文本小部件(与其他小部件相似)如何给出错误但其他小部件却不给出错误。我什至尝试删除其他小部件,因为我认为这是垂直空间的问题,但事实并非如此。我什至尝试删除circleavatar,因为......
【问题讨论】:
-
我相信这是因为在未来返回之前,该小部件上的文本为空,但其他小部件上的文本为空。尝试在
$fullName之后添加一个空格以进行快速修复。我相信您可以调整您的代码以获得更好的解决方案。 -
好的。我现在就试试。肯定会调整我的代码,但像 $userName 这样的其他小部件也来自未来,所以还是有点令人费解
-
与我所知的不同之处在于,即使未来尚未返回,因此变量为空,传递给
Text的字符串也不是,即'@'在'@$userName'与'$fullName'。因此,Text小部件永远不会是空字符串,因此永远不会隐含宽度为 0。 -
是的,你完全正确!现在才弄清楚,我今天晚些时候再测试一下,让你知道是否提交作为答案
-
@Lee3 加个空格修复了!