Flutter界面元素查找插件widget_finder的使用

Flutter界面元素查找插件widget_finder的使用

Easily find position and size of widget. Additionally, available to receive the size through callback function, using WidgetFinder.sizeNotifier widget.

开始使用

在你的 pubspec.yaml 文件中添加 widget_finder

dependencies:
  widget_finder:

示例

以下是一个完整的示例,展示了如何使用 widget_finder 插件来查找和获取控件的位置和大小。

import 'dart:math';

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final key = GlobalKey();

  Offset? position;
  Size? size;

  double _width = 200;
  double _height = 200;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Widget_Finder Example'),
        ),
        body: Container(
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                // 使用 WidgetFinder.sizeNotifer 来接收控件的大小变化
                WidgetFinder.sizeNotifer(
                  key: key,
                  onSizeChanged: (Size value) {
                    print('Size is changed! : $value');
                  },
                  child: Container(
                    color: Colors.red,
                    width: _width,
                    height: _height,
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: MaterialButton(
                        color: Colors.blue,
                        onPressed: () {
                          Random _rand = Random();
                          setState(() {
                            _width = (_rand.nextBool() ? 1 : -1) * _rand.nextInt(100) + 200;
                            _height = (_rand.nextBool() ? 1 : -1) * _rand.nextInt(100) + 200;
                          });
                        },
                        child: Text('Change size!'),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: MaterialButton(
                        color: Colors.blue,
                        onPressed: () {
                          setState(() {
                            position = WidgetFinder.of(key).topLeft;
                            size = WidgetFinder.of(key).size;
                          });
                        },
                        child: Text('Find!'),
                      ),
                    ),
                  ],
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text('position of topLeft: $position'),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text('size: $size'),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

说明

  1. 创建全局键

    final key = GlobalKey();
    
  2. 创建包含全局键的控件

    WidgetFinder.sizeNotifer(
      key: key,
      onSizeChanged: (Size value) {
        print('Size is changed! : $value');
      },
      child: Container(
        color: Colors.red,
        width: _width,
        height: _height,
      ),
    )
    
  3. 查找控件位置和大小

    position = WidgetFinder.of(key).topLeft;
    size = WidgetFinder.of(key).size;
    

更多关于Flutter界面元素查找插件widget_finder的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter界面元素查找插件widget_finder的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用widget_finder插件来进行界面元素查找的一个基本示例。需要注意的是,widget_finder并不是Flutter官方或广泛认知的插件名称,通常我们使用的是flutter_test包来进行UI测试,其中包含了用于查找和交互界面元素的API。如果你提到的widget_finder是一个自定义或第三方插件,其API可能会有所不同,但大多数原理是相似的。这里,我将展示如何使用flutter_test包中的功能,因为这是在Flutter中进行UI测试的标准方式。

首先,确保你的pubspec.yaml文件中包含了flutter_test依赖(这通常是默认包含的):

dev_dependencies:
  flutter_test:
    sdk: flutter

然后,你可以编写一个测试文件,例如test/widget_test.dart,来展示如何查找和验证界面元素。

import 'package:flutter_test/flutter_test.dart';
import 'package:your_app/main.dart'; // 导入你的主应用文件

void main() {
  testWidgets('finds text in appbar', (WidgetTester tester) async {
    // 构建应用
    await tester.pumpWidget(MyApp());

    // 查找AppBar中的文本
    final appBarTextFinder = find.text('Your App Title'); // 替换为你的AppBar标题
    expect(appBarTextFinder, findsOneWidget); // 验证是否找到一个匹配的widget

    // 你还可以查找其他类型的widget,例如按钮、图标等
    final floatingActionButtonFinder = find.byIcon(Icons.add); // 查找带有特定图标的FloatingActionButton
    expect(floatingActionButtonFinder, findsOneWidget);

    // 如果你想与找到的widget交互,例如点击一个按钮
    await tester.tap(floatingActionButtonFinder);
    await tester.pumpAndSettle(); // 等待动画和状态更新完成

    // 验证点击后的状态变化,例如检查是否导航到了新页面或显示了对话框等
    // 例如,检查是否显示了Snackbar
    final snackbarFinder = find.byType(SnackBar);
    expect(snackbarFinder, findsOneWidget);
  });
}

在这个示例中:

  1. 使用testWidgets函数定义了一个测试。
  2. 使用WidgetTesterpumpWidget方法构建了应用。
  3. 使用find.textfind.byIcon等方法查找界面元素。
  4. 使用expect函数验证是否找到了预期的widget。
  5. 使用tester.tap方法与找到的widget进行交互。
  6. 使用tester.pumpAndSettle等待应用状态更新。
  7. 再次使用findexpect验证交互后的状态变化。

如果你确实有一个名为widget_finder的特定插件,并且它的用法与flutter_test不同,请查阅该插件的文档以获取具体的API和用法示例。不过,大多数UI测试插件都会提供类似的功能集,包括查找widget、验证其存在、与它们交互以及验证交互后的应用状态。

回到顶部