flutter - 动态设置连续项目的大小
问题描述
我犯了一个错误,首先只在 iOS 上使用我的代码,然后当我去检查 Android 应用程序时,格式都搞砸了,但在 iOS 上仍然很好。
下面是每个的样子。 iOS 格式看起来不错, Android 格式已关闭
所以在小盒子里有一个容器,里面有一排包含图标和文本的容器。当我的 firebase 端更新时,它会将它变成一张卡片,它是可点击的。
我的问题是我已经在使用 AutoSizeText 并且在这种情况下它对我没有帮助,我尝试了使用布局构建器设置最大尺寸约束的解决方案,但也没有这样做。
知道如何设置它以便动态设置文本和图标大小,甚至动态设置图标大小吗?
我当前的代码如下:
class AlertWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('alerts')
.where('access',
arrayContains: FirebaseAuth.instance.currentUser.uid)
.where('read', isEqualTo: false)
//.orderBy('createdAt', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData || snapshot.data.docs.length <= 0) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
bottomLeft: Radius.circular(10),
bottomRight: Radius.circular(10)),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(0, 3), // changes position of shadow
),
],
),
height: 60.0,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(width: 10.0),
Icon(
Icons.check_box,
color: Colors.green,
size: 60,
),
SizedBox(width: 10.0),
AutoSizeText(
"No Current Messages or Alerts",
maxLines: 1,
maxFontSize: 20,
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
//if (snapshot.hasData) SizedBox(width: 45),
],
),
);
}
Alert alert = Alert.fromFirestore(snapshot.data.docs.first);
return Card(
//height: 60.0,
child: InkWell(
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(width: 10.0),
getIcon(snapshot),
SizedBox(width: 15.0),
Text(
"New Alert for Your Patient",
style:
TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
//if (snapshot.hasData) SizedBox(width: 45),
if (snapshot.hasData) Icon(Icons.arrow_right, size: 50)
],
),
onTap: () {}),
elevation: 12,
);
});
}
解决方案
您需要包装AutoSizeText
小部件Expanded
以限制文本的大小,AutoSizeText
然后可以调整它的大小。
以下是一些解释:AutoSizeText
因为 Row 和 Container、Column 或 ListView 等其他小部件不约束它们的子级,所以文本会溢出。您可以通过约束 AutoSizeText 来解决此问题。在 Row 和 Column 的情况下使用 Expanded 包裹它,或者使用 SizedBox 或其他具有固定宽度(和高度)的小部件。
AutoSizeText(
"No Current Messages or Alerts",
maxLines: 1,
maxFontSize: 20,
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
应更改为:
Expanded(
child:
AutoSizeText(
"No Current Messages or Alerts",
maxLines: 1,
maxFontSize: 20,
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
),
推荐阅读
- javascript - Webpack - 当导入未定义/未声明时发出警告
- python-3.x - Flask-SocketIO 发射不能从不同的模块工作?
- python - Pandas 将时间序列数据重新采样为 15 分钟和 45 分钟 - 使用多索引或列
- xml - xmlstarlet Fritzbox FB 电话簿到 CSV
- php - 如何通过存储在其中的值搜索 Google 表格
- python - Pandas——将值从一列映射到另一列
- php - 尝试从应用文件夹而不是存储中检索 Laravel 文件夹中的文件
- java - 在 log4j 2 中使用 throwable 和参数
- unity3d - 我只能通过在 Unity 中使用着色器来获得游戏对象周围的发光效果吗?
- react-native - React Native 白色黑屏问题