Flutter – 异常的小部件对齐行为

huangapple go评论82阅读模式
英文:

Flutter - Strange Widget Alignment behavior

问题

这里是您要翻译的代码部分:

  1. 我有一个小部件,显示其中的一组餐点。在一个屏幕上,它对齐得很好,而在另一个屏幕上,文本对齐到底部,并且有一个无法解释的项目之上留有空白。为演示目的,我添加了一个红色容器:
  2. 这是小部件:

Widget buildFoodItem() {
return SizedBox(
width: 130,
child: Stack(
children: <Widget>[
Padding(
padding:
const EdgeInsets.only(top: 32, left: 8, right: 8, bottom: 15),
child: Container(
decoration: BoxDecoration(
boxShadow: <BoxShadow>[
BoxShadow(
color: fumigruen,
offset: const Offset(1.1, 4.0),
blurRadius: 8.0),
],
gradient: LinearGradient(
colors: [
fumigruen,
fumigruen_accent,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
borderRadius: const BorderRadius only(
bottomRight: Radius.circular(8.0),
bottomLeft: Radius.circular(8.0),
topLeft: Radius.circular(8.0),
topRight: Radius.circular(54.0),
),
),
child: Padding(
padding: const EdgeInsets.only(
top: 30, left: 10, right: 10, bottom: 0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"${widget.mahlzeit}",
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
letterSpacing: 0.2,
color: Colors.white,
),
),
Expanded(
child: Container(
color: Colors.red,
child: Padding(
padding: const EdgeInsets.only(top: 2, bottom: 8),
child: buildItemList(),
),
),
),
(totalMJ != 0)
? Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Text(
"${totalMJ.toStringAsFixed(1)}",
textAlign: TextAlign center,
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 24,
letterSpacing: 0.2,
color: Colors white,
),
),
Padding(
padding:
const EdgeInsets.only(left: 4, bottom: 3),
child: Text(
'MJ',
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 14,
letterSpacing: 0.2,
color: Colors white,
),
),
),
],
)
: Container(),
Positioned(some icons),
Positioned(some icons),
],
),
),
),
),
],
),
);
}

Widget buildItemList() {
return SingleChildScrollView(
child: ListView.builder(
shrinkWrap: true,
itemCount: widget.futterplan.lRationOfFutterplan!.length,
itemBuilder: (context, index) {
final item = widget.futterplan.lRationOfFutterplan![index];
return (item.mahlzeit == widget.mahlzeit)
? Text(
'${item.fFuttermittel!.name!}',
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 10,
color: Colors white,
),
)
: Container();
},
),
);
}

这里是您提供的小部件的代码部分的翻译。

英文:

I have a widget I build, showing a list of meals in it. On one screen it aligns perfectly, on another screen the text is aligned to the bottom and there is a space over the items I cant explain. I added a red container for demonstration purposes:

Here is the widget:

  1. Widget buildFoodItem() {
  2. return SizedBox(
  3. width: 130,
  4. child: Stack(
  5. children: &lt;Widget&gt;[
  6. Padding(
  7. padding:
  8. const EdgeInsets.only(top: 32, left: 8, right: 8, bottom: 15),
  9. child: Container(
  10. decoration: BoxDecoration(
  11. boxShadow: &lt;BoxShadow&gt;[
  12. BoxShadow(
  13. color: fumigruen,
  14. offset: const Offset(1.1, 4.0),
  15. blurRadius: 8.0),
  16. ],
  17. gradient: LinearGradient(
  18. colors: [
  19. fumigruen,
  20. fumigruen_accent,
  21. ],
  22. begin: Alignment.topLeft,
  23. end: Alignment.bottomRight,
  24. ),
  25. borderRadius: const BorderRadius.only(
  26. bottomRight: Radius.circular(8.0),
  27. bottomLeft: Radius.circular(8.0),
  28. topLeft: Radius.circular(8.0),
  29. topRight: Radius.circular(54.0),
  30. ),
  31. ),
  32. child: Padding(
  33. padding: const EdgeInsets.only(
  34. top: 30, left: 10, right: 10, bottom: 0),
  35. child: Column(
  36. mainAxisAlignment: MainAxisAlignment.start,
  37. mainAxisSize: MainAxisSize.min,
  38. crossAxisAlignment: CrossAxisAlignment.start,
  39. children: &lt;Widget&gt;[
  40. Text(
  41. &quot;${widget.mahlzeit}&quot;,
  42. textAlign: TextAlign.center,
  43. style: TextStyle(
  44. fontWeight: FontWeight.bold,
  45. fontSize: 16,
  46. letterSpacing: 0.2,
  47. color: Colors.white,
  48. ),
  49. ),
  50. Expanded(
  51. child: Container(
  52. color: Colors.red,
  53. child: Padding(
  54. padding: const EdgeInsets.only(top: 2, bottom: 8),
  55. child: buildItemList(),
  56. ),
  57. ),
  58. ),
  59. (totalMJ != 0)
  60. ? Row(
  61. mainAxisAlignment: MainAxisAlignment.end,
  62. crossAxisAlignment: CrossAxisAlignment.end,
  63. children: &lt;Widget&gt;[
  64. Text(
  65. &quot;${totalMJ.toStringAsFixed(1)}&quot;,
  66. textAlign: TextAlign.center,
  67. style: TextStyle(
  68. fontWeight: FontWeight.w500,
  69. fontSize: 24,
  70. letterSpacing: 0.2,
  71. color: Colors.white,
  72. ),
  73. ),
  74. Padding(
  75. padding:
  76. const EdgeInsets.only(left: 4, bottom: 3),
  77. child: Text(
  78. &#39;MJ&#39;,
  79. style: TextStyle(
  80. fontWeight: FontWeight.w500,
  81. fontSize: 14,
  82. letterSpacing: 0.2,
  83. color: Colors.white,
  84. ),
  85. ),
  86. ),
  87. ],
  88. )
  89. : Container(),
  90. Positioned(some icons),
  91. Positioned(some icons),
  92. ],
  93. ),
  94. ),
  95. ),
  96. ),
  97. ],
  98. ),
  99. );
  100. }
  101. Widget buildItemList() {
  102. return SingleChildScrollView(
  103. child: ListView.builder(
  104. shrinkWrap: true,
  105. itemCount: widget.futterplan.lRationOfFutterplan!.length,
  106. itemBuilder: (context, index) {
  107. final item = widget.futterplan.lRationOfFutterplan![index];
  108. return (item.mahlzeit == widget.mahlzeit)
  109. ? Text(
  110. &#39;${item.fFuttermittel!.name!}&#39;,
  111. style: TextStyle(
  112. fontWeight: FontWeight.w500,
  113. fontSize: 10,
  114. color: Colors.white,
  115. ),
  116. )
  117. : Container();
  118. },
  119. ),
  120. );
  121. }

and here are the pictures of the widget in action

the correct working widget

Flutter – 异常的小部件对齐行为

same widget but not working properly

Flutter – 异常的小部件对齐行为

many thanks in advance for helping me out

答案1

得分: 0

在此链接中找到了答案:https://stackoverflow.com/questions/63639472/flutter-unexpected-space-at-the-top-of-listview

看起来有一个可以通过插入以下内容来避免的填充:

padding: EdgeInsets.zero,

唯一奇怪的是,我只需要在一个特定屏幕上使用这行代码,而不需要在另一个我也在使用该小部件的屏幕上使用它。

英文:

Found an answer here: https://stackoverflow.com/questions/63639472/flutter-unexpected-space-at-the-top-of-listview

It seems that there was a padding that could be avoidded by inserting

  1. padding: EdgeInsets.zero,

The only strange thing was, that i needed the code line for onw specific screen and not on the other screen where i was also using that widget.

huangapple
  • 本文由 发表于 2023年2月24日 04:53:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定