Flutter 日期控件展示汉化 showDatePicker (五)

  _showDataPicker(int type) async {
    Locale myLocale = Localizations.localeOf(context);
    var picker = await showDatePicker(
        context: context,
        initialDate: DateTime.now(),
        firstDate: DateTime(1984),
        lastDate: DateTime(DateTime.now().year+1),
        locale: myLocale);
    setState(() {
      if(type==1){
        startTime = picker==null? "请选择日期":picker.toString().split(" ")[0].trim();
        }else{
        endTime= picker==null? "请选择日期":picker.toString().split(" ")[0].trim();
      }
    });
  }
                        onTap: () {
                          _showDataPicker(1);
                        },

设置中文: main入口

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      //国际语言包
      localizationsDelegates:[GlobalMaterialLocalizations.delegate,GlobalWidgetsLocalizations.delegate],
      supportedLocales: [const Locale('zh', 'CH')],
      theme: ThemeData(
          primaryColor: themeColor,
          textTheme: TextTheme(
            display1: TextStyle(
                color: Colors.black54,
                fontSize: 16,
                fontWeight: FontWeight.bold
            )
          )
//        backgroundColor: Colors.black12,
        //cardColor:Colors.black12,
//        canvasColor: Colors.black12
      ),
      home: HomeScaffoldView(),
      routes: MyRoutes.routes,
    );
  }

依赖

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

效果图:Flutter 日期控件展示汉化 showDatePicker (五)_第1张图片

 

你可能感兴趣的:(flutter)