gpt4 book ai didi

Flutter:如何在测试中访问/检查小部件的属性?

转载 作者:行者123 更新时间:2023-12-04 13:16:09 29 4
gpt4 key购买 nike

我想测试一些小部件的属性,但我没有找到一种简单的方法。

这是一个带有密码字段的简单示例,我如何检查 blurText 是否设置为 true ?


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

const darkBlue = Color.fromARGB(255, 18, 32, 47);

Future<void> main() async {
testWidgets('Wheelio logo appear on the login screen',
(WidgetTester tester) async {
final Key _formKey = GlobalKey<FormState>();
final TextEditingController passwordController = TextEditingController();
const Key _passwordKey = Key('PASSWORD_KEY');
final Finder passwordField = find.byKey(_passwordKey);
await tester.pumpWidget(MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Form(
key: _formKey,
child: TextFormField(
key: _passwordKey,
obscureText: true,
controller: passwordController,
),
),
),
),
));

await tester.pump();
expect(passwordField, findsOneWidget);
final TextFormField myPasswordWidget =
tester.widget(passwordField) as TextFormField;

// How can I check that obscureText property is set to true ?
});
}

最佳答案

您可以使用 testerfind获取小部件树中的任何内容。

例如,如果你想测试一个 Text有房产textAlign设置为 TextAlign.center , 你可以做:

expect(
tester.widget(find.byType(Text)),
isA<Text>().having((t) => t.textAlign, 'textAlign', TextAlign.center),
);

关于Flutter:如何在测试中访问/检查小部件的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60336656/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com