在flutter中如何在国际化(Intl)和枚举之间进行通信?

new9mtju  于 2023-01-31  发布在  Flutter
关注(0)|答案(1)|浏览(144)

在Flutter中如何在国际化(Intl)和枚举之间进行通信?
就在下面我留下了一段代码,例如:

'import '../generated/l10n.dart';

enum AminoacidosEnum { TITULO(S.current.title), HELLOWORD(" ");

final String value;

const AminoacidosEnum(this.value); }'

'import 'package:flutter/material.dart'; import 'package:internacionalizacao/enums/aminoacidos_enum.dart';

import '../generated/l10n.dart';

class HomePage extends StatelessWidget { const HomePage({super.key});

@override Widget build(BuildContext context) { return Scaffold( body: Center( //child: Text (AppLocalizations.of(context)!.helloWorld), child: Column(children:[
      Text(AminoacidosEnum.TITULO),
      Text(S.of(context).helloWorld),
      Text(S.of(context).concatenedText('Thiago C. Pedroso')),
      Text(
          S.of(context).textWithPlaceHolders("Pedroso", 'Thiago Cristian')),
      Text(S.of(context).pageNotificationsCount(0)),
      Text(S.of(context).pageNotificationsCount(1)),
      Text(S.of(context).pageNotificationsCount(2)),
      //Text(S.of(context).gender(1)),
      Text(S.of(context).gender("male")),
      Text(S.current.pageHomeBalance(1234567890, DateTime.now())),
    ]),
  ),
);
} }'

Arguments of a constant creation must be constant expressions. Try making the argument a valid constant, or use 'new' to call the constructor. Arguments of a constant creation must be constant expressions. Try making the argument a valid constant, or use 'new' to call the constructor. A value of type 'Null' can't be assigned to a parameter of type 'String' in a const constructor. Try using a subtype, or removing the keyword 'const'.
mwecs4sa

mwecs4sa1#

我得到了我想要的,如果它对某人有用,我已经添加了一些使用它的方法。

enum AminoacidosEnum {
   TITULO("title"),
   HELLOWORD("helloWorld");
   final String value;
   const AminoacidosEnum(this.value);
 }


import 'package:flutter/material.dart';
import 'package:internacionalizacao/enums/aminoacidos_enum.dart';
// ignore: depend_on_referenced_packages
import 'package:intl/intl.dart';
import '../generated/l10n.dart';

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        //child: Text (AppLocalizations.of(context)!.helloWorld),
        child: Column(children: [
          //https://localizely.com/flutter-arb/
          //https://api.flutter.dev/flutter/intl/DateFormat-class.html

          Text(getEnumString(AminoacidosEnum.HELLOWORD)),
          Text(getEnumString(AminoacidosEnum.TITULO)),

          Text(Intl.message(AminoacidosEnum.HELLOWORD.value)),
          Text(Intl.message(AminoacidosEnum.HELLOWORD.value,
              name: AminoacidosEnum.HELLOWORD.value)),

          Text(
              S.of(context).textWithPlaceHolders("Pedroso", 'Thiago Cristian')),
          Text(S.of(context).pageNotificationsCount(0)),
          Text(S.of(context).pageNotificationsCount(1)),
          Text(S.of(context).gender("male")),
          Text(S.current.pageHomeBalance(1234567890, DateTime.now())),
        ]),
      ),
    );
  }

  String getEnumString(AminoacidosEnum enumValue) {
    switch (enumValue) {
      case AminoacidosEnum.TITULO:
        return Intl.message("AminoacidosEnum.TITULO", name: "title");
        break;
      case AminoacidosEnum.HELLOWORD:
        return Intl.message("AminoacidosEnum.HELLOWORD", name: "helloWorld");
        break;
    }
  }
}

相关问题