简体   繁体   English

如何在 flutter 中使用 function 导航页面?

[英]How to navigate page using function in flutter?

How to navigate to different page for each _buildcard function?如何导航到每个 _buildcard function 的不同页面? Below are my code.下面是我的代码。 I want to navigate to different page using _buildcard function but still get error since it is function and not class.我想使用 _buildcard function 导航到不同的页面,但仍然出现错误,因为它是 function 而不是 class。 Is there any way to do?有什么办法吗?

        _buildCard(3, "Azzahra\nVilla", 1400),
        _buildCard(4, "Afsana\nVilla", 1300)

        
 

var rating = 0.0;



_buildCard(img, text, price)
 {
  
    return Card(
        child: Row(children: <Widget>[
      Container(
        margin: EdgeInsets.all(8.0),
        width: 120.0,
        height: 180.0,
        decoration: BoxDecoration(
            image: DecorationImage(
                image: AssetImage("assets/$img.jpeg"), fit: BoxFit.cover),
            borderRadius: BorderRadius.circular(10.0)),
      ),
      Expanded(
          child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
            Padding(
                padding: EdgeInsets.all(8.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text(
                            "$text",
                            maxLines: 2,
                            textAlign: TextAlign.justify,
                            style: TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 22.0),
                          ),
                          SizedBox(height: 5.0),
                          Text(
                            "Janda Baik",
                            style:
                                TextStyle(fontSize: 11.0, color: Colors.grey),
                          ),
                          SizedBox(height: 5.0),
                          SmoothStarRating(
                            rating: rating,
                            isReadOnly: false,
                            size: 14,
                            filledIconData: Icons.star,
                            halfFilledIconData: Icons.star_half,
                            defaultIconData: Icons.star_border,
                            color: Colors.yellow,
                            starCount: 5,
                            allowHalfRating: true,
                            spacing: 2.0,
                            onRated: (value) {
                              print("rating value -> $value");
                              // print("rating value dd -> ${value.truncate()}");
                            },
                          ),
                        ]),
                    Column(children: <Widget>[
                      Text(
                        "\RM$price",
                        style: TextStyle(
                            fontSize: 22.0, fontWeight: FontWeight.bold),
                      ),
                      Text(
                        "per night",
                        style: TextStyle(fontSize: 11.0, color: Colors.grey),
                      )
                

Try this code试试这个代码

     return  InkWell(
    onTap: () {
text=='Azzahra\nVilla'?
    Navigator.push(context, MaterialPageRoute(builder: (context)=> AzzahraClassName())):
Navigator.push(context, MaterialPageRoute(builder: (context)=> AfsanaClassName())); 
    },
    child: Card(child:....))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Flutter 中使用 MenuItem 导航到不同的页面? - How to navigate to a different page using MenuItem in Flutter? Flutter 如何在搜索功能中导航到另一个页面 - Flutter How do you navigate to another page in search function 如何使用设备后退按钮导航到上一页 - Flutter - How to navigate to previous page using device back button - Flutter 如何使用 Flutter 中的 ListTile 导航到新页面/路由? - How do you navigate to a new page/route using a ListTile in Flutter? 如何在页面上导航后打开bottomSheet而不使用onPressed flutter? - how to open bottomSheet after navigate on the page without using onPressed flutter? 如何使用 Flutter 中“for 循环”生成的小部件导航到另一个页面? - How to navigate to another page using the widgets generated from a 'for loop' in Flutter? 在flutter中使用uni_links时如何导航到特定页面 - How to navigate to specific page when using uni_links in flutter Flutter:如何在使用 popUntil 导航到页面时更新页面 - Flutter: How to update a page when using popUntil to navigate to it 如何使用 Flutter 导航到屏幕 - How to Navigate to screens using Flutter 如何在不使用flutter_bloc更改Flutter中的state的情况下将一个页面导航到另一个页面 - How to navigate one page to another without changing state in Flutter using flutter_bloc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM