首页 > 解决方案 > 颤振 - 如何将类保存在变量中

问题描述

我的应用程序中有一个项目列表。在每个项目上,都会导致不同的页面,我怎样才能将类名存储到变量中。如果我的问题有任何其他解决方案,我会很乐意接受。谢谢你

这是我的列表项代码

                  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),
            )
          ],
        ),
      ),
    );
  }
}

标签: androidflutterdart

解决方案


您的问题不清楚,但在您的代码中,我看到您没有正确导航。

您将字符串而不是小部件提供给 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()


推荐阅读