I am trying to tap an item of ListView on integration test. The app is basically bookmark app that you can register and delete bookmarks. Also, it has defalut value which is 'Apple' and 'Google' and I would like to tap these default value on integration test. How can I do it? Please help me. Here is the code.
late Box box;
const bookmarkBox = 'bookmark_box';
Future<void> main() async {
await Hive.initFlutter();
box = await Hive.openBox(bookmarkBox);
runApp(const BookmarkApp());
}
class BookmarkApp extends StatelessWidget {
const BookmarkApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Bookmark App',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.white),
useMaterial3: false,
),
home: BookmarkListPage(text: '', url: '',),
);
}
}
class BookmarkListPage extends StatefulWidget {
BookmarkListPage({super.key, title, required this.text, required this.url});
String text;
String url;
final String title = 'Bookmark';
@override
State<BookmarkListPage> createState() => _BookmarkListPageState();
}
class _BookmarkListPageState extends State<BookmarkListPage> {
// Google, Apple are default value
// I would like to tap these items on integration test
List<dynamic> bookmarkList = box.get('BookmarkTitle', defaultValue: ['Google', 'Apple']);
List<dynamic> bookmarkUrl = box.get('BookmarkUrl', defaultValue: [
'https://www.google.com/',
'https://www.apple.com/jp/',
]);
_launchUrl(int index) async {
var url = Uri.parse(bookmarkUrl[index]);
if (await canLaunchUrl(url)) {
await launchUrl(url);
} else {
throw "Could not open $url";
}
}
@override
Widget build(BuildContext context) {
var box = Hive.box(bookmarkBox);
return Scaffold(
appBar: AppBar(
centerTitle: true,
backgroundColor: Colors.white,
title: Text(
widget.title,
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.black),
),
bottom: PreferredSize(
child: Container(
height: 1,
color: Colors.black,
),
preferredSize: Size.fromHeight(1),
),
),
body: ListView.builder(
key: Key('listview_bookmark'),
itemCount: bookmarkList.length,
itemBuilder: (context, index) {
return Dismissible(
background: Container(
color: Colors.red,
child: Icon(
Icons.delete,
color: Colors.white,
),
),
onDismissed: (direction) {
setState(() {
bookmarkList.removeAt(index);
bookmarkUrl.removeAt(index);
box.put('BookmarkTitle', bookmarkList);
box.put('BookmarkUrl', bookmarkUrl);
});
},
key: UniqueKey(),
child: Card(
child: ListTile(
title: Text(
bookmarkList[index],
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
subtitle: Text(bookmarkUrl[index]),
onTap:() => _launchUrl(index),
),
),
);
},
),
floatingActionButton: FloatingActionButton(
shape: const CircleBorder(),
backgroundColor: Colors.blue,
onPressed: () async {
final (newBookmarkTitle, newBookmarkUrl) = await showDialog(
context: context,
builder: (_) {
return BookmarkDialog();
});
setState(() {
bookmarkList.add(newBookmarkTitle);
bookmarkUrl.add(newBookmarkUrl);
box.put('BookmarkTitle', bookmarkList);
box.put('BookmarkUrl', bookmarkUrl);
});
},
child: const Icon(
Icons.add,
color: Colors.white,
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}