简体   繁体   English

Flutter / Dart:在小部件测试中点击 AppBar 小部件时不会调用 onPressed

[英]Flutter / Dart: onPressed not called when tapping an AppBar widget in Widget Test

my problem is that I have a stateless widget that returns a AppBar with an onPressed event.我的问题是我有一个无状态小部件,它返回一个带有 onPressed 事件的AppBar In my corresponding widgetTest, I tap the component and expect that the onPressed method is called.在我对应的 widgetTest 中,我点击组件并期望 onPressed 方法被调用。 However, it is not.然而,事实并非如此。

This is my widget:这是我的小部件:

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

class IconAppBar extends StatelessWidget with PreferredSizeWidget {
  final VoidCallback onPressed;

  IconAppBar({this.onPressed});

  @override
  Widget build(BuildContext context) {
    return AppBar(
      leading: IconButton(
        color: Colors.black,
        icon: SvgPicture.asset(
          "resources/images/icon.svg",
          width: 24,
          height: 24,
        ),
        onPressed: onPressed,
      ),
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(kToolbarHeight);
}

In the test, I create the widget with a simple test for the onPressed if the component was tapped.在测试中,我创建了一个小部件,如果组件被点击,则对 onPressed 进行一个简单的测试。

void main() {
    testWidgets("Should run onPressed when tapped",
        (WidgetTester tester) async {
      var counter = 0;
      void callback() {
        counter++;
      }

      await tester.pumpWidget(
        MediaQuery(
          data: MediaQueryData(),
          child: MaterialApp(
            localizationsDelegates: [
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],
            home: IconAppBar(
              onPressed: callback,
            ),
          ),
        ),
      );

      await tester.pump(Duration(milliseconds: 300));
      await tester.tap(find.byType(IconAppBar));
      await tester.pump(Duration(milliseconds: 300));

      expect(counter, 1); // fails because actual counter is 0
    });
}

Changing to tester.pump() or tester.pumpAndSettle() did not change anything.更改为tester.pump()tester.pumpAndSettle()没有任何改变。 The method is not called.不调用该方法。

Running this on an actual device or in an emulator, the onPressed works as expected.在实际设备或模拟器上运行它,onPressed 会按预期工作。

Also a smoke test with find.byType(IconAppBar) does find the widget.还有一个使用find.byType(IconAppBar)的冒烟测试确实找到了小部件。

I would really appreciate any help or ideas.我真的很感激任何帮助或想法。 Thanks!谢谢!

EDIT: It seems this just doesn't work with IconButton .编辑:这似乎不适用于IconButton I just changed it to a FlatButton with the SVG as its child and it was fine.我只是将其更改为以 SVG 作为其子项的FlatButton ,这很好。

Don't know if this helps:不知道这是否有帮助:

class IconAppBar extends StatelessWidget with PreferredSizeWidget {
  final VoidCallback onPressed;

  BackButtonAppBar({this.onPressed});

  void click() {
    print("Its Working!");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
      leading: IconButton(
        color: Colors.black,
        icon: SvgPicture.asset(
          "resources/images/icon.svg",
          width: 24,
          height: 24,
        ),
        onPressed: () {
          click();
        },
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM