android - 颤振 - 如何将类保存在变量中
问题描述
我的应用程序中有一个项目列表。在每个项目上,都会导致不同的页面,我怎样才能将类名存储到变量中。如果我的问题有任何其他解决方案,我会很乐意接受。谢谢你
这是我的列表项代码
ItemMenu(
image: "assets/images/logo.png",
title: "Agama",
page: "Agama_page()",
),
ItemMenu(
image: "assets/images/logo.png",
title: "Gender",
page: "Gender_page()",
),
ItemMenu(
image: "assets/images/logo.png",
title: "Geografi",
page: "Geografi_page()",
)
这是项目菜单代码的类
class ItemMenu extends StatelessWidget {
final String image;
final String title;
final String page;
const ItemMenu({
Key key,
this.image,
this.title,
this.page,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
Get.to(() => page));
},
child: Container(
padding: EdgeInsets.all(10),
// width: 100,
margin: EdgeInsets.only(right: 20, bottom: 10, top: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Colors.white,
boxShadow: [
BoxShadow(
offset: Offset(0, 10),
blurRadius: 20,
color: Color(0xFF4056C6).withOpacity(.15),
)
]),
child: Column(
children: <Widget>[
Image.asset(
image,
height: 90,
),
Text(
title,
style: TextStyle(fontWeight: FontWeight.bold),
)
],
),
),
);
}
}
解决方案
您的问题不清楚,但在您的代码中,我看到您没有正确导航。
您将字符串而不是小部件提供给 Get.to() 方法。
尝试像这样更新您的代码。
class ItemMenu extends StatelessWidget {
final String image;
final String title;
final String page;
const ItemMenu({
Key key,
this.image,
this.title,
this.page,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
//! change this part in your code
//Get.to(() => page)); <- change this
if(page == 'Agama_page()'){ // <- to this
Get.to(() => AgamaPage());
}
//TODO: do the rest off you navigation below
},
child: Container(
padding: EdgeInsets.all(10),
// width: 100,
margin: EdgeInsets.only(right: 20, bottom: 10, top: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Colors.white,
boxShadow: [
BoxShadow(
offset: Offset(0, 10),
blurRadius: 20,
color: Color(0xFF4056C6).withOpacity(.15),
)
]),
child: Column(
children: <Widget>[
Image.asset(
image,
height: 90,
),
Text(
title,
style: TextStyle(fontWeight: FontWeight.bold),
)
],
),
),
);
}
}
// and of course create separate screen for every navigation
class AgamaPage extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
body:Center(child:Text('AgamaPage')) );
}
或者您可以定义路线并使用 Get.toNamed()
推荐阅读
- python - 验证和过滤多索引 Pandas 数据框中的数据
- php - 如何在 Twig/Timber Wordpress 中将周转换为天
- javascript - AJAX、JS、setInterval。多次显示数据
- json - 将 CSV 数据的 JSON 表示形式转换为实际的 CSV 数据
- json - 将内存中 JSON 数据插入 Postgres 的最快方法,除了 json_populate_record
- swift - 如何为 UIStepper 实现观察者设计模式
- postgresql - 如何装饰我的实体字段多对多关系 TypeOrm NestJS
- r - 在 R 中使用 Tidyverse 嵌套 for 循环,其中迭代器在第一个循环中生成
- javascript - 如果我在不同的相应字段中有数据,则更新字段
- powershell - 通过 ansible 执行 .ps1 文件