![](/img/trans.png)
[英]What is the difference between the codes for importing and calling Class from another dart.file (Flutter)
[英]I wanna call a value from function based widget from another class in the same dart.file into a Text widget
我有一個名為 VarianKemasan 的 class 有一個名為 totalBelanjaKemasan 的 function
class VarianKemasan extends StatefulWidget {
final productDetail;
const VarianKemasan({Key? key, this.productDetail}) : super(key: key);
@override
_VarianKemasanState createState() => _VarianKemasanState();
}
class _VarianKemasanState extends State<VarianKemasan> {
totalBelanjaKemasan() {
Text(
"${GlobalFunctions().rupiah(widget.productDetail['price'] * _parseCounter())}");
}
並想將 function 具有的值調用到另一個 class 中的文本小部件中
class ProductDetailPage extends StatefulWidget {
final productDetail;
const ProductDetailPage({Key? key, this.productDetail}) : super(key: key);
@override
_ProductDetailPageState createState() => _ProductDetailPageState();
}
class _ProductDetailPageState extends State<ProductDetailPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFFFFFFFF),
body: Container(
child: Text(
"called value here",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w500,
color: Color(0xff31708F)),
)
我怎樣才能做到這一點?
試試下面的代碼希望它對你有幫助。 參考我的答案here和here也用於使用的widget.productDetail
您的 VarianKemasan Class:
class VarianKemasan extends StatefulWidget {
final product = 'Product Data';
@override
_VarianKemasanState createState() => _VarianKemasanState();
}
class _VarianKemasanState extends State<VarianKemasan> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("App Bar"),
),
body: Center(
child: Container(
child: TextButton(
onPressed: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ProductDetailPage(
productDetail: widget.product,
),
),
),
child: Text(
'Ok',
),
),
),
),
);
}
}
您的產品詳情頁ProductDetailPage
class ProductDetailPage extends StatefulWidget {
final productDetail;
ProductDetailPage({
Key? key,
required this.productDetail,
}) : super(
key: key,
);
@override
_ProductDetailPageState createState() => _ProductDetailPageState();
}
class _ProductDetailPageState extends State<ProductDetailPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("App Bar"),
),
body: Center(
child: Container(
child: Text(
widget.productDetail,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w500,
color: Color(0xff31708F)),
),
),
),
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.