getSavedAPIKeysWidget function

Widget getSavedAPIKeysWidget(
  1. BuildContext context,
  2. GlobalKey<FormState> formKey
)

Implementation

Widget getSavedAPIKeysWidget(
  BuildContext context,
  GlobalKey<FormState> formKey,
) {
  return Consumer2<FontsProvider, ColorProvider>(
    builder: (BuildContext context, FontsProvider fontProv,
        ColorProvider colorProv, Widget? child) {
      return FutureBuilder<List<ApiKeyModel>>(
        future: APIKeySharedPref.getApiKeys(),
        builder:
            (BuildContext context, AsyncSnapshot<List<ApiKeyModel>> snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Center(child: CircularProgressIndicator());
          } else if (snapshot.hasError) {
            return Center(
                child: Text(
              // 'Error: ${snapshot.error}',
              AppLocalizations.of(context)!
                  .settings_errorSnapshot(snapshot.error.toString()),
              style: TextStyle(
                fontSize: fontProv.fonts.textSize,
                color: Colors.black,
                fontFamily: fontType,
              ),
            ));
          } else if (!snapshot.hasData || snapshot.data!.isEmpty) {
            return Center(
                child: Text(
              // 'No API Keys Found',
              AppLocalizations.of(context)!.settings_noApiKeyFound,
              style: TextStyle(
                fontSize: fontProv.fonts.textSize,
                color: Colors.black,
                fontFamily: fontType,
              ),
            ));
          } else {
            List<ApiKeyModel> apiKeys = snapshot.data!;

            Map<String, List<ApiKeyModel>> groupedApiKeys = {};
            for (var apiKey in apiKeys) {
              if (groupedApiKeys.containsKey(apiKey.serviceType)) {
                groupedApiKeys[apiKey.serviceType]!.add(apiKey);
              } else {
                groupedApiKeys[apiKey.serviceType] = [apiKey];
              }
            }

            return SingleChildScrollView(
              child: Column(
                children: groupedApiKeys.entries.map((service) {
                  String serviceType = service.key;
                  List<ApiKeyModel> serviceApiKeys = service.value;

                  return Container(
                    margin: EdgeInsets.symmetric(vertical: 8.0),
                    padding: EdgeInsets.all(8.0),
                    decoration: BoxDecoration(
                      color: colorProv.colors.shadow,
                      borderRadius: BorderRadius.circular(10),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          serviceType,
                          style: TextStyle(
                            fontSize: fontProv.fonts.textSize + 2,
                            color: Colors.black,
                            fontFamily: fontType,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        SizedBox(
                          height: MediaQuery.of(context).size.height * 0.02,
                        ),
                        Column(
                          children: serviceApiKeys.map((apiKey) {
                            return Row(
                              children: [
                                Expanded(
                                  child: Padding(
                                    padding: const EdgeInsets.symmetric(
                                        vertical: 4.0),
                                    child: Text(
                                      apiKey.name,
                                      overflow: TextOverflow.ellipsis,
                                      maxLines: 1,
                                      style: TextStyle(
                                        fontSize: fontProv.fonts.textSize,
                                        color: Colors.black,
                                        fontFamily: fontType,
                                      ),
                                    ),
                                  ),
                                ),
                                IconButton(
                                  icon: Icon(CupertinoIcons.eye, size: 30),
                                  onPressed: () async {
                                    await editAPIKeyDialog(
                                      context,
                                      colorProv,
                                      formKey,
                                      fontProv,
                                      apiKey.name,
                                      apiKey.key,
                                      apiKey.serviceType,
                                    );
                                  },
                                ),
                                IconButton(
                                  icon: Icon(CupertinoIcons.delete, size: 30),
                                  onPressed: () async {
                                    await APIKeySharedPref.removeApiKey(
                                        apiKey.name, serviceType);
                                    ScaffoldMessenger.of(context).showSnackBar(
                                      SnackBar(
                                          backgroundColor: LgAppColors.lgColor4,
                                          content: Text(
                                            // 'API Key deleted. Please refresh your API keys',
                                            AppLocalizations.of(context)!
                                                .settings_apiKeyDeleteNotification,
                                            style: TextStyle(
                                              fontSize: fontProv.fonts.textSize,
                                              color: Colors.white,
                                              fontFamily: fontType,
                                            ),
                                          )),
                                    );
                                  },
                                ),
                                IconButton(
                                  icon: Icon(
                                      apiKey.isDefault
                                          ? CupertinoIcons.star_fill
                                          : CupertinoIcons.star,
                                      size: 30,
                                      color: apiKey.isDefault
                                          ? LgAppColors.lgColor3
                                          : Colors.grey),
                                  onPressed: () async {
                                    await APIKeySharedPref.saveDefaultApiKey(
                                        serviceType, apiKey.name);
                                    ScaffoldMessenger.of(context).showSnackBar(
                                      SnackBar(
                                          backgroundColor: LgAppColors.lgColor4,
                                          content: Text(
                                            // 'API Key is set as default. Please refresh your API keys',
                                            AppLocalizations.of(context)!
                                                .settings_apiKeySetDefaultNotification,
                                            style: TextStyle(
                                              fontSize: fontProv.fonts.textSize,
                                              color: Colors.white,
                                              fontFamily: fontType,
                                            ),
                                          )),
                                    );
                                  },
                                ),
                              ],
                            );
                          }).toList(),
                        ),
                      ],
                    ),
                  );
                }).toList(),
              ),
            );
          }
        },
      );
    },
  );
}