簡體   English   中英

我想從同一個 dart.file 中的另一個 class 中的基於 function 的小部件中調用一個值到文本小部件中

[英]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)),
                    )

我怎樣才能做到這一點?

試試下面的代碼希望它對你有幫助。 參考我的答案herehere也用於使用的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM