Temas y estilos de Flutter
En esta sección se presentará la configuración de temas en Flutter, incluyendo temas de Material Design, temas personalizados y modo oscuro.
ThemeData - Aplicar el tema
Usando ThemeData se puede configurar de manera unificada el color del tema, la fuente, los estilos, etc. de la aplicación.
Ejemplo: Configurar el tema de la aplicación
void main() {
runApp(
MaterialApp(
theme: ThemeData(
// Color principal
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue,
brightness: Brightness.light,
),
// Usar Material 3
useMaterial3: true,
// Fuente
fontFamily: 'Roboto',
// Tema de la barra de aplicación
appBarTheme: const AppBarTheme(
centerTitle: true,
elevation: 0,
),
// Tema de botones
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
),
),
// Tema del campo de entrada
inputDecorationTheme: InputDecorationTheme(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
),
filled: true,
),
),
home: const MyHomePage(),
),
);
}
runApp(
MaterialApp(
theme: ThemeData(
// Color principal
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue,
brightness: Brightness.light,
),
// Usar Material 3
useMaterial3: true,
// Fuente
fontFamily: 'Roboto',
// Tema de la barra de aplicación
appBarTheme: const AppBarTheme(
centerTitle: true,
elevation: 0,
),
// Tema de botones
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
),
),
// Tema del campo de entrada
inputDecorationTheme: InputDecorationTheme(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
),
filled: true,
),
),
home: const MyHomePage(),
),
);
}
Theme.of - Uso del tema
En el Widget, usa Theme.of(context) para obtener el tema actual.
Ejemplo: Usar colores del tema
class ThemedButton extends StatelessWidget {
const ThemedButton({super.key});
@override
Widget build(BuildContext context) {
// Obtener el tema
final theme = Theme.of(context);
return Column(
children: [
// Usar el color del tema
Container(
color: theme.colorScheme.primary,
padding: const EdgeInsets.all(16),
child: Text(
'Color principal',
style: TextStyle(color: theme.colorScheme.onPrimary),
),
),
const SizedBox(height: 16),
// Usar el color secundario
Container(
color: theme.colorScheme.secondary,
padding: const EdgeInsets.all(16),
child: Text(
'Color secundario',
style: TextStyle(color: theme.colorScheme.onSecondary),
),
),
const SizedBox(height: 16),
// Usar el color de error
Container(
color: theme.colorScheme.error,
padding: const EdgeInsets.all(16),
child: Text(
'Color de error',
style: TextStyle(color: theme.colorScheme.onError),
),
),
],
);
}
}
const ThemedButton({super.key});
@override
Widget build(BuildContext context) {
// Obtener el tema
final theme = Theme.of(context);
return Column(
children: [
// Usar el color del tema
Container(
color: theme.colorScheme.primary,
padding: const EdgeInsets.all(16),
child: Text(
'Color principal',
style: TextStyle(color: theme.colorScheme.onPrimary),
),
),
const SizedBox(height: 16),
// Usar el color secundario
Container(
color: theme.colorScheme.secondary,
padding: const EdgeInsets.all(16),
child: Text(
'Color secundario',
style: TextStyle(color: theme.colorScheme.onSecondary),
),
),
const SizedBox(height: 16),
// Usar el color de error
Container(
color: theme.colorScheme.error,
padding: const EdgeInsets.all(16),
child: Text(
'Color de error',
style: TextStyle(color: theme.colorScheme.onError),
),
),
],
);
}
}
Modo oscuro
Ejemplo: Soporte de modo oscuro
void main() {
runApp(
MaterialApp(
theme: ThemeData(
// Tema claro
brightness: Brightness.light,
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
),
darkTheme: ThemeData(
// Tema oscuro
brightness: Brightness.dark,
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue,
brightness: Brightness.dark,
),
),
// Habilitar ambos (opcional)
themeMode: ThemeMode.system, // Según la configuración del sistema
// themeMode: ThemeMode.light // Forzar modo claro
// themeMode: ThemeMode.dark // Forzar modo oscuro
home: const ThemeTogglePage(),
),
);
}
// Cambiar tema
class ThemeTogglePage extends StatelessWidget {
const ThemeTogglePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Cambiar tema')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Tema actual'),
const SizedBox(height: 20),
// Botón para cambiar tema
ElevatedButton(
onPressed: () {
final currentMode = Theme.of(context).brightness;
if (currentMode == Brightness.light) {
// Cambiar a oscuro
Theme.of(context).setThemeMode(ThemeMode.dark);
} else {
// Cambiar a claro
Theme.of(context).setThemeMode(ThemeMode.light);
}
},
child: const Text('Cambiar tema'),
),
],
),
),
);
}
}
runApp(
MaterialApp(
theme: ThemeData(
// Tema claro
brightness: Brightness.light,
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
),
darkTheme: ThemeData(
// Tema oscuro
brightness: Brightness.dark,
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue,
brightness: Brightness.dark,
),
),
// Habilitar ambos (opcional)
themeMode: ThemeMode.system, // Según la configuración del sistema
// themeMode: ThemeMode.light // Forzar modo claro
// themeMode: ThemeMode.dark // Forzar modo oscuro
home: const ThemeTogglePage(),
),
);
}
// Cambiar tema
class ThemeTogglePage extends StatelessWidget {
const ThemeTogglePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Cambiar tema')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Tema actual'),
const SizedBox(height: 20),
// Botón para cambiar tema
ElevatedButton(
onPressed: () {
final currentMode = Theme.of(context).brightness;
if (currentMode == Brightness.light) {
// Cambiar a oscuro
Theme.of(context).setThemeMode(ThemeMode.dark);
} else {
// Cambiar a claro
Theme.of(context).setThemeMode(ThemeMode.light);
}
},
child: const Text('Cambiar tema'),
),
],
),
),
);
}
}
Extensiones de estilos personalizados
Ejemplo: TextTheme personalizado
ThemeData(
textTheme: const TextTheme(
// Texto de exhibición (título grande)
displayLarge: TextStyle(fontSize: 57, fontWeight: FontWeight.w400),
displayMedium: TextStyle(fontSize: 45, fontWeight: FontWeight.w400),
displaySmall: TextStyle(fontSize: 36, fontWeight: FontWeight.w400),
// Texto de título
headlineLarge: TextStyle(fontSize: 32, fontWeight: FontWeight.w400),
headlineMedium: TextStyle(fontSize: 28, fontWeight: FontWeight.w400),
headlineSmall: TextStyle(fontSize: 24, fontWeight: FontWeight.w400),
// Texto de título
titleLarge: TextStyle(fontSize: 22, fontWeight: FontWeight.w500),
titleMedium: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
titleSmall: TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
// Texto del cuerpo
bodyLarge: TextStyle(fontSize: 16, fontWeight: FontWeight.w400),
bodyMedium: TextStyle(fontSize: 14, fontWeight: FontWeight.w400),
bodySmall: TextStyle(fontSize: 12, fontWeight: FontWeight.w400),
// Texto de etiqueta
labelLarge: TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
labelMedium: TextStyle(fontSize: 12, fontWeight: FontWeight.w500),
labelSmall: TextStyle(fontSize: 11, fontWeight: FontWeight.w500),
),
)
textTheme: const TextTheme(
// Texto de exhibición (título grande)
displayLarge: TextStyle(fontSize: 57, fontWeight: FontWeight.w400),
displayMedium: TextStyle(fontSize: 45, fontWeight: FontWeight.w400),
displaySmall: TextStyle(fontSize: 36, fontWeight: FontWeight.w400),
// Texto de título
headlineLarge: TextStyle(fontSize: 32, fontWeight: FontWeight.w400),
headlineMedium: TextStyle(fontSize: 28, fontWeight: FontWeight.w400),
headlineSmall: TextStyle(fontSize: 24, fontWeight: FontWeight.w400),
// Texto de título
titleLarge: TextStyle(fontSize: 22, fontWeight: FontWeight.w500),
titleMedium: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
titleSmall: TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
// Texto del cuerpo
bodyLarge: TextStyle(fontSize: 16, fontWeight: FontWeight.w400),
bodyMedium: TextStyle(fontSize: 14, fontWeight: FontWeight.w400),
bodySmall: TextStyle(fontSize: 12, fontWeight: FontWeight.w400),
// Texto de etiqueta
labelLarge: TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
labelMedium: TextStyle(fontSize: 12, fontWeight: FontWeight.w500),
labelSmall: TextStyle(fontSize: 11, fontWeight: FontWeight.w500),
),
)
Otras extensionesUsar ThemeData para gestionar de forma unificada los estilos de la aplicación permite implementar fácilmente el cambio de tema y la uniformidad de estilos, facilitando el mantenimiento.