我正在为搜索和评论购物车页面设置布尔条件

I'm making a bool condition for search and review cart page

我正在为搜索和查看购物车页面设置一个 bool 条件,然后我遇到了这 3 个错误,我试图解决但没有成功(容器上的前 2 个错误//预期找到','。//元素类型 'bool' 无法分配给列表类型 'Widget'。)和文本小部件上的第二个(预期会找到“]”。)

bool isBool = false;
  SigleItem(this.isBool);



 isBool ==false  Container( //Expected to find ','. //The element type 'bool' can't be assigned to the list type 'Widget'.
                  margin: const EdgeInsets.only(right: 15),
                  padding: const EdgeInsets.symmetric(horizontal: 10),
                  height: 35,
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.grey),
                    borderRadius: BorderRadius.circular(30),
                  ),
                  child: Row(
                    children: [
                      const Expanded(
                        child: Text(
                          'RS-799',
                          style: TextStyle(
                            color: Colors.grey,
                          ),
                        ),
                      ),
                      Center(
                        child: Icon(
                          Icons.arrow_drop_down,
                          size: 20,
                          color: primaryColor,
                        ),
                      ),
                    ],
                  ),
                ):Text('750')//Expected to find ']'.

您忘记使用问号了。你需要这样使用。

isBool == false ? Container(
                  margin: const EdgeInsets.only(right: 15),
                  padding: const EdgeInsets.symmetric(horizontal: 10),
                  height: 35,
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.grey),
                    borderRadius: BorderRadius.circular(30),
                  ),
                  child: Row(
                    children: [
                      const Expanded(
                        child: Text(
                          'RS-799',
                          style: TextStyle(
                            color: Colors.grey,
                          ),
                        ),
                      ),
                      Center(
                        child: Icon(
                          Icons.arrow_drop_down,
                          size: 20,
                          color: primaryColor,
                        ),
                      ),
                    ],
                  ),
                ):Text('750'),

你忘记了 bool 条件后的问号,阅读 this for Conditional Statement

isBool == false
        ? Container(
            margin: const EdgeInsets.only(right: 15),
            padding: const EdgeInsets.symmetric(horizontal: 10),
            height: 35,
            decoration: BoxDecoration(
              border: Border.all(color: Colors.grey),
              borderRadius: BorderRadius.circular(30),
            ),
            child: Row(
              children: [
                const Expanded(
                  child: Text(
                    'RS-799',
                    style: TextStyle(
                      color: Colors.grey,
                    ),
                  ),
                ),
                Center(
                  child: Icon(
                    Icons.arrow_drop_down,
                    size: 20,
                    color: Colors.red,
                  ),
                ),
              ],
            ),
          )
        : Text('750'),

结果屏幕->

你可以像这样直接使用三元运算符

isBool ? text("if true then show this") : text("if false then show this)

boool_var ? true_widget : false_widget