Flutter:如何将变量从StatelessWidget传递到StatefulWidget [英] Flutter: How to pass variables from StatelessWidget to StatefulWidget

查看:837
本文介绍了Flutter:如何将变量从StatelessWidget传递到StatefulWidget的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是我无法将自己的类ForceSelection值从另一个屏幕传递到另一个屏幕的StatefulWidget。在StatelessWidget中工作正常。我试着从下面的教程中学习: https://flutter.dev/docs/cookbook/navigation/passing-data#4-navigate-and-pass-data-to-the-detail-screen

The problem is that I cannot pass the values of my own class ForceSelection values from another screen to another screen's StatefulWidget. It works fine in StatelessWidget. I have tried to learn from this flutter tutorial: https://flutter.dev/docs/cookbook/navigation/passing-data#4-navigate-and-pass-data-to-the-detail-screen

我在levelSelection.dart中有这类课程

I have this kind of class in levelSelection.dart

class ForceSelection {
  final String forceSelection;
  final String langSelection;

  ForceSelection(this.forceSelection, this.langSelection);
}

我正在尝试将值传递给下一个文件PlayQuiz.dart

I am trying to pass the values to next file PlayQuiz.dart

Game(forceSelection: ForceSelection('maa', 'fin'))

game.dart看起来像这样:

game.dart looks like this:

class Game extends StatelessWidget {
  final ForceSelection forceSelection;

  Game({Key key, @required this.forceSelection}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: forceSelection.langSelection, // TODO should work and change later
      theme: new ThemeData(
        primaryColor: Colors.white,
      ),
      home: PlayQuiz(),
    );
  }
}

我想将ForceSelection值传递给PlayQuiz( )

And I want to pass the ForceSelection values to PlayQuiz()

class PlayQuizState extends State<PlayQuiz> {
  Game.forceSelection // <--- How can I get the value out of here?
}

class PlayQuiz extends StatefulWidget {
  @override
  PlayQuizState createState() => new PlayQuizState(); 
}

完整代码可在此处找到: https://pastebin.com/nKssz42R
以及levelSelection.dart: https://pastebin.com/8QbBD0A2

The whole code can be found here: https://pastebin.com/nKssz42R and also levelSelection.dart: https://pastebin.com/8QbBD0A2

推荐答案

尝试此代码

在PlayQuiz()中添加了forceSelecton参数

class Game extends StatelessWidget {
final ForceSelection forceSelection;

Game({Key key, @required this.forceSelection}) : super(key: key);

@override
Widget build(BuildContext context) {
  return MaterialApp(
    title: forceSelection.langSelection, // TODO should work and change later
    theme: new ThemeData(
      primaryColor: Colors.white,
    ),
    home: PlayQuiz(forceSelection),
  );
 }
}

游戏测验中

class PlayQuiz extends StatefulWidget {
  final ForceSelection forceSelection;

  PlayQuiz(this.forceSelection);

  @override
  PlayQuizState createState() => new PlayQuizState(forceSelection); 
}

在PlayQuizState中

class PlayQuizState extends State<PlayQuiz> {
  ForceSelection forceSelection;

  PlayQuizState(ForceSelection forceSelection) {
    this.forceSelection = forceSelection;
  }
}

这篇关于Flutter:如何将变量从StatelessWidget传递到StatefulWidget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆