當我嘗試在 listveiw 中使用時,我創建了 bool 條件,但出現了這個錯誤(預期 1 個位置引數,但找到了 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,
),
],
),
);
}
}
uj5u.com熱心網友回復:
“isBool”實體屬于“SingleItem”類。在您的“ReviewCart”類中,您在創建“SingleItem”類的實體時嘗試傳遞該類中不可用的“isBool”(本身)。
嘗試傳遞一個實際的布林值(真/假)而不是“isBool”。
uj5u.com熱心網友回復:
我認為你正在合并你的類和小部件類,所以我最好的猜測是你應該為你的建構式添加一個 bool 引數;
body: ListView(
children: [
const SizedBox(
height: 10,
),
SigleItem(isBool),
const SizedBox(
height: 10,
),
],
),
如果我猜錯了,請在問題中添加更多資訊
uj5u.com熱心網友回復:
問題是該變數isBool在構建方法的背景關系中未定義ReviewCart- 它只能作為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(),
),
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/477915.html
