flutter 我使布尔条件时,我试图使用在listveiw我得到了这个错误

dtcbnfnu  于 2023-06-24  发布在  Flutter
关注(0)|答案(3)|浏览(92)

当我尝试在listveiw中使用时,我做了bool条件,我得到了这个错误(1个位置参数(s)预期,但发现0。尝试添加丢失的参数。),当传递isBool值时,它会给我这个错误(未定义的名称'isBool'。尝试将名称更正为已定义的名称,或定义名称。)

import 'package:chad_cafe/configs/color.dart';
import 'package:flutter/material.dart';

class SigleItem extends StatelessWidget {
  //const SigleItem({Key? key}) : super(key: key);

  bool isBool = false;
  SigleItem(this.isBool);
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 10),
      child: Row(
        children: [
          Expanded(
              child: Container(
            child: Center(
              child: Image.asset('assets/bbqpizza.png'),
            ),
            height: 100,
          )),
          Expanded(
              child: Container(
            height: 100,
            child: Column(
              mainAxisAlignment: isBool == false
                  ? MainAxisAlignment.spaceAround
                  : MainAxisAlignment.spaceEvenly,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Column(
                  children: [
                    Text(
                      'ProductName',
                      style: TextStyle(
                          color: textColor, fontWeight: FontWeight.bold),
                    ),
                    const Text(
                      'RS-799',
                      style: TextStyle(
                        color: Colors.grey,
                      ),
                    )
                  ],
                ),
                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,
                              ),
                            ),
                          ],
                        ),
                      )
                    : const Text('750')
              ],
            ),
          )),
          Expanded(
            child: Container(
                height: 100,
                padding: isBool == false
                    ? EdgeInsets.symmetric(horizontal: 15, vertical: 32)
                    : EdgeInsets.only(left: 15, right: 15),
                child: isBool == false
                    ? Container(
                        decoration: BoxDecoration(
                          border: Border.all(color: Colors.grey),
                          borderRadius: BorderRadius.circular(30),
                        ),
                        child: Center(
                          child: Row(
                            children: [
                              Icon(
                                Icons.add,
                                color: primaryColor,
                                size: 20,
                              ),
                              Text(
                                'Add',
                                style: TextStyle(
                                  color: primaryColor,
                                ),
                              )
                            ],
                          ),
                        ),
                      )
                    : Column(
                        children: [
                          const Icon(
                            Icons.delete,
                            size: 30,
                            color: Colors.black,
                          ),
                          const SizedBox(height: 5),
                          Container(
                            decoration: BoxDecoration(
                              border: Border.all(color: Colors.grey),
                              borderRadius: BorderRadius.circular(30),
                            ),
                            child: Center(
                              child: Row(
                                children: [
                                  Icon(
                                    Icons.add,
                                    color: primaryColor,
                                    size: 20,
                                  ),
                                  Text(
                                    'Add',
                                    style: TextStyle(
                                      color: primaryColor,
                                    ),
                                  )
                                ],
                              ),
                            ),
                          )
                        ],
                      )),
          ),
          isBool == false
              ? Container()
              :const Divider(
                  height: 1,
                  color: Colors.black,
                ),
        ],
      ),
    );
    isBool == false
        ? Container()
        : Divider(
            height: 1,
            color: Colors.black,
          );
  }
}



import 'package:chad_cafe/Widgets/single_item.dart';
import 'package:chad_cafe/configs/color.dart';
import 'package:flutter/material.dart';

class ReviewCart extends StatelessWidget {
  const ReviewCart({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: ListTile(
        title: const Text("Total Amount"),
        trailing: Container(
          width: 160,
          child: MaterialButton(
            onPressed: () {},
            child: const Text('Submit '),
            color: primaryColor,
            shape:
                RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
          ),
        ),
      ),
      appBar: AppBar(
        backgroundColor: primaryColor,
        title: const Text(
          'Review Cart',
          style: TextStyle(
            fontSize: 18,
          ),
        ),
      ),
      body: ListView(
        children: [
          const SizedBox(
            height: 10,
          ),
          SigleItem(isBool),//Undefined name 'isBool'.
Try correcting the name to one that is defined, or defining the name.
          const SizedBox(
            height: 10,
          ),
        ],
      ),
    );
  }
}
nue99wik

nue99wik1#

“isBool”示例属于“SingleItem”类。在你的'ReviewCart'类中,你试图传递'isBool'(本身),这在这个类中是不可用的,同时创建'SingleItem'类的示例。
尝试传递一个实际的bool值(true/false)而不是'isBool'示例。

bq3bfh9z

bq3bfh9z2#

我认为你正在合并类和widget类,所以我最好的猜测是你应该在构造函数中添加一个bool参数;

body: ListView(
        children: [
          const SizedBox(
            height: 10,
          ),
          SigleItem(isBool),
          const SizedBox(
            height: 10,
          ),
        ],
      ),

如果我猜错了,请在问题上补充更多信息

pes8fvy9

pes8fvy93#

问题是变量isBoolReviewCart的构建方法的上下文中未定义-它只能作为SingleItem示例的成员使用。
当你构造一个SingleItem的新示例时,你需要传递一个值,它可以是一个定义的变量,也可以是一个布尔文本:

class ReviewCart extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // omitted
      body: ListView(
        children: [
          SingleItem(false); // or true, or some variable which is defined in this context.
        ],
      ),
    );
  }
}

从名称ReviewCart判断,在某个时候,你可能会从购物车中的产品列表中构建列表-下面是一个非常简单的例子,说明它的外观:

class ReviewCart extends StatelessWidget {
  List<Product> products;

  ReviewCart(this.products);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // omitted
      body: ListView(
        children: products.map((product) => 
          SingleItem(product.someBooleanValue)
        ).toList(),
      ),
    );
  }
}

相关问题