【问题标题】:Curious why my onPressed function is not working in debug mode flutter好奇为什么我的 onPressed 函数在调试模式下无法正常工作
【发布时间】:2020-12-04 06:16:43
【问题描述】:

我正在尝试在 onPressed 方法中实现简单的底部表单

这是我的代码

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

class LoginScreen extends StatelessWidget {
  void _onButtonPressed(BuildContext context) {
    showModalBottomSheet(
        context: context,
        builder: (context) => new Column(
              children: [
                new Container(
                  child: Text('Hello'),
                )
              ],
            ));
  }

  @override
  Widget build(BuildContext context) => new Scaffold(
        body: new RaisedButton(
          onPressed: () => _onButtonPressed(context),
          child: Text('Hello'),
        ),
      );
}

因此,每当我将 flutterMode 设置为启动文件调试模式时,它都会在我单击按钮时显示异常

'package:flutter/src/widgets/navigator.dart': Failed assertion: line 3524 pos 12: '!_debugLocked': is not true.

但是当我在配置文件或发布模式下构建我的应用程序时,底部工作表就像一个魅力。 这有什么具体原因吗?

因为我有另一个页面也实现了底部工作表,但它适用于任何模式。

刚刚在 android studio 上构建了这个应用

════════ Exception caught by gesture ═══════════════════════════════════════════════════════════════
The following assertion was thrown while handling a gesture:
'package:flutter/src/widgets/navigator.dart': Failed assertion: line 3524 pos 12: '!_debugLocked': is not true.


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=BUG.md

When the exception was thrown, this was the stack: 
#2      NavigatorState.push (package:flutter/src/widgets/navigator.dart:3524:12)
#3      showModalBottomSheet (package:flutter/src/material/bottom_sheet.dart:667:65)
#4      _LoginScreen._onButtonPressed (package:mahayoga_admin/loginScreen.dart:10:5)
#5      _LoginScreen.build.<anonymous closure> (package:mahayoga_admin/loginScreen.dart:29:53)
#6      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:993:19)
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#095ab
  debugOwner: GestureDetector
  state: possible
  won arena
  finalPosition: Offset(18.8, 44.1)
  finalLocalPosition: Offset(44.0, 18.0)
  button: 1
  sent tap down
════════════════════════════════════════════════════════════════════════════════════════════════════

所以我把我的代码改成了这样

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mahayoga_admin/bottomSheetDrawer.dart';

class LoginScreen extends StatefulWidget {
  State<StatefulWidget> createState() => _LoginScreen();
}

class _LoginScreen extends State<LoginScreen> {
  void _onButtonPressed(BuildContext context) {
    showModalBottomSheet(
        context: context,
        builder: (context) => SingleChildScrollView(
          child: Container(
            padding: EdgeInsets.only(
              bottom: MediaQuery.of(context).viewInsets.bottom,
            ),
            child: new Container(
              child: Text("Hello world"),
            ),
          ),
        ));
  }

  @override
  Widget build(BuildContext context) => new Scaffold(
      body: new Container(
          height: MediaQuery.of(context).size.height,
          child: SingleChildScrollView(
            child: new Container(
              child: Column(
                children: [
                  new RaisedButton(onPressed: () => _onButtonPressed(context))
                ],
              ),
            ),
          )));
}

【问题讨论】:

  • 尝试使用 StatefullWidget 而不是 StatelessWidget
  • StatefulWidget 也没有同样的错误

标签: flutter flutter-layout


【解决方案1】:

要解决这个问题,只需将其包装在 Container 中并将高度设置为屏幕最大高度,如下所示:

@override
  Widget build(BuildContext context) {
    return Container(
      height: MediaQuery.of(context).size.height,
      child: SingleChildScrollView(
        child: ... //You can then have a column warped within a container(height specified)....after that under column use the raised button
      ),
    ),
  }

【讨论】:

  • 您是否完全使用了我提供的 statefull 小部件?
  • 尝试像文本或图标一样将子元素添加到凸起的按钮,而不是 => 尝试使用 {} 进行包装,是的,请参考模态表文档。
【解决方案2】:

好吧,抱歉打扰大家了,但就我手动调试而言,这是我通过屏幕导航我的应用程序的方式。实际上发生了什么,我通过检查用户是否在先前状态的 onInit 函数中经过身份验证,跳转到问题陈述中提到的上述屏幕。在我提到的先前状态中,我实现了底部表。 S 执行失败,突然我跳到另一个屏幕,这就是为什么在上面的屏幕中我无法获得我的底页。所以我所要做的就是正确地改变我的导航。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多