我的代码中有一个问题,ElevatedButton()onPressed中的函数addCu():当父Column()是AlertDialog()的子级时,属性不向ListVew.Builder()添加项
代码如下:
import 'package:flutter/material.dart';
import '../constants.dart';
import '../models/currencies.dart';
class AddCurrency extends StatefulWidget {
final Function addCu;
const AddCurrency({
super.key,
required this.addCu,
});
@override
State<AddCurrency> createState() => _AddCurrencyState();
}
class _AddCurrencyState extends State<AddCurrency> {
@override
Widget build(BuildContext context) {
void addNewCurrency(String cuName, double cuPrice, double cuPercentage) {
final newCu = Currencies(
currencyName: cuName,
currencyPrice: cuPrice,
currencyPercent: cuPercentage,
);
setState(() {
currenciesList.add(newCu);
});
}
return FloatingActionButton(
backgroundColor: Colors.pink,
child: const Icon(Icons.notification_add),
onPressed: () => showDialog(context: context, builder: (context) => AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextFormFieldSource(
data: 'Add Currency Name',
controller: currencyNameController,
),
SizedBox(
height: kDefaultPadding,
),
TextFormFieldSource(
data: 'Add Currency Price',
controller: currencyPriceController,
),
SizedBox(
height: kDefaultPadding,
),
TextFormFieldSource(
data: 'Add Currency Percentage',
controller: currencyPercentController,
),
SizedBox(
height: kDefaultPadding,
),
ElevatedButton(
onPressed: () {
widget.addCu(
currencyNameController.text,
double.parse(currencyPriceController.text),
double.parse(currencyPercentController.text),
);
addNewCurrency;
Navigator.of(context).pop();
},
child: const Text('Add currency now!'),
),
],
),
),),
);
}
}
请注意,该函数在删除FloatingActionButton()、showDialog()和AlertDialog()时工作并添加项。
在下面的代码中,为什么我们需要在ListVew.Builder()中使用一个空列表“listOfCurrencies”,而不是我们想要向其中添加项目的列表“currenciesList”,后者位于包含模型的dart文件中,并且其中至少有一个对象示例?
import 'package:flutter/material.dart';
import '../constants.dart';
import '../models/currencies.dart';
class CurrencyList extends StatelessWidget {
final List<Currencies> listOfCurrencies;
const CurrencyList({Key? key, required this.listOfCurrencies}) : super(key: key);
@override
Widget build(BuildContext context) {
return SizedBox(
height: 300,
child: ListView.builder(
shrinkWrap: true,
itemCount: listOfCurrencies.length,
itemBuilder: (context , index) => Card(
color: Colors.white,
child: Row(
children: [
Container(
margin: const EdgeInsets.all(15),
height: 50,
width: 50,
decoration: const BoxDecoration(
color: Colors.purple,
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
offset: Offset(0, 5),
blurRadius: 15,
),
]),
child: Center(
child: Text(
listOfCurrencies[index].currencyName!.substring(0, 1).toUpperCase(),
style: const TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
),
SizedBox(
width: kDefaultPadding,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
listOfCurrencies[index].currencyName!,
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
SizedBox(
width: kDefaultPadding,
),
Text(
'\$${listOfCurrencies[index].currencyPrice!}',
),
SizedBox(
width: kDefaultPadding,
),
Text(
'1 hour: ${listOfCurrencies[index].currencyPercent!}%',
style: const TextStyle(
color: Colors.red,
),
),
],
)
],
),
),
),
);
}
}
当需要Maven的时候,他们去了哪里?!!!!
1条答案
按热度按时间ozxc1zmp1#
在您的代码中,您实际上并没有调用“addNewCurrency”函数。您需要像常规函数一样使用“addNewCurrency()”而不是“addNewCurrency”来调用它。