Flutter-onPressed功能

时间:2018-10-26 00:22:20

标签: 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'),
      ],
    ),
  );

3 个答案:

答案 0 :(得分: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');
          }),
        ],
      ),
    );

答案 1 :(得分: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 :(得分:1)

您可以使用此:

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'),
          ],
        ),
      ),
    ),
  );
}