How to make a Flutter container conditional?

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

How to make a Flutter container conditional?

问题

我有一个字符串变量'sdesc1'。现在这个变量可能有值,也可能为空。我正在使用一个容器来显示上述变量。

在应用程序屏幕上,当变量'sdesc1'为空时,上述代码会生成一个占用一定高度的空白行,这是不希望的。

我尝试将这段代码移到一个小部件方法中并返回容器,但它显示了一个错误,说返回类型可能为空之类的。

当变量为空时,我该如何使容器有条件地显示?请帮忙提供一些代码。提前谢谢。

英文:

I have String variable 'sdesc1'. Now this variable could either have a value or it could be empty. I am using a container to display the above variable.

....
,
Container(
  width: double.infinity,
  color: Colors.transparent,
  child: Padding(
	padding: const EdgeInsets.all(8.0),
	child: Text(sdesc1,
		style:
			TextStyle(fontSize: 10, fontStyle: FontStyle.italic)),
  ),
),
....

In the app screen, the above code generates a blank row occupying some height when the variable 'sdesc1' is empty which is not desirable.

I tried to move the piece of code in a widget method and return the container, but it showed me an error saying that return type could be null some thing like that.

How can I make a container conditional when the variables are empty. Please help with some codes. Thanks in advance.

答案1

得分: 1

这个在build方法中的条件可以防止创建一个容器(我猜测sdesc1是一个字符串):

if(sdesc1.isNotEmpty)
    Container(
      width: double.infinity,
      color: Colors.transparent,
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Text(sdesc1,
            style:
                TextStyle(fontSize: 10, fontStyle: FontStyle.italic)),
      ),
    )
英文:

This condition in the build method can prevents from creating a container (I guessed sdesc1 was a string):

if(sdesc1.isNotEmpty)
    Container(
      width: double.infinity,
      color: Colors.transparent,
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Text(sdesc1,
            style:
                TextStyle(fontSize: 10, fontStyle: FontStyle.italic)),
      ),
    ),

答案2

得分: 1

除了VincentDR的答案之外,有时候你可能会遇到无法像他在答案中展示的那样使用if()语句的情况。在这种情况下,你可以这样使用if语句:

sdesc1.isNotEmpty ? Container() : const sizedbox.Shrink

这里是另一个例子,你可以在其中使用这种方式:

bool highlightContainer = false;
Container(
  color: highlightContainer ? Colors.red : Colors.transparent
),

另外,如果返回的文本可以为空,还可以使用??来替换文本。

所以,如果你希望即使没有文本也保留容器,你可以这样做:

child: Text(sdesc1 ?? '')
英文:

In addition to VincentDR's answer, you may sometimes find situations where you cannot use if() statements as he showed in his answer. In this case you can use if statement this way:

sdesc1.isNotEmpty ? Container() : const sizedbox.Shrink

here is another example in where you would use that:

bool highlightContainer = false;
Container(
  color: highlightContainer ? Colors.red : Colors.transparent
),

Also, another way to replace the text if its return is nullable is to use ??

so if you prefer to keep the container there even if there is no text, you can do this:

child: Text(sdesc1 ?? '')

huangapple
  • 本文由 发表于 2023年8月8日 20:04:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859386.html
匿名

发表评论

匿名网友

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

确定