Flutter Route 没有响应 onClick [英] Flutter Route Not Responding onClick

查看:60
本文介绍了Flutter Route 没有响应 onClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个卡片组件,其中包含图标、文本和pressn 作为卡片组件内部的参数.pressn 代表路线.除了代表路线小部件的pressn小部件外,所有其他小部件都在工作.

I have created a card component that contains icons, text and pressn as parameters inside card component. The pressn represents route. All other widgets are working except pressn widget which represents the route widget.

我是否想为pressn分配任何unClick或tab功能?

Am I suppose to assign any unClick or tab function to pressn?

下面是代码

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:api_example_app/constants.dart';
import 'package:flutter/widgets.dart';

class CardsParent extends StatefulWidget {
  const CardsParent({
    Key key,
    @required this.size,
    this.icon,
    this.title,
    this.subtitle,
    this.pressn,
  }) : super(key: key);

  final Size size;
  final IconData icon;
  final String title;
  final String subtitle;
  final GestureTapCallback pressn;

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

class _CardsParentState extends State<CardsParent> {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 140,
      height: 100,
      child: Card(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(15.0),
        ),
        color: Colors.white,
        elevation: 10,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            ListTile(
              leading: Icon(
                widget.icon,
                size: 50,
                color: kOrangeColor,
              ),
              title: Text(widget.title,
                  style: TextStyle(fontSize: 14.0, color: kOrangeColor)),
              subtitle: Text(widget.subtitle,
                  style: TextStyle(fontSize: 11.0, color: kOrangeColor)),
            ),
          ],
        ),
      ),
    );
  }
}

下面是我想使用代码的地方,我通过pressn来代表导航器.

Below is where I want to make use of the code, where I passed pressn to represent the Navigator.

 Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                CardsParent(
                  size: size,
                  icon: FontAwesomeIcons.temperatureHigh,
                  title: 'Tem',
                  subtitle: '33C',
                  pressn: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => TemSensorScreen(),
                      ),
                    );
                  },
                ),
                CardsParent(
                  size: size,
                  title: 'Hum ',
                  subtitle: '75%',
                  icon: FontAwesomeIcons.cloudShowersHeavy,
                 pressn: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => HumSensorScreen(),
                      ),
                    );
                  },
                ),
              ],
            ),

我做错了什么?

推荐答案

您的代码中有几个问题:

There are several issues in your code:

  • 在当前场景中,当您调用 onTap 时,它将始终返回 null 值,并且您的 GestureDetector 不会响应触摸.所以删除以下代码:
get onTap => null; // Remove this

  • 您需要将 widget.onTap 传递给您的手势检测器(不是 onTap):
  • @override
    Widget build(BuildContext context) {
       return GestureDetector(
          onTap: widget.onTap, // Change is here
          child: Container(
              // Remaining Code
          ),
       );
    }
    
    

    这篇关于Flutter Route 没有响应 onClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆