早上好,我有几个页面,在应用程序栏中有一个按钮,可以很好地更改语言,当我更改语言时,我关闭应用程序,然后重新打开应用程序,一切都很好。当我更改语言时,我通过单击图像在页面之间导航,它也很好。
我的问题是,当我改变语言,我点击左箭头在顶部的Flutter应用程序(appbar)的语言更改是不适用于在前一页!你能帮我请。谢谢。
import 'package:flutter/material.dart';
import 'package:firstapps/language/langue.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:firstapps/pages/auther/Index_history.dart';
import 'package:firstapps/pages/Visites.dart';
import 'package:firstapps/pages/Carte.dart';
import 'package:firstapps/pages/mv.dart';
import 'package:firstapps/pages/lmhb.dart';
import 'package:firstapps/pages/hbd.dart';
class Accueil extends StatefulWidget {
const Accueil({Key? key}) : super(key: key);
@override
_Accueil createState() => _Accueil();
}
class _Accueil extends State<Accueil> {
late SharedPreferences _prefs;
String _langue = '';
@override
void initState() {
super.initState();
SharedPreferences.getInstance().then((prefs) {
setState(() {
_prefs = prefs;
_langue = _prefs.getString('langue') ?? 'fr';
});
});
}
void _changerLangue(String nouvelleLangue) async {
setState(() {
_langue = nouvelleLangue;
});
await _prefs.setString('langue', nouvelleLangue);
}
Widget _buildGridTile(String title, String imageAsset, Widget page) {
return GridTile(
child: InkWell(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => page));
},
child: ClipRRect(
borderRadius: BorderRadius.circular(10.0),
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(imageAsset),
fit: BoxFit.cover,
colorFilter: ColorFilter.mode(Colors.black.withOpacity(0.6), BlendMode.darken), // Assombrit l'image avec un opacité de 0.5
),
),
child: Center(
child: Text(
title,
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontFamily: 'poppinslight',
),
textAlign: TextAlign.center,
),
),
),
),
),
);
}
@override
Widget build(BuildContext context) {
return FutureBuilder<SharedPreferences>(
future: SharedPreferences.getInstance(),
builder: (BuildContext context, AsyncSnapshot<SharedPreferences> snapshot) {
if (snapshot.hasData) {
final prefs = snapshot.data!;
final _language = Language();
final _langue = prefs.getString('langue') ?? 'fr';
_language.setLanguage(_langue);
return ChangeNotifierProvider.value(
value: _language,
child: Scaffold(
appBar: AppBar(
title: Text(_language.transchoixcat()),
actions: <Widget>[
DropdownButton<String>(
value: _langue,
onChanged: (String? nouvelleLangue) {
if (nouvelleLangue != null) {
_changerLangue(nouvelleLangue);
_language.setLanguage(nouvelleLangue);
}
},
items: <String>['fr', 'ar'].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Row(
children: <Widget>[
Image.asset(
'assets/images/flag-${value}.png', // chemin de l'image correspondante
height: 24,
width: 24,
),
SizedBox(width: 10,),
Text(value),
],
),
);
}).toList(),
),
],
),
body: Center(
child: GridView.count(
crossAxisCount: 2, // nombre de colonnes
mainAxisSpacing: 16.0, // espacement vertical entre les images
crossAxisSpacing: 16.0, // espacement horizontal entre les images
padding: EdgeInsets.all(16.0),
children: [
_buildGridTile(_language.transhisttoir(), 'assets/images/histoire.jpg', Index_history()),
_buildGridTile(_language.transvisit(), 'assets/images/visites.jpg', Visites()),
_buildGridTile(_language.transcart(), 'assets/images/carte.jpg', Carte()),
_buildGridTile(_language.transvide(), 'assets/images/mv.jpg', mv()),
_buildGridTile(_language.transmise(), 'assets/images/lmhb.jpg', lmhb()),
_buildGridTile(_language.transapro(), 'assets/images/hbd.jpg', hbd()),
],
),
),
),
);
} else {
return Container();
}
},
);
}
}```
when i change the language it works great my only problem is with the back button which is in the appbar of flutter, when i change the language and i go back the language is not applied but when i click on the images to lavigate the language is well applied.
1条答案
按热度按时间ztyzrc3y1#
在这种情况下,你需要的是使用状态管理来持久化语言环境。在屏幕B中执行此操作并弹出到屏幕A并不意味着屏幕A会感知到更改并更改所有内容,相反,屏幕A将保持与您将新屏幕推到导航堆栈时相同的状态。在这种情况下,状态管理将如何帮助您:
在屏幕B或屏幕A中更改某些内容,您对状态所做的每个更改都会反映到调用该状态的其他屏幕。
**编辑:**如果您想测试数据是否正确保存,您可以在更改区域设置的屏幕后推送一个新屏幕。