如何基于用户onTap启动具有不同功能的另一个屏幕



我目前正在研究一个数学应用程序,该应用程序有一个类别屏幕,其中有用于不同数学操作(加,减,除)的卡。当用户点击它显示下一个屏幕的问题和答案。我创建了一个单独的类来生成问题和答案,并在其中为每个数学运算创建了方法,如下所示

class QandAgeneration{
//Perform the addition quiz generation 
Map addition(){}
//Perform the subtraction quiz generation 
Map subtraction(){}
//Perform the division quiz generation 
Map division(){}
// Generate answers
List generateAnswers(){}
}

在下一个屏幕中,我想显示用户选择操作的输出,例如:-QandAgeneration.addition()作为小部件,这些小部件状态更改为显示下一个问题,当用户按下按钮选择答案时。我想我可以通过使用if else通过使用带字符串参数的构造函数将字符串值从类别屏幕传递到下一个屏幕来实现。然后我可以像下面这样做

if(widget.selectedType == 'addition'){
QandAgeneration.addition()
}
else if(widget.selectedType == 'subtraction'){
QandAgeneration.subtraction()
}else{
QandAgeneration.division()
}

这是实现此功能的最佳方式。还有其他方法吗?

为另一个屏幕创建一个类,并向该屏幕传递所需的参数。在另一个屏幕上,根据传递的数据实现功能。例如:

class AnotherScreen extends StatelessWidget {
final data;
AnotherScreen(this.data);
@override
void build(BuildContext context) {
if (data == 'firstType') {
return widgetForDataWithFirstType(data);
}
return widgetForDatawithSecondType(data);
}
}

移动到这个屏幕:

Navigator.push(
context,
MaterialPageRoute(builder: (context) => AnotherScreen(data)),
);

其中data—您需要接收到另一个屏幕的参数。

你可以在官方文档中阅读更多关于如何使用导航的信息。

相关内容

最新更新