在下面包含底部导航栏项目的主屏幕类的代码下面,我的问题是除了这个底部导航栏标签之外,所有其他字符串转换都可以工作.
class Main extends StatefulWidget {
}
class _MainState extends State<Main> {
List<BottomNavigationBarItem> items = [
BottomNavigationBarItem(
icon: Image.asset(icons[0]),
label: 'home'.tr(),
),
BottomNavigationBarItem(
icon: Image.asset(icons[5]),
label: 'comments'.tr(),
),
BottomNavigationBarItem(
icon: Image.asset(icons[1]),
label: 'settings'.tr(),
),
BottomNavigationBarItem(
icon: Image.asset(icons[2]),
label: 'tools'.tr(),
),
];
@override
Widget build(BuildContext context) {
return BlocBuilder<MainBloc, MainState>(
builder: (context, state) {
return Scaffold(
bottomNavigationBar: Container(
color: Colors.white,
child: Row(
children: items.asMap().entries.where((element) =>
state.itemShown![element.key]).map((element) {
final index = element.key;
final item = element.value;
final isSelected = state.actualIndex == index;
return InkWell(
onTap: () {
},
child: Padding(
padding: const EdgeInsets.only(bottom: 50.0,top: 16),
child: Container(
decoration: BoxDecoration(
color: isSelected
? ThemeManager.colors
: Colors.white,
borderRadius: BorderRadius.circular(20.0),
),
padding: const EdgeInsets.symmetric(
horizontal: 25, vertical: 15),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Image.asset(
icons[index],
fit: BoxFit.cover,
width:
MediaQuery.of(context).size.width / 15,
color: isSelected
? Colors.white
: Colors.black54,
),
Text(
item.label!,
style: TextStyle(
color: isSelected ? Colors.white : Colors.black,
fontWeight: FontWeight.w400,
// Text color
),
),
],
),
),
),
);
}).toList(),
),
),
);
},
);
}
}
下面是main.dart类代码
Future<void>main() async {
await runZonedGuarded (
() async{
WidgetsFlutterBinding.ensureInitialized();
await EasyLocalization.ensureInitialized();
runApp((EasyLocalization(
supportedLocales: const [Locale('en', 'US'),Locale('es', 'ES')],
path: 'assets/translations',
fallbackLocale: const Locale('en', 'US'),
child: const MyApp())));
},
);
}
/// inside material app
MaterialApp(
debugShowCheckedModeBanner: false,
onGenerateRoute: Routes.generateRoute,
theme: ThemeManager.lightTheme,
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
initialRoute:Routes.splashScreen,
),
在这里,我使用一个下拉列表来 Select 语言翻译,在 Select 所选语言之后,我将调用此函数.
在ONTAP函数上,我使用了以下代码.
LocalizationChecker.changeLanguage(context,newValue);
本地化判断器类下方
class LocalizationChecker {
static changeLanguage(BuildContext context, String language) {
Locale? currentLocal = EasyLocalization.of(context)!.currentLocale;
if (language == 'English') {
EasyLocalization.of(context)!.setLocale(const Locale('es', 'ES'));
} else {
EasyLocalization.of(context)!.setLocale(const Locale('en', 'US'));
}
}
}
我的主要问题是点击此按钮后,本地化在除底部导航栏以外的所有屏幕上都有效.