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

Flutter Android设备MaterialButton点击异常问题求助

Flutter Android端MaterialButton点击异常问题

问题现象

  • Chrome浏览器运行Flutter应用完全正常,Android设备上点击MaterialButton出现Bug
  • 预期效果:点击按钮先执行订单总额折扣计算(或选中特定商品时总额加5000),再跳转页面
  • 实际情况:
    • 计算函数写在Navigator.of(context).push之前:点击按钮无任何反应,无法跳转目标页面
    • 计算函数写在Navigator跳转之后:计算函数完全不执行
  • 已尝试调整函数在onPressed回调中的写法、更换多种按钮组件,结果一致

可复现代码示例

main.dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'minimal_cart_bill.dart';
import 'minimal_cart_controller.dart';

void main() {
runApp(const MyApp());
}

//Stateless du MaterialApp
// ignore: must_be_immutable
class MyApp extends StatelessWidget {
// final bool showHome;
const MyApp({Key? key}) : super(key: key);

static const String title = "Eclat d'Afrik";
// final userr = UserPreferences.myUser;

@override
Widget build(BuildContext context) {
return MaterialApp(
title: title,
home: MinimalReproductible(),
// home: showHome ? const AuthPage() : OnBrodingPage(),
);
}
}

class MinimalReproductible extends StatelessWidget {
MinimalReproductible({super.key});

final minimalController = Get.find<MinimalCartController>();

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Bill Generator')),
body: MaterialButton(
onPressed: () {
minimalController.realIfSuExpress();
Navigator.of(context).push(
MaterialPageRoute(builder: (builder) => const MinimalCartBill()));
},
color: const Color(0xFF5ACC80),
height: 55.0,
child: const Text(
"GENERATE MY BILL",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14.0,
color: Colors.black,
),
),
),
);
}
}

minimal_cart_controller.dart

// ignore: depend_on_referenced_packages
import 'package:get/get.dart';

class MinimalCartController extends GetxController {
var realTotal = 0.0;
var globalsom = 3000.0;
var ifCinqMilles = 5000.0;
var totalOfArticles = 2.0;
var theRemiseTwenty = 0.0;
var theRemiseTen = 0.0;

void realIfSuExpress() {
realTotal = globalsom + ifCinqMilles;
if (totalOfArticles > 20) {
theRemiseTwenty = (globalsom * 20) / 100;
}
if (totalOfArticles > 9 && totalOfArticles < 20) {
theRemiseTen = (globalsom * 15) / 100;
}
}
}

minimal_cart_bill.dart

import 'package:flutter/material.dart';

class MinimalCartBill extends StatelessWidget {
const MinimalCartBill({super.key});

@override 
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Welcome Page')),
body: const Padding(
padding: EdgeInsets.all(.0),
child: Text (
"Welcome !",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14.0,
color: Colors.black,
),
),
),
);
}
}

环境与日志信息

  • Flutter run --verbose:无异常关键输出
  • Flutter Analyse:项目分析未发现问题(耗时548.3秒)
  • Flutter Doctor:Flutter稳定版3.3.3,Android SDK 33.0.0,所有环境配置正常

解决方案

1. 初始化GetX控制器

问题核心是Android端未找到MinimalCartController实例,导致调用realIfSuExpress()时抛出异常,被框架静默捕获后中断后续逻辑。Chrome调试环境存在 fallback 机制,所以运行正常。

修复方式:在main()函数中提前初始化控制器:

void main() {
  Get.put(MinimalCartController()); // 初始化控制器
  runApp(const MyApp());
}

或在MinimalReproductible组件中使用Get.put替代Get.find:

final minimalController = Get.put(MinimalCartController());

2. 响应式变量改造(若需在新页面使用计算结果)

如果后续需要在MinimalCartBill页面展示计算后的总额,需将控制器中的变量改为Rx响应式类型,确保数据变化能被感知:

class MinimalCartController extends GetxController {
  var realTotal = 0.0.obs;
  var globalsom = 3000.0.obs;
  var ifCinqMilles = 5000.0.obs;
  var totalOfArticles = 2.0.obs;
  var theRemiseTwenty = 0.0.obs;
  var theRemiseTen = 0.0.obs;

  void realIfSuExpress() {
    realTotal.value = globalsom.value + ifCinqMilles.value;
    if (totalOfArticles.value > 20) {
      theRemiseTwenty.value = (globalsom.value * 20) / 100;
    }
    if (totalOfArticles.value > 9 && totalOfArticles.value < 20) {
      theRemiseTen.value = (globalsom.value * 15) / 100;
    }
  }
}

3. 确保跳转逻辑执行(可选)

若控制器初始化后仍存在跳转延迟问题,可通过异步延迟确保计算完成后再执行跳转:

onPressed: () async {
  minimalController.realIfSuExpress();
  await Future.delayed(const Duration(milliseconds: 50)); // 短暂延迟确保计算完成
  Navigator.of(context).push(
    MaterialPageRoute(builder: (builder) => const MinimalCartBill()));
},

内容的提问来源于stack exchange,提问作者Djèlì Bi Berci

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 15:55:20