【问题标题】:How to navigate to a different screen with onTap in flutter如何在颤动中使用 onTap 导航到不同的屏幕
【发布时间】:2021-07-18 22:27:01
【问题描述】:

当用户点击警报对话框中的按钮时,我试图将用户导航到不同的屏幕,所以我用墨水瓶包装容器,然后使用 onTap () 和导航器推送替换:

onTap: (){
           Navigator.pushReplacement(
           context,MaterialPageRoute(builder: (context) => Subscription ()),);
          } 

但这给了我一个错误提示:错误:3 positional argument(s) expected, but 0 found.

InkWell() 的完整块

child: InkWell(
                            child: Container(
                            child: Center(
                              child: Text(
                                "Subscribe Now",
                                style: TextStyle(
                                    fontSize: 14,
                                    color: textColor,
                                    fontWeight: FontWeight.bold),
                              ))),
                              onTap: (){
                                Navigator.pushReplacement(
                                  context,MaterialPageRoute(builder: (context) => Subscription ()),);
                        }
                          )

订阅类:

    class Subscription extends StatefulWidget {
  final bool isPaymentSuccess;
  final User currentUser;
  final Map items;
  Subscription(this.currentUser, this.isPaymentSuccess, this.items);

  @override
  _SubscriptionState createState() => _SubscriptionState();
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您需要通过导航传递 yoru 参数,如下所示:

    
    onTap:(){
    Navigator.pushReplacement(
      context,MaterialPageRoute(builder: (context) => Subscription (true, currentUser,items) //here pass the actual values of these variables, for example false if the payment isn't successfull..etc
    ),);
    } 
    

    【讨论】:

      【解决方案2】:

      订阅页面有3个参数需要传入...

      你需要传入值

      Final bool ispaymentSuccess;
      Final User currentUser;
      Final Map items;
      

      这就是错误出现的原因......

      为了更好的用户体验... 给所有订阅构造函数@required,这样当你想要导航到页面时它会要求你传递这些值......

      这样

      Subscription({@required this.isPaymentSuccess, @required this.items, @required this.currentUser});
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-11
        • 2023-03-31
        • 2021-09-18
        • 2018-10-05
        • 2020-02-08
        • 2021-03-06
        • 2020-03-19
        • 2020-05-24
        相关资源
        最近更新 更多