Flutter 本地化不';无法正确使用Android Emulator和Chrome

Flutter 本地化不';无法正确使用Android Emulator和Chrome,flutter,localization,Flutter,Localization,你好,我是tonakainohana 我想本地化CupertinodePicker小部件 我试着这么做,以供参考 我终于明白了。像这样。我成功地用日语本地化了这个小部件 但是,当我在Android Emulator和Chrome中检查这个外观时,它并没有改变语言。它似乎是默认语言 请看下面的图片 我不明白那有什么意义。有人知道为什么在这些设备中本地化效果不好吗 这是我下面代码的一部分 import 'dart:math' as math; import 'package:flutter/c

你好,我是tonakainohana

我想本地化CupertinodePicker小部件

我试着这么做,以供参考

我终于明白了。像这样。我成功地用日语本地化了这个小部件

但是,当我在Android Emulator和Chrome中检查这个外观时,它并没有改变语言。它似乎是默认语言

请看下面的图片

我不明白那有什么意义。有人知道为什么在这些设备中本地化效果不好吗

这是我下面代码的一部分

import 'dart:math' as math;
import 'package:flutter/cupertino.dart';
import 'package:intl/intl.dart' as intl;  
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'JapaneseCupertinoLocalizations.dart' as jcl;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.

  @override
  Widget build(BuildContext context) {

    return MaterialApp(


      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        DefaultCupertinoLocalizations.delegate,
        jcl.JapaneseCupertinoLocalizations.delegate,
      ],

      supportedLocales: [
        const Locale('en', 'US'),
        const Locale('ja', 'JP'),
      ],
      //locale: Locale('ja', 'JP'),    //It supporsed to OK to commentout


      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: Page0(),
    );
  }
}



/* ━━━━━━━━━━━ 関数 ━━━━━━━━━━━ */
//CupertinoTimePicker
Widget _buildBottomPicker(Widget picker) {

  return Container(
    height:216,
    padding:const EdgeInsets.only(top:6.0),
    color: CupertinoColors.white,
    child:DefaultTextStyle(
      style:const TextStyle(
        color:CupertinoColors.black,
        fontSize:22.0,
      ),
      child: GestureDetector(
        onTap:(){},
        child:SafeArea(
            top:false,
            child: picker,
        )
      )
    ),
  );
}

Route _createNextRoute(Widget classRtnWidget) {
  return PageRouteBuilder(
    pageBuilder: (context, animation,secondaryAnimation) => classRtnWidget,
    transitionsBuilder: (context, animation, secondaryAnimation,child){
      var begin = Offset(1.0,0.0);
      var end = Offset.zero;
      var curve = Curves.ease;
      var tween = Tween(begin: begin, end: end).chain(CurveTween(curve:curve));

      return SlideTransition(
        position: animation.drive(tween),
        child: child,
      );
    },
  );
}
/* ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ */
/* ━━━━━━━━━ StatefulWidget ━━━━━━━━━━━━ */
class Page0 extends StatefulWidget {
  @override
  Page0State createState() => Page0State();
}

class Page2 extends StatefulWidget {
  @override
  Page2State createState() => Page2State();
}
/* ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ */
/* ━━━━━━━━━━━━━ State ━━━━━━━━━━━━━━ */
class Page0State extends State<Page0> {
  final Object heroTag = "next";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child:Text('アンケートにご協力ください'),
      ),
        floatingActionButton: FloatingActionButton(
          heroTag: heroTag,
          child: Icon(Icons.forward),
            onPressed: (){
              Navigator.push(
                  context,
                  _createNextRoute(Page2())
              );
            }
            ),
    );
  }
}

class Page2State extends State<Page2> {

  final Object heroTagNext = "next";
  final Object heroTagBefore = "before";

  DateTime _date = DateTime.now();

  @override
  void initState(){
    super.initState();
    initializeDateFormat();
  }

  Future<void> initializeDateFormat() async{
    await initializeDateFormatting("ja_JP", null);
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body:
          Stack(
            children:<Widget> [
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Container(
                    width:1000,
                    height:100,
                    child: Row(
                      children: <Widget>[
                    CupertinoButton(
                      child:Text('来所時間'),
                      onPressed: () {
                        //DateFormat debug
                        print( intl.DateFormat.yMMMEd('ja').format(_date));
                        showCupertinoModalPopup<void>(
                          context: context,
                          builder: (BuildContext context) {
                            return _buildBottomPicker(
                              CupertinoDatePicker(

                                mode: CupertinoDatePickerMode.dateAndTime,
                                initialDateTime: _date,
                                onDateTimeChanged: (DateTime newDateTime) {
                                  setState(() {
                                    _date = newDateTime;
                                  });
                                })
                            );
                          },
                        );
                      },
                    ),
                       // Text('DateFormat.yMMMEd().format($_date)'),
                      ]
                    ),
                  ),
                ],
              ),
              Positioned(
              //右の次へボタン
                bottom:50,
                right: 50,
                child:FloatingActionButton(
                  heroTag: heroTagNext,
                  child: Icon(Icons.forward),
                  onPressed: () {
                    Navigator.push(
                      context,
                      _createNextRoute(Page3())  //Actually Page3 Class Exists
                    );
                  },
                ),
              ),
              Positioned(
              //左の前へボタン
              bottom: 50,
              left:50,
              child: FloatingActionButton(
                heroTag: heroTagBefore,
                child:Icon(
                  Icons.forward,
                  textDirection: TextDirection.rtl,
                ),
                onPressed:(){
                  Navigator.pop(context);
                },
              ),
              ),
        ],
      ),
    );
  }
}

/* ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ */ 
导入'dart:math'作为数学;
进口“包装:颤振/cupertino.dart”;
将“package:intl/intl.dart”导入为intl;
导入“package:flatter_本地化/flatter_本地化.dart”;
导入“package:intl/date_symbol_data_local.dart”;
将“JapaneseCupertinologizations.dart”作为jcl导入;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
本地化授权:[
GlobalMaterialAllocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
DefaultCupertinoLocalizations.delegate,
jcl.japanesecupertinologizations.delegate,
],
支持的区域设置:[
常量区域设置('en','US'),
常量区域设置('ja','JP'),
],
//locale:locale('ja','JP'),//支持确定注释输出
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:Page0(),
);
}
}
/* ━━━━━━━━━━━ 関数 ━━━━━━━━━━━ */
//Cupertinotimecipker
小部件选择器(小部件选择器){
返回容器(
身高:216,
填充:仅限常量边集(顶部:6.0),
颜色:铜色。白色,
子项:DefaultTextStyle(
样式:consttextstyle(
颜色:铜色。黑色,
字体大小:22.0,
),
儿童:手势检测器(
onTap:(){},
儿童:安全区(
上图:错,
孩子:皮克,
)
)
),
);
}
路由_createNextRoute(小部件类rtnwidget){
返回页路由生成器(
pageBuilder:(上下文、动画、第二动画)=>classRtnWidget,
transitionsBuilder:(上下文、动画、第二动画、子动画){
var begin=偏移量(1.0,0.0);
var end=偏移量0.0;
var曲线=Curves.ease;
变量tween=tween(开始:开始,结束:结束).chain(曲线之间(曲线:曲线));
返回幻灯片转换(
位置:动画。驱动(吐温),
孩子:孩子,
);
},
);
}
/* ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ */
/* ━━━━━━━━━ 状态控件━━━━━━━━━━━━ */
类Page0扩展了StatefulWidget{
@凌驾
Page0State createState()=>Page0State();
}
类Page2扩展了StatefulWidget{
@凌驾
Page2State createState()=>Page2State();
}
/* ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ */
/* ━━━━━━━━━━━━━ 陈述━━━━━━━━━━━━━━ */
类Page0State扩展状态{
最终对象heroTag=“下一步”;
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
子:文本('アンケートにご協力ください'),
),
浮动操作按钮:浮动操作按钮(
heroTag:heroTag,
子:图标(图标向前),
已按下:(){
导航器。推(
上下文
_createNextRoute(第2页())
);
}
),
);
}
}
类Page2State扩展状态{
最终对象heroTagNext=“next”;
最终对象heroTagBefore=“before”;
DateTime _date=DateTime.now();
@凌驾
void initState(){
super.initState();
初始化为teformat();
}
未来初始化的TeFormat()异步{
等待初始化数据格式(“ja_JP”,空);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:
堆叠(
儿童:[
纵队(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
容器(
宽度:1000,
身高:100,
孩子:排(
儿童:[
丘比特纽扣(
子:文本('来所時間'),
已按下:(){
//日期格式调试
打印(intl.DateFormat.yMMMEd('ja')。格式(_-date));
showcupbertinomodalpopup(
上下文:上下文,
生成器:(BuildContext上下文){
回油选择器(
Cupertinodepicker(
模式:CupertinodePickerMode.dateAndTime,
initialDateTime:\u日期,
onDateTimeChanged:(日期时间newDateTime){
设置状态(){
_日期=newDateTime;
});
})
);
},
);
},
),
//Text('DateFormat.yMMMEd().format($\u date)'),
]
),
),
],
),
定位(
//右の次へボタン
底数:50,
右:50,,
子:浮动操作按钮(
heroTag:heroTagNex
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';

class _CupertinoLocalizationDelegate extends LocalizationsDelegate<CupertinoLocalizations> {
  const _CupertinoLocalizationDelegate();

  @override
  bool isSupported(Locale locale) => locale.languageCode == 'ja';

  @override
  Future<CupertinoLocalizations> load(Locale locale)=> JapaneseCupertinoLocalizations.load(locale);

  @override
  bool shouldReload( _CupertinoLocalizationDelegate old) => false;

  @override
  String toString() => 'DefaultCupertinoLocalizations.delegate(ja_JP)';
}

class JapaneseCupertinoLocalizations implements CupertinoLocalizations{

  const JapaneseCupertinoLocalizations();

  static const List<String> _shortWeekdays = <String>[
    '(月)',
    '(火)',
    '(水)',
    '(木)',
    '(金)',
    '(土)',
    '(日)',
  ];

  static const List<String> _shortMonths = <String> [
    '1月',
    '2月',
    '3月',
    '4月',
    '5月',
    '6月',
    '7月',
    '8月',
    '9月',
    '10月',
    '11月',
    '12月',
  ];

  static const List<String> _months = <String>[
    '1月',
    '2月',
    '3月',
    '4月',
    '5月',
    '6月',
    '7月',
    '8月',
    '9月',
    '10月',
    '11月',
    '12月',
  ];

  @override
  String datePickerYear(int yearIndex) => yearIndex.toString();

  @override
  String datePickerMonth(int monthIndex) => _months[monthIndex -1];

  @override
  String datePickerDayOfMonth(int dayIndex) => dayIndex.toString();

  @override
  String datePickerHour(int hour) => hour.toString();

  @override
  String datePickerHourSemanticsLabel( int hour) => hour.toString() + "時";

  @override
  String datePickerMinute(int minute) => minute.toString().padLeft(2, '0');

  @override
  String datePickerMinuteSemanticsLabel( int minute) {
    if(minute == 1)
      return '1 分';
    return minute.toString() + '分';
  }

  @override
  String datePickerMediumDate(DateTime date) {
    return
        '${_shortMonths[date.month - DateTime.january]} '
        '${date.day.toString().padRight(2)+ '日'}'
        '${_shortWeekdays[date.weekday - DateTime.monday] } ';
  }

  @override
  DatePickerDateOrder get datePickerDateOrder => DatePickerDateOrder.mdy;

  @override
  DatePickerDateTimeOrder get datePickerDateTimeOrder => DatePickerDateTimeOrder.date_time_dayPeriod;

  @override
  String get anteMeridiemAbbreviation => '午前';

  @override
  String get postMeridiemAbbreviation => '午後';

  @override
  String get alertDialogLabel => 'Info';

  @override
  String timerPickerHour(int hour ) => hour.toString();

  @override
  String timerPickerMinute(int minute) => minute.toString();

  @override
  String timerPickerSecond(int second) => second.toString();

  @override
  String timerPickerHourLabel(int hour)  => hour == 1 ? '時' : '時'; //勉強のため参考の書式のまま

  @override
  String timerPickerMinuteLabel(int minute) => '分';

  @override
  String timerPickerSecondLabel(int second) => '秒';

  @override
  String get cutButtonLabel => 'カット';

  @override
  String get copyButtonLabel => 'コピー';

  @override
  String get pasteButtonLabel => 'ペースト';

  @override
  String get selectAllButtonLabel => '選択';

  @override
  String get todayLabel => '今日';  //参考サイトにはなかったが、参照しないとエラーが出るので独自で追加
  /// Creates an object that provides US English resource values for the
  /// cupertino library widgets.
  ///
  /// The [locale] parameter is ignored.
  /// localeパラメーターは無視されている。
  /// This method is typically used to create a [LocalizationsDelegate].
  /// LocalizationsDelegateによってこのメソッド(load)は呼ばれる
  static Future<CupertinoLocalizations> load(Locale locale) {
    return SynchronousFuture<CupertinoLocalizations>(const JapaneseCupertinoLocalizations());
  }

  /// A [LocalizationsDelegate] that uses [DefaultCupertinoLocalizations.load]
  /// to create an instance of this class.
  static const LocalizationsDelegate<CupertinoLocalizations> delegate = _CupertinoLocalizationDelegate();
} 
  localizationsDelegates: [
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    DefaultCupertinoLocalizations.delegate,
    jcl.JapaneseCupertinoLocalizations.delegate,
  ],

  supportedLocales: [
    const Locale('en', 'US'),
    const Locale('ja', 'JP'),
  ],
  //locale: Locale('ja', 'JP'),    // ×
  locale: Locale('ja', 'JP'),      // 〇 It needs write locale.