
Paso 1: Configura tu proyecto Flutter
- Crea un nuevo proyecto Flutter si aún no tienes uno. Abre una terminal y ejecuta:
flutter create top_menu_example - Navega al directorio del proyecto:
cd top_menu_example
Paso 2: Define la clase TopMenuItem
- Crea una nueva clase
TopMenuItempara representar cada ítem del menú. Crea un archivo llamadotop_menu_item.dartdentro del directorioliby define la clase allí:
import 'package:flutter/material.dart';
class TopMenuItem {
final IconData? icon;
final String value;
final String section;
TopMenuItem({
this.icon,
required this.value,
required this.section,
});
}
Paso 3: Crea el widget TopMenu
- Define el widget
TopMenuque mostrará los ítems del menú en una fila. Crea un archivo llamadotop_menu.darten el directorioliby define el widget allí:
import 'package:flutter/material.dart';
import 'top_menu_item.dart'; // Asegúrate de importar la clase TopMenuItem
class TopMenu extends StatelessWidget {
final List<TopMenuItem> items;
final String selectedSection;
final Function(String) onSectionChanged;
TopMenu({
required this.items,
required this.selectedSection,
required this.onSectionChanged,
});
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: items.map((item) {
final isSelected = item.section == selectedSection;
return GestureDetector(
onTap: () {
onSectionChanged(item.section);
},
child: Column(
children: [
if (item.icon != null)
Icon(
item.icon,
color: isSelected ? Colors.blue : Colors.black,
),
Text(
item.value,
style: TextStyle(
color: isSelected ? Colors.blue : Colors.black,
fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
),
),
],
),
);
}).toList(),
);
}
}
Paso 4: Usa el widget TopMenu en tu aplicación
- Modifica el archivo
main.dartpara usar el widgetTopMenu. Aquí está un ejemplo completo:
import 'package:flutter/material.dart';
import 'top_menu.dart';
import 'top_menu_item.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _currentSection = 'home';
void _onSectionChanged(String newSection) {
setState(() {
_currentSection = newSection;
});
}
@override
Widget build(BuildContext context) {
List<TopMenuItem> menuItems = [
TopMenuItem(icon: Icons.home, value: 'Home', section: 'home'),
TopMenuItem(icon: Icons.search, value: 'Search', section: 'search'),
TopMenuItem(value: 'Settings', section: 'settings'), // Sin ícono
];
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Top Menu Example'),
),
body: Column(
children: [
TopMenu(
items: menuItems,
selectedSection: _currentSection,
onSectionChanged: _onSectionChanged,
),
Expanded(
child: Center(
child: Text(
'Current section: $_currentSection',
style: TextStyle(fontSize: 24),
),
),
),
],
),
),
);
}
}
Explicación
- Clase
TopMenuItem: Define los datos para cada ítem del menú, incluyendo un ícono opcional (IconData?), el texto (value), y una sección identificadora (section). - Widget
TopMenu: Muestra una fila de ítems del menú. El ítem seleccionado se resalta con un color y un estilo de texto diferente. Usa unGestureDetectorpara manejar los toques en cada ítem y llama a la funciónonSectionChangedpara actualizar la sección seleccionada. - Uso del
TopMenu: En el widget principal (MyApp), se define una lista de ítems del menú y se pasa al widgetTopMenu, junto con la sección seleccionada y la función para manejar los cambios de sección.

Ingeniero en Informática, Investigador, me encanta crear cosas o arreglarlas y darles una nueva vida. Escritor y poeta. Más de 20 APPs publicadas y un libro en Amazon.