You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter页面导航无报错但无法跳转问题求助

Flutter 点击无页面跳转问题修复

问题出在InkWell的onTap回调写法上:你当前写的()=> selectCategory只是返回了函数对象,并没有执行这个函数,而且也没传递函数必需的BuildContext参数,导致点击时根本没触发导航逻辑。

修正方案

把onTap的写法改成以下任意一种即可:

写法1(箭头函数直接调用)

onTap: () => selectCategory(context),

写法2(匿名函数包裹执行)

onTap: () {
  selectCategory(context);
},

修正后的完整代码

import 'package:flutter/material.dart';
import 'meals_category_screen.dart';

class CategoryItem extends StatelessWidget {
  final String title;
  final Color color;

  const CategoryItem(this.title, this.color);

  void selectCategory(BuildContext ctx) {
    Navigator.of(ctx).push(MaterialPageRoute(builder: (_) {
      return const MealsCategoryScreen();
    }));
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      // 这里是修正的核心位置
      onTap: () => selectCategory(context),
      splashColor: Theme.of(context).primaryColor,
      borderRadius: BorderRadius.circular(15),
      child: Container(
        padding: const EdgeInsets.all(15),
        child: Text(title, style: Theme.of(context).textTheme.subtitle1),
        decoration: BoxDecoration(
          gradient: LinearGradient(
            colors: [color.withOpacity(.7), color],
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
          ),
          borderRadius: BorderRadius.circular(15),
        ),
      ),
    );
  }
}

内容的提问来源于stack exchange,提问作者Wael Atef

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 10:24:13