简体   繁体   English

提升按钮颜色的颤振小部件测试

[英]Flutter widget test of elevated button color

Widget test for elevated button background color test fails, My Build method of Button提升按钮背景颜色测试的小部件测试失败,Button 的 My Build 方法

    @override
  Widget build(BuildContext context) {
    return Container(
      margin: mMargin,
      child: ElevatedButton(
        key: widgetKey,
        style: ButtonStyle(
          shape: MaterialStateProperty.all<RoundedRectangleBorder>(
            RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(20.0),
            ),
          ),
          minimumSize: MaterialStateProperty.all(Size(double.infinity, 40)),
          backgroundColor: onClick == null
              ? MaterialStateProperty.all(AppColors.button_disabled)
              : MaterialStateProperty.all(AppColors.pink_accent),
          shadowColor: MaterialStateProperty.all(Colors.transparent),
        ),
        onPressed: () => onClick(context),
        child: Padding(
          padding: const EdgeInsets.only(
            top: 10,
            bottom: 10,
          ),
          child: Container(
            child: isLoading == false
                ? Text(
                    btnName,
                    style: Theme.of(context)
                        .textTheme
                        .button
                        .copyWith(color: AppColors.white),
                  )
                : SizedBox(
                    height: 25,
                    width: 25,
                    child: CircularProgressIndicator(
                      color: Theme.of(context).indicatorColor,
                    ),
                  ),
          ),
        ),
      ),
    );
  }

Test class code测试类代码

void main() {
  testWidgets('Login Screen Widget Test', (WidgetTester tester) async {
    await tester.pumpWidget(loginScreenWidget());
    await tester.pumpAndSettle(Duration(seconds: 2));
    expect(find.text('MyMedia'), findsOneWidget);
    expect(find.text('MyApp'), findsOneWidget);
    expect(find.byType(TextField), findsNWidgets(2));

    await tester.pumpAndSettle(Duration(seconds: 1));
    expect(find.byType(ElevatedButton), findsOneWidget);

    expect(
        ((tester.firstWidget(find.byType(ElevatedButton)) as ElevatedButton)
                .style)
            .backgroundColor,
        MaterialStateProperty.all(AppColors.button_disabled));

    expect(find.text('username'), findsOneWidget);
    var usernameField = find.byKey(Key('login_username'));
    await tester.enterText(usernameField, 'test name');
    expect(find.text('test name'), findsOneWidget);

    var passwordField = find.byKey(Key('login_password'));
    await tester.enterText(passwordField, 'password');
    expect(find.text('password'), findsOneWidget);

    expect(find.text('Login'), findsOneWidget);
  });
}

at this line在这条线上

expect(
            ((tester.firstWidget(find.byType(ElevatedButton)) as ElevatedButton)
                    .style)
                .backgroundColor,
            MaterialStateProperty.all(AppColors.button_disabled));

i get error我得到错误

The following TestFailure object was thrown running a test:运行测试时抛出了以下 TestFailure 对象:

I/flutter (123):   Expected: _MaterialStatePropertyAll<Color>:<MaterialStateProperty.all(Color(0xff6e2443))>

I/flutter (123):   Actual: _MaterialStatePropertyAll<Color>:<MaterialStateProperty.all(Color(0xff6e2443))>

See my example, I think you can to adapt it:看我的例子,我认为你可以适应它:

final finder = find.widgetWithText(ElevatedButton, 'Problems');
final widget = tester.firstWidget(finder) as ElevatedButton;
final states = <MaterialState>{};
final bgColor = widget.style?.backgroundColor?.resolve(states);
expect(bgColor, const Color(0xFFc0c0c0));

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

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