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

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

Flutter - Strange Widget Alignment behavior

问题

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

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

这是小部件:

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:

 Widget buildFoodItem() {
    return SizedBox(
      width: 130,
      child: Stack(
        children: &lt;Widget&gt;[
          Padding(
            padding:
                const EdgeInsets.only(top: 32, left: 8, right: 8, bottom: 15),
            child: Container(
              decoration: BoxDecoration(
                boxShadow: &lt;BoxShadow&gt;[
                  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: &lt;Widget&gt;[
                    Text(
                      &quot;${widget.mahlzeit}&quot;,
                      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: &lt;Widget&gt;[
                              Text(
                                &quot;${totalMJ.toStringAsFixed(1)}&quot;,
                                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(
                                  &#39;MJ&#39;,
                                  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(
                  &#39;${item.fFuttermittel!.name!}&#39;,
                  style: TextStyle(
                    fontWeight: FontWeight.w500,
                    fontSize: 10,
                    color: Colors.white,
                  ),
                )
              : Container();
        },
      ),
    );
  }

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

 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:

确定