Flutter - onPressed 函数

标签 flutter

我需要在下面的图标上插入 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'),
      ],
    ),
  );

最佳答案

为此,您可以使用 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');
          }),
        ],
      ),
    );

关于Flutter - onPressed 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52999790/

相关文章:

flutter - 我是否应该将所有方法参数标记为最终并指定类型

Flutter 扩展方法不起作用,它说 "undefined class"和 "requires the extension-methods language feature"

macos - 如何在 Mac 中将 Flutter 永久添加到路径

firebase - flutter :(24658):PlatformException(network_error,com.google.android.gms.common.api.ApiException:7:,null,null)

dart - 小部件不会在屏幕底部对齐

asp.net-core - Flutter 的 SignalR 集线器连接错误

flutter - 带有 Flutter 的 Firestore

flutter - 如何为 Flutter 中的 Snack Bar 编写一个简单的测试?

java - 我如何最好地使用 flutter MethodChannel.invokeMethod 的 java 版本提供多个参数?

macos - 如何在 Mac OS 上从 Docker Container 访问 USB 端口