不确定您是否找到了此问题的答案,但我将在此处发布适合我的解决方案。基本上,BottomNavigationBar 有一个您需要使用的 key 属性。一旦 Flutter Driver 识别出这个键,你就可以告诉驱动程序点击它的任何子项,即BottomNavigationBarItem。
我的屏幕有 2 个bottomNavigationBarItems,如下所示,我为它们的父小部件定义了键,即BottomNavigationBar:
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.shifting,
key: Key('bottom'),
items: [
BottomNavigationBarItem(
icon: Icon(Icons.ac_unit, color: Colors.green,),
title: Text('First', style: TextStyle(color: Colors.black),)
),
BottomNavigationBarItem(
icon: Icon(Icons.cast, color: Colors.yellow,),
title: Text('Second', style: TextStyle(color: Colors.black),)
)
],
),
我编写了一个颤振驱动程序测试来挖掘这两个项目,它们都运行良好。
test('bottomnavigationbar test', () async {
await driver.waitFor(find.byValueKey('bottom'));
await driver.tap(find.text('First'));
print('clicked on first');
await driver.tap(find.text('Second'));
print('clicked on second too');
});
结果: