【问题标题】:Flutter - onPressed functionFlutter - onPressed 函数
【发布时间】:2019-03-30 16:28:57
【问题描述】:

我需要在下面的图标上插入onpressed()函数(导航器推送到其他页面):

Column buildButtonColumn(IconData icon, String label) {
    Color color = Theme.of(context).primaryColor;
      return Column(
      mainAxisSize: MainAxisSize.min,
       mainAxisAlignment: MainAxisAlignment.center,
       children: [
      Icon(icon, color: color),
      Container(
        margin: const EdgeInsets.only(top: 8.0),
        child: Text(
          label,
          style: TextStyle(
            fontSize: 12.0,
            fontWeight: FontWeight.w400,
            color: color,
             ),
           ),
         ),
       ],
     );
   }

   Widget buttonSection = Container(
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        buildButtonColumn(Icons.call, 'CALL'),
        buildButtonColumn(Icons.near_me, 'ROUTE'),
        buildButtonColumn(Icons.share, 'SHARE'),
      ],
    ),
  );

【问题讨论】:

标签: flutter


【解决方案1】:

用 GestureDetector 包裹您的列:

要转到其他页面,您可以使用 Navigator.pushNamed 或 Navigator.push,例如 PushNamed 在代码中

Widget buildButtonColumn(IconData icon, String label) {
    Color color = Theme.of(context).primaryColor;
    return GestureDetector(
      onTap: (){
        Navigator.pushNamed(context, label);
      },
      child: Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(icon, color: color),
          Container(
            margin: const EdgeInsets.only(top: 8.0),
            child: Text(
              label,
              style: TextStyle(
                fontSize: 12.0,
                fontWeight: FontWeight.w400,
                color: color,
              ),
            ),
          ),
        ],
      ),
    );
  }

【讨论】:

    【解决方案2】:

    您可以为此使用IconButton 小部件。

    IconButton(
         icon:  Icon(Icons.info),
         onPressed: (){
               // Do something               
         },
     )
    

    你们可以使用GestureDetector

    GestureDetector(
      onTap: () {
        //Do something
      },
      child: icon: Icon(Icons.info),
    )
    

    编辑:

    对下面粘贴的代码稍作修改。更改 buildButtonColumn:

    buildButtonColumn(icon, label, onTap) {
          Color color = Theme.of(context).primaryColor;
          return GestureDetector(
            onTap: onTap,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(icon, color: color),
                Container(
                  margin: const EdgeInsets.only(top: 8.0),
                  child: Text(
                    label,
                    style: TextStyle(
                      fontSize: 12.0,
                      fontWeight: FontWeight.w400,
                      color: color,
                    ),
                  ),
                ),
              ],
            ),
          );
        }
    

    对于按钮部分,使用它。

    Widget buttonSection = Container(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              buildButtonColumn(Icons.call, 'CALL', (){
                    print('call');
              }),
              buildButtonColumn(Icons.near_me, 'ROUTE', (){
                print('route');
              }),
              buildButtonColumn(Icons.share, 'SHARE', (){
                print('share');
              }),
            ],
          ),
        );
    

    【讨论】:

    • nonybrighto tks 用于回复,但它不适用于我的代码。另一方面,我需要将 Widget buttonSection 与容器一起使用,我需要在代码中插入函数。有可能吗?
    • 你是如何实现的?显示 buildButtonColumn 的代码
    • nonybrighto tks 再次,请参阅上面的整个按钮代码。
    • nonybrighto 工作得非常好,但我怎样才能将用户发送到 3 个按钮中的每一个上的另一个页面?我尝试使用 onPressed 但没有工作我编辑了您的更改
    • @mLstudent33 有一个空的 onPressed 使按钮处于活动状态/启用状态,因此即使按钮没有任何操作,您仍然可以获得飞溅效果。
    【解决方案3】:

    你可以用这个:

    GestureDetector buildButtonColumn(IconData icon, String label){
        onTap(){
          print("Pressed");
        }
        Color color = Theme.of(context).primaryColor;
        return GestureDetector(
          onTap: onTap,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(icon, color: color),
              Container(
                margin: const EdgeInsets.only(top: 8.0),
                child: Text(
                  label,
                  style: TextStyle(
                    fontSize: 12.0,
                    fontWeight: FontWeight.w400,
                    color: color,
                  ),
                ),
              ),
            ],
          ),
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
        home: Scaffold(
          body: Center(
            child:Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                buildButtonColumn(Icons.call, 'CALL'),
                buildButtonColumn(Icons.near_me, 'ROUTE'),
                buildButtonColumn(Icons.share, 'SHARE'),
              ],
            ),
          ),
        ),
      );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-21
      • 1970-01-01
      • 2017-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多