Bud*_*oni 1 currency number-formatting flutter
我正在遵循一个教程,尝试创建一个水平SingleChildScrollView,其中包含带有产品描述的对象和带有两位小数的货币+价格标签。实现NumberFormat.currency方法返回错误:NoSuchMethodError 类“String”没有实例 getter“isNegative”
有人可以帮助理解为什么它没有拉动“价格”价值吗?如果我对其进行硬编码并为最终的双倍价格分配一个值=即:29.90;在AngeboteCard()类中它可以,但在Angebote()类中则不是。
这是我的课:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hotz/constants.dart';
import 'package:intl/intl.dart';
import 'package:intl/number_symbols.dart';
class Angebote extends StatelessWidget {
const Angebote({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: <Widget>[
AngeboteCard(
image: "assets/images/pork_belly.png",
product: "Schweinsbrust frisch",
price: 29.90,
press: () {},
),
],
),
);
}
}
class AngeboteCard extends StatelessWidget {
const AngeboteCard({
Key key,
this.image,
this.title,
this.product,
this.price,
this.press,
}) : super(key: key);
final String image, title, product;
final double price;
final Function press;
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return GestureDetector(
onTap: press,
child: Container(
margin: EdgeInsets.only(
left: kDefaultPadding,
top: kDefaultPadding / 2,
bottom: kDefaultPadding / 0.5,
right: kDefaultPadding,
),
width: size.width * 0.8,
height: 300,
child: Column(
children: <Widget>[
Image.asset(image),
GestureDetector(
onTap: press,
child: Container(
padding: EdgeInsets.all(kDefaultPadding / 2),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(10),
bottomRight: Radius.circular(10),
),
boxShadow: [
BoxShadow(
offset: Offset(0, 10),
blurRadius: 50,
color: kPrimaryColour.withOpacity(0.23),
),
],
),
child: Row(
children: <Widget>[
RichText(
text: TextSpan(
children: [
TextSpan(
text: "$title\n".toUpperCase(),
style: Theme.of(context).textTheme.button),
TextSpan(
text: "$product\n".toUpperCase(),
style: TextStyle(
color: kPrimaryColour.withOpacity(0.5)),
),
],
),
),
Spacer(),
Text(
NumberFormat.currency(locale: 'de_CH', decimalDigits: 2).format('$price'),
// 'CHF ''$price',
style: Theme.of(context)
.textTheme
.button
.copyWith(color: kPrimaryColour),
)
],
),
),
),
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
======== 小部件库捕获异常======================================== ================== 构建AngeboteCard时抛出以下NoSuchMethodError(脏,依赖项:[MediaQuery,_LocalizationsScope-[GlobalKey#ff95d],_InheritedTheme]):类“String”有没有实例 getter 'isNegative'。接收者:“null”尝试调用:isNegative
相关的导致错误的小部件是:AngeboteCard file:///Users/nebojsa.kuzmanovic/Development/FlutterProjects/hotz/lib/screens/home/components/angebote.dart:29:11 当抛出异常时,这是stack: #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5) #1 NumberFormat._signPrefix (package:intl/src/intl/number_format.dart:786:30) #2 NumberFormat.format (包:intl/src/intl/number_format.dart:430:10) #3 AngeboteCard.build (包:hotz/screens/home/components/angebote.dart:107:80) #4 StatelessElement.build (包:flutter/ src/widgets/framework.dart:4569:28) ...
因此,您不能在 .format 函数中使用字符串,您需要使用数字(双精度或整数),我在代码中尝试了这种方式,并正常使用 2 位数字:
Text(
NumberFormat.currency(locale: 'de_CH').format(price),
style: Theme.of(context)
.textTheme
.button
.copyWith(color: kPrimaryColour),
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18890 次 |
| 最近记录: |