import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Theming Demo',
theme: ThemeData(
primaryColor: Colors.blue,
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue).copyWith(secondary: Colors.orange),
disabledColor: Colors.grey.shade400,
),
home: const ThemedButtonsScreen(),
);
}
}
class ThemedButtonsScreen extends StatelessWidget {
const ThemedButtonsScreen({super.key});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Scaffold(
appBar: AppBar(
title: const Text('Themed Buttons Screen'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton(
onPressed: () {},
child: const Text('Primary Button'),
),
const SizedBox(height: 16),
OutlinedButton(
onPressed: () {},
style: OutlinedButton.styleFrom(
foregroundColor: theme.colorScheme.secondary,
),
child: const Text('Secondary Button'),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: null, // disabled
style: ElevatedButton.styleFrom(
backgroundColor: theme.disabledColor,
foregroundColor: Colors.white,
),
child: const Text('Disabled Button'),
),
],
),
),
);
}
}
We define a ThemeData with primary and secondary colors. The MaterialApp uses this theme, so all widgets inside can access it. The ElevatedButton uses the primary color automatically. The OutlinedButton uses the secondary color from the theme. The disabled button uses the theme's disabled color to appear faded. This way, all buttons share consistent colors and styles, making the UI look unified and professional.