使用GetX更改主题

问题描述

我只想在特定路线上将主题更改为import java.util.Scanner; import java.util.concurrent.ThreadLocalRandom; import java.util.Calendar; import java.util.Date; // How to create a Jumble word game in Java public class MyProgram { private static final String[] WORDS_DATABASE = new String[] { "code","kids","baby","dosa","beach","house","apple","paper","Java","school","science","Oracle","Disney","cricket","carrot","vada" }; private static String[] hints = new String[] { "Programs are made of ___","Synonym for children","Synonym for infant","Idli,vada,and ___","This place is next to ocean","Other word for 'home'","This is a red color fruit","pencil and ____","programming language","place for education","Physics,chemistry,biology are all ____","Larry Ellison founded this company","the ____ princesses","Famous sport with bat and ball","What rabbits eat","idli and ___" }; public static void main(String[] args) { MyProgram jg = new MyProgram(); jg.startGame(); } /** * Run a game of Jumble in Java. The steps in the game are,* 1. Get a random word from the words database * 2. Shuffle/jumble the word by randomly shuffling characters * 3. Present the jumbled word to the user and ask him to guess the word. * 4. Repeat the guess till answer is found or user decides to quit. */ private void startGame() { // int numberOfGuesses = 0; // String original = selectRandomWord(); //String shuffled = getShuffledWord(original); boolean gameOn = true; while(gameOn) { int numberOfGuesses = 0; String original = selectRandomWord(); String shuffled = getShuffledWord(original); System.out.println("Shuffled word is: "+shuffled); numberOfGuesses++; String userGuess = getUserGuess(); if(original.equalsIgnoreCase(userGuess)) { System.out.println("Congratulations! You found the word in "+numberOfGuesses+" guesses"); System.out.println(""); //gameOn = false; }else { System.out.println("Sorry,Wrong answer"); System.out.println("--------"); if (numberOfGuesses > 0){ displayHint(original); System.out.println(""); // String userGuess = getUserGuess(); } } } } /** * Get the user's word guess from command line * @return */ public String getUserGuess() { Scanner sn = new Scanner(System.in); System.out.println("Please type in the original word: "); return sn.nextLine(); } /** * Select a random word from the WORDS_DATABASE array. * @return */ public String selectRandomWord() { int rPos = ThreadLocalRandom.current().nextInt(0,WORDS_DATABASE.length); return WORDS_DATABASE[rPos]; } /** * Shuffle the original word by randomly swapping characters 10 times * @param original * @return */ public String getShuffledWord(String original) { String shuffledWord = original; // start with original int wordSize = original.length(); int shuffleCount = 10; // let us randomly shuffle letters 10 times for(int i=0;i<shuffleCount;i++) { //swap letters in two indexes int position1 = ThreadLocalRandom.current().nextInt(0,wordSize); int position2 = ThreadLocalRandom.current().nextInt(0,wordSize); shuffledWord = swapCharacters(shuffledWord,position1,position2); } return shuffledWord; } /** * Swaps characters in a string using the given character positions * @param shuffledWord * @param position1 * @param position2 * @return */ private String swapCharacters(String shuffledWord,int position1,int position2) { char[] charArray = shuffledWord.toCharArray(); // Replace with a "swap" function,if desired: char temp = charArray[position1]; charArray[position1] = charArray[position2]; charArray[position2] = temp; return new String(charArray); } private void displayHint(String s){ for (int i = 0; i < WORDS_DATABASE.length; i++){ if (WORDS_DATABASE[i].equals(s)){ System.out.println("HINT:" + hints[i]); } } } } 。为此,我使用了redTheme之类的

routingCallback

不幸的是,这是造成的

════════小部件库捕获到异常 ══════════════════════════════════════════════════ ═════setState()或 在构建过程中调用markNeedsBuild()。相关的引起错误 小部件是:方向性 文件:///Users/jakub/.pub-cache/hosted/pub.dartlang.org/get_navigation-3.12.0/lib/src/root/root_widget.dart:235:22 ══════════════════════════════════════════════════ ══════════════════════════════════════════════════

因此,我将主题更改包装在try-catch块中,因此一开始我没有遇到异常

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'App',theme: defaultTheme(context),initialRoute: AppPages.INITIAL,getPages: AppPages.routes,routingCallback: (routing) => _routingCallback(context,routing),);
  }

  _routingCallback(BuildContext context,Routing routing) {
    if (routing.current == Routes.PURSUE_MODE) {
      _changeThemeIfNeeded(redTheme(context));
    } else {
      _changeThemeIfNeeded(defaultTheme(context));
    }
  }

  void _changeThemeIfNeeded(ThemeData theme) {
    Get.changeTheme(theme);
  }
}

但是我相信使用 void _changeThemeIfNeeded(ThemeData theme) { try { if (Get.theme != theme) { Get.changeTheme(theme); } } catch (e) { print('Not ready e = $e'); } } 框架可以更优雅地解决这个问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)