首页 > 解决方案 > 没有为类型“TapScreen”定义方法“Tap”

问题描述

我是这个世界的新手,还在学习,这对你来说可能很容易,但它不适合我:(我正在对一个测试应用程序进行一些编码,但我卡在了 TapBar 小部件上,消息是:方法'没有为类型“TapScreen”定义 Tap。尝试将名称更正为现有方法的名称,或定义名为“Tap”的方法。dartundefined_method

class TapScreen extends StatelessWidget {
  const TapScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 2,
      child: Scaffold(
        appBar: AppBar(
          bottom: TabBar(
            tabs: <Widget>[
              Tap (                             <===// The problem is here on (Tap).
                icon: Icon(Icons.dashboard),
                text: 'Skills',
              ),
              Tap(                             <===// The problem is here on (Tap).
                icon: Icon(Icons.star),
                text: 'Favorites',
              ),
            ],
          ),
        ),
        body: TabBarView(
          children: [
            CategoriesScreen(),
            FavoritesScreen(),
          ],
        ),
      ),
    );
  }
}

标签: flutterwidget

解决方案


问题是没有在您的文件中导入,或者小部件不存在,

如果您需要识别点击手势的小部件,请尝试 InkWell 或 GestureDetector 小部件

 GestureDetector(
      onTap: () {
        FocusScopeNode currentFocus = FocusScope.of(context);
        if (!currentFocus.hasPrimaryFocus) {
          currentFocus.unfocus();
        }
      },
      child: YourCustomWidget()
  )

同样的代码应该适用于 InkWell


推荐阅读