0

我在可能会颤动的应用程序中有两个按钮。其中一个用于切换主题,另一个用于更改语言环境以支持应用程序中的英语和波斯语。一切正常,应用程序正常工作,但是当我想为每种语言的文本设置主题时,textstyle 设置不正确。我定义了两个文本主题,意思是波斯语的 faTextTheme 和英语的 enTextTheme,但文本上只设置了 entexttheme

这是我的颤振代码:

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({
    Key? key,
  }) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Locale _locale = Locale('en');

  @override
  Widget build(BuildContext context) {
    return AdaptiveTheme(
      light: ThemeData(
          brightness: Brightness.light,
          primarySwatch: Colors.red,
          textTheme: _locale == Locale('fa') ? faTextTheme : enTextTheme),
      dark: ThemeData(
          brightness: Brightness.dark,
          primarySwatch: Colors.red,
          textTheme: _locale == Locale('fa') ? faTextTheme : enTextTheme),
      initial: AdaptiveThemeMode.light,
      builder: (theme, darkTheme) => MaterialApp(
        title: 'Adaptive Theme Demo',
        localizationsDelegates: [
          AppLocalizations.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
        ],
        supportedLocales: AppLocalizations.supportedLocales,
        theme: theme,
        locale: _locale,
        darkTheme: darkTheme,
        home: MyHomePage(
          changedLanguage: (Languages newLanguage) {
            setState(() {
              newLanguage == Languages.fa
                  ? _locale = Locale('fa')
                  : _locale = Locale('en');
            });
          },
        ),
      ),
    );
  }
}

TextTheme get enTextTheme =>
    GoogleFonts.robotoTextTheme(TextTheme(bodyText2: TextStyle(fontSize: 10,fontWeight: FontWeight.bold)));

TextTheme get faTextTheme => TextTheme(
      bodyText2: TextStyle(
          fontSize: 40, fontFamily: 'vazir', ),
    );

class MyHomePage extends StatefulWidget {
  final Function(Languages language) changedLanguage;
  const MyHomePage({required this.changedLanguage});
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Languages languages = Languages.en;

  void updateSelectedLanguage(Languages newLanguges) {
    widget.changedLanguage(
        languages == Languages.fa ? Languages.en : Languages.fa);
    setState(() {
      languages = newLanguges;
    });
  }

  @override
  Widget build(BuildContext context) {
    var localization = AppLocalizations.of(context)!;
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Center(
            child: Text(localization.helloWorld),
          ),
          ElevatedButton(
              onPressed: () {
                // AdaptiveTheme.of(context).toggleThemeMode();
                final ThemeData themeData = Theme.of(context);
                var currentBrightness =
                    themeData.brightness == Brightness.dark ? 'dark' : 'light';

                if (currentBrightness == 'dark') {
                  AdaptiveTheme.of(context).setLight();
                } else {
                  AdaptiveTheme.of(context).setDark();
                }
              },
              child: Text(localization.toggleThemeBtn)),
          ElevatedButton(
              onPressed: () {
                var newLanguage;
                if (languages == Languages.fa) {
                  newLanguage = Languages.en;
                } else {
                  newLanguage = Languages.fa;
                }
                updateSelectedLanguage(newLanguage);
              },
              child: Text(
                localization.changeLanguageBtn,
              ))
        ],
      ),
    );
  }
}

enum Languages { en, fa }

当我调试一切正常时。当语言环境为英语时,它们将被返回,当语言环境为 fa 时,主题将被返回。但文本没有正确的主题和字体谢谢你们。

4

0 回答 0