import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:patbond_flutter/core/theme/app_theme.dart'; import 'package:patbond_flutter/core/widgets/primary_button.dart'; Widget wrap(Widget child) { return MaterialApp( theme: buildAppTheme(), home: Scaffold(body: Center(child: child)), ); } void main() { testWidgets('默认态显示文字,点击触发回调', (tester) async { var tapped = 0; await tester.pumpWidget( wrap(PrimaryButton(label: '登录', onPressed: () => tapped++)), ); expect(find.text('登录'), findsOneWidget); await tester.tap(find.byType(PrimaryButton)); expect(tapped, 1); }); testWidgets('loading 态显示转圈、隐藏文字且不可点击', (tester) async { var tapped = 0; await tester.pumpWidget( wrap( PrimaryButton(label: '登录', isLoading: true, onPressed: () => tapped++), ), ); expect(find.byType(CircularProgressIndicator), findsOneWidget); expect(find.text('登录'), findsNothing); await tester.tap(find.byType(PrimaryButton), warnIfMissed: false); await tester.pump(); expect(tapped, 0); // loading 不是禁用态:填充保持 primaryStrong,尺寸保持 52 高。 final button = tester.widget(find.byType(FilledButton)); expect(button.onPressed, isNull); expect( button.style?.backgroundColor?.resolve({WidgetState.disabled}), AppColors.primaryStrong, ); expect(tester.getSize(find.byType(PrimaryButton)).height, 52); }); testWidgets('onPressed 为 null 时进入禁用态', (tester) async { await tester.pumpWidget(wrap(const PrimaryButton(label: '登录'))); final button = tester.widget(find.byType(FilledButton)); expect(button.onPressed, isNull); expect(find.text('登录'), findsOneWidget); expect(find.byType(CircularProgressIndicator), findsNothing); }); }