Home > Net >  i make bool condition when i try to use in listveiw i got this error
i make bool condition when i try to use in listveiw i got this error

Time:05-19

i make bool condition when i try to use in listveiw i got this error(1 positional argument(s) expected, but 0 found. Try adding the missing arguments.) and when is pass isBool value it gives me this error (Undefined name 'isBool'. Try correcting the name to one that is defined, or defining the name.)

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,
          ),
        ],
      ),
    );
  }
}

CodePudding user response:

I think you'e merging your class and widget class, so my best guess is that you should bass a bool parameter to your constructor;

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

If I assumed wrong, please add more information to the question

CodePudding user response:

The problem is that the variable isBool is undefined in the context of the build method for ReviewCart - It is only available as a member of an instance of SingleItem.

When you construct a new instance of SingleItem, you will need to pass it a value which is either a defined variable or a boolean literal:

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.
        ],
      ),
    );
  }
}

Judging from the name ReviewCart, at some point you will probably be building the list from a list of products in the cart - Here's a very simple example of how that might look:

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(),
      ),
    );
  }
}
  • Related