aws-amplify / amplify-flutter

A declarative library with an easy-to-use interface for building Flutter applications on AWS.
https://docs.amplify.aws
Apache License 2.0
1.31k stars 245 forks source link

【amplify_authenticator】Error extends InputResolver for localization support #1249

Closed quirkeer closed 2 years ago

quirkeer commented 2 years ago

Describe the bug A clear and concise description of what the bug is.

To Reproduce Steps to reproduce the behavior:

  1. Extends InputResolver
    class LocalizedInputResolver extends InputResolver {
    // 
    }
  2. Implement title method
    @override
    String title(BuildContext context, InputField field) {
    switch (field) {
      case InputField.username:
        return AuthenticatorInputLocalizations.of(context).username;
      case InputField.password:
        return AuthenticatorInputLocalizations.of(context).password;
      case InputField.newPassword:
        return AuthenticatorInputLocalizations.of(context).newPassword;
      case InputField.passwordConfirmation:
        final attributeName =
            AuthenticatorInputLocalizations.of(context).password;
        return AuthenticatorInputLocalizations.of(context)
            .confirmAttribute(attributeName);
      case InputField.verificationCode:
        return AuthenticatorInputLocalizations.of(context).verificationCode;
      case InputField.address:
        return AuthenticatorInputLocalizations.of(context).address;
      case InputField.birthdate:
        return AuthenticatorInputLocalizations.of(context).birthdate;
      case InputField.email:
        return AuthenticatorInputLocalizations.of(context).email;
      case InputField.familyName:
        return AuthenticatorInputLocalizations.of(context).familyName;
      case InputField.gender:
        return AuthenticatorInputLocalizations.of(context).gender;
      case InputField.givenName:
        return AuthenticatorInputLocalizations.of(context).givenName;
      // case InputField.locale:
      //   return AuthenticatorInputLocalizations.of(context).locale;
      case InputField.middleName:
        return AuthenticatorInputLocalizations.of(context).middleName;
      case InputField.name:
        return AuthenticatorInputLocalizations.of(context).name;
      case InputField.nickname:
        return AuthenticatorInputLocalizations.of(context).nickname;
      case InputField.phoneNumber:
        return AuthenticatorInputLocalizations.of(context).phoneNumber;
      // case InputField.picture:
      //   break;
      case InputField.preferredUsername:
        return AuthenticatorInputLocalizations.of(context).preferredUsername;
      // case InputField.profile:
      //   break;
      // case InputField.zoneinfo:
      //   break;
      // case InputField.updatedAt:
      //   break;
      // case InputField.website:
      //   break;
      case InputField.rememberDevice:
        return AuthenticatorInputLocalizations.of(context).rememberDevice;
      case InputField.usernameType:
        return AuthenticatorInputLocalizations.of(context).usernameType;
    }
    }
  3. See errors
    • Duplicate GlobalKey detected in widget tree
    • 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4647 pos 12: 'child == _child': is not true.

Expected behavior The l10n works good.

Code snippet for generating the l10n output

#!/bin/bash

HEADER=$(cat <<EOF
/*
 * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
import 'package:amplify_authenticator/amplify_authenticator.dart';
EOF
)

OUTPUT_DIR=lib/l10n/authenticator/generated
TEMPLATES=('titles_en.arb' 'buttons_en.arb' 'inputs_en.arb' 'countries_en.arb' 'messages_en.arb')
ARB_DIRS=('lib/l10n/authenticator/src/titles' 'lib/l10n/authenticator/src/buttons' 'lib/l10n/authenticator/src/inputs' 'lib/l10n/authenticator/src/countries' 'lib/l10n/authenticator/src/messages')
OUTPUT_CLASSES=('AuthenticatorTitleLocalizations' 'AuthenticatorButtonLocalizations' 'AuthenticatorInputLocalizations' 'AuthenticatorCountryLocalizations' 'AuthenticatorMessageLocalizations')
OUTPUT_FILES=('title_localizations.dart' 'button_localizations.dart' 'input_localizations.dart' 'country_localizations.dart' 'message_localizations.dart')

for i in "${!TEMPLATES[@]}"; do
    ARB_DIR=${ARB_DIRS[i]}
    TEMPLATE=${TEMPLATES[i]}
    OUTPUT_CLASS=${OUTPUT_CLASSES[i]}
    OUTPUT_FILE=${OUTPUT_FILES[i]}

    echo "Generating localizations for \"${ARB_DIR}/${TEMPLATE}\""

    flutter gen-l10n \
        --arb-dir $ARB_DIR \
        --output-dir $OUTPUT_DIR \
        --template-arb-file $TEMPLATE \
        --output-localization-file $OUTPUT_FILE \
        --output-class $OUTPUT_CLASS \
        --header "$HEADER" \
        --use-deferred-loading \
        --no-synthetic-package \
        --required-resource-attributes
done
# Clean up generated files
flutter format $OUTPUT_DIR

for i in "${!COUNTRY_OUTPUT_FILES[@]}"; do
flutter format "${COUNTRY_OUTPUT_FILES[i]}"
done

dart fix --apply
quirkeer commented 2 years ago

sorry fixed just now.

My bad, just forgot to add the delegate.

      localizationsDelegates: const [
        AuthenticatorButtonLocalizations.delegate,
+       AuthenticatorInputLocalizations.delegate,
      ],
haverchuck commented 2 years ago

@quirkeer Thanks for letting us know what the issue was. Closing this now.