Skip to content

Instantly share code, notes, and snippets.

@asartalo
Last active February 29, 2024 02:07
Show Gist options
  • Save asartalo/24e653c5475584a74a9011ad0bc44ca4 to your computer and use it in GitHub Desktop.
Save asartalo/24e653c5475584a74a9011ad0bc44ca4 to your computer and use it in GitHub Desktop.
Flutter list items and menu highlight weird interaction
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class Task {
final String name;
final int id;
const Task(this.id, this.name);
}
final tasks = [
const Task(1, 'First Task'),
const Task(2, 'Second Task'),
const Task(3, 'Third Task'),
const Task(4, 'Fourth Task'),
];
final menuItems = [
'Something Something',
'Edit Task',
'Delete Task',
];
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: ListView.builder(
itemCount: tasks.length,
itemBuilder: (context, index) {
final task = tasks[index];
return Material(
key: Key(task.id.toString()),
type: MaterialType.transparency,
child: ListTile(
title: Text(task.name),
onTap: () async {},
trailing: MenuAnchor(
menuChildren: menuItems
.map(
(item) => MenuItemButton(
child: Text(item),
onPressed: () {},
),
)
.toList(),
builder: (context, controller, child) => IconButton(
icon: const Icon(Icons.more_vert),
onPressed: () {
if (controller.isOpen) {
controller.close();
} else {
controller.open();
}
},
),
),
),
);
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment