Created
November 7, 2020 08:43
-
-
Save Amitbhave/6c22fdde9386cdaba743b30f6380fde6 to your computer and use it in GitHub Desktop.
Flutter - Internationalization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:easy_localization/easy_localization.dart'; | |
import 'package:flutter/material.dart'; | |
class Home extends StatefulWidget { | |
@override | |
_HomeState createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
bool isHindi = false; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Flutter Internationalization'), | |
), | |
body: SafeArea( | |
child: Center( | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
Image.asset( | |
isHindi ? 'icons/flags/png/in.png' : 'icons/flags/png/us.png', | |
package: 'country_icons', | |
fit: BoxFit.cover, | |
), | |
SizedBox( | |
height: 25, | |
), | |
Text( | |
'message', | |
style: TextStyle( | |
fontSize: 18, | |
), | |
).tr(args: ['Flutter']), | |
SizedBox( | |
height: 20, | |
), | |
Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Text('Switch to Hindi?'), | |
Switch( | |
value: isHindi, | |
onChanged: (value) { | |
setState(() { | |
// this.setState is important here to change the locale! | |
this.setState(() { | |
if (value) { | |
context.locale = Locale('hi', 'IN'); | |
} else { | |
context.locale = Locale('en', 'US'); | |
} | |
}); | |
isHindi = value; | |
}); | |
}, | |
activeTrackColor: Colors.redAccent, | |
activeColor: Colors.red, | |
), | |
], | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment