簡體   English   中英

Flutter中如何將函數中的String類型參數傳遞給IconData

[英]How to pass String type parameter in function to IconData in Flutter

我已經使用這個顫振項目 2 周了,我在如何將函數中的 String 類型參數傳遞給 IconData 類型時遇到了問題。 在此之前,我有這樣的 JSON 響應:

{
  "status": "success",
  "data": {
    "general": [
      {
        "id": 1,
        "name": "Sumbangan Pembinaan Pendidikan",
        "icon": "credit_card",
        "type": "monthly",
        "amount": 125000
      },
      {
        "id": 2,
        "name": "Uang Bangunan",
        "icon": "credit_card",
        "type": "yearly",
        "amount": 1250000
      }
    ],

我想傳遞“圖標”鍵,我的按鈕功能如下所示:

Widget studentFeeButtonMenu(BuildContext context, String text, IconData iconFee){
    return Container(
      width: double.infinity,
      height: screenHeight(context)*(1/12),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
      ),
      child: Center(
        child: Container(
          width: screenWidth(context)*(1/1.3),
          height: double.infinity,
          color: Colors.red,
          child: Row(
            children: [
              Icon(
                iconFee,
                color: Color(0xff84923f),
              ),
              SizedBox(
                width: screenWidth(context)*(1/10),
              ),
              Text(
                text,
                style: TextStyle(
                  color: Colors.black,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

我想創建 ListView.builder 並且 itemBuilder 正在使用該函數,如下所示:

child: ListView.builder(
            itemCount: generalStudentFees == null ? 0 : generalStudentFees.length,
            itemBuilder: (context, index){
              return studentFeeButtonMenu(context, generalStudentFees[index]['name'], Icons.generalStudentFees[index]['icon']);
            },
          ),

'name' 鍵工作正常,可以獲取數據,但我不知道如何傳遞圖標,錯誤消息說:

getter 'generalStudentFees' 沒有為類型 'Icons' 定義。

Flutter 的 Icons 類從 MaterialFonts 中獲取圖標,如下所示:

IconData icon = IconData(0xea00, fontFamily: 'MaterialIcons')

該十六進制數字是字體中字形的 Unicode 代碼點。

您可以使用 Map<String,Int> 將您的 json 圖標 String 值映射到您希望它表示的代碼點 int。 例如:

Map<String, Int> iconCodepoint = {
  'credit_card': 0xe06c,
  'gift_card'  : 0xe020,
  //etc.
}

//String iconName holds your icon String value ('credit_card')
studentFeeButtonMenu(context, 'Hello World', IconData(iconCodepoint[iconName], fontFamily: 'MaterialIcons');

此處的 .codepoint 文件包含字形代碼點編號: Github 上的 Material-Design-icons

https://api.flutter.dev/flutter/material/Icons-class.html

在上面鏈接的文檔中,我們可以使用 codePoint 字段。

IconData(60969, fontFamily: 'MaterialIcons')

60969 是名為“ac unit outlines”的材質圖標的代碼點。

iconDataCodePoint: Icons.ac_unit_outlined.codePoint

用法示例:

Icon(
                            events.iconDataCodePoint != null
                                ? IconData((events.iconDataCodePoint as int),
                                    fontFamily: 'MaterialIcons')
                                : Icons.announcement_rounded,
                            size: 30,
                          ),

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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