【问题标题】:The named parameter 'onTap' isn't defined未定义命名参数“onTap”
【发布时间】:2021-06-12 19:34:55
【问题描述】:

我目前正在学习 Flutter,对它很陌生。在我的应用程序中,我使用了响应式网格包并在响应式容器中添加了文本。我想在点击此文本时转到另一个页面,但我的错误是它给了我这个错误。

The named parameter 'onTap' isn't defined.
Try correcting the name to an existing named parameter's name, or defining a named parameter with the name 'onTap'.

我使用了以下代码:

Widget build(BuildContext context) {
return Scaffold(
  body: SingleChildScrollView(
    child: Container(
      child: ResponsiveGridRow(children: [
        ResponsiveGridCol(
          lg: 12,
          child: Container(
            height: 400,
            alignment: Alignment.center,
            color: Colors.orange,
            child: Column(
              children: [
                Container(
                  margin: EdgeInsets.only(top: 150),
                  alignment: Alignment.center,
                  child: Text("Welcome To",
                     style: TextStyle(
                       fontSize: 40,
                       color: Colors.white)),
                ),
                Container(
                  alignment: Alignment.center,
                  child: Text("our App",
                     style: TextStyle(
                       fontSize: 40,
                       color: Colors.white,
                       fontWeight: FontWeight.bold)),
                ),
              ],
              ),
          ),
        ),
             ResponsiveGridCol(
          xs: 4,
          md: 2,
          child: Container(
              height: 18,
              alignment: Alignment.centerLeft,
              child: Text("Login",
                  style: TextStyle(
                      fontSize: 13,
                      // decoration: TextDecoration.underline,
                      color: Colors.orange[800])),
              onTap: () { // i got error here
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => SignIn()),
                );
              }
              ),
        )
      ]),
    ),
  ),
);

} }

【问题讨论】:

    标签: flutter responsive-design flutter-layout flutter-container


    【解决方案1】:

    您的小部件没有 onTap 属性,您需要通过使用 gesture detector 或 InkWell 包装您需要可点击的小部件来创建如下所示的属性

    GestureDetector(
       onTap: () { 
                    Navigator.push(
                      context,
                      MaterialPageRoute(builder: (context) => SignIn()),
                    );
                  }
         child:Container(
                  height: 18,
                  alignment: Alignment.centerLeft,
                  child: Text("Login",
                      style: TextStyle(
                          fontSize: 13,
                          // decoration: TextDecoration.underline,
                          color: Colors.orange[800])),
                  
                  )),
    

    【讨论】:

      【解决方案2】:

      Container 小部件没有 onTap 属性尝试将其包装在 InkWell 中,如下所示:

      InkWell(
          onTap: () {
              Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => SignIn()),
              );
            },
         child: Container(
            height: 18,
            alignment: Alignment.centerLeft,
            child: Text("Login",
                style: TextStyle(
                    fontSize: 13,
                    // decoration: TextDecoration.underline,
                    color: Colors.orange[800])),
          )))
      

      【讨论】:

        猜你喜欢
        • 2022-01-04
        • 2020-02-13
        • 2022-11-01
        • 2020-06-24
        • 2018-05-14
        • 2020-02-16
        • 2021-06-15
        • 2020-12-03
        相关资源
        最近更新 更多