Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created March 22, 2025 00:19
Show Gist options
  • Save prof3ssorSt3v3/5b97d5d89e4f6cd2d8ec53a8ca942992 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/5b97d5d89e4f6cd2d8ec53a8ca942992 to your computer and use it in GitHub Desktop.
Flutter example of setting colors with WidgetStatePropertyAll<Color> and WidgetStateColor.resolveWith
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(),
home: MyPage(),
);
}
}
class MyPage extends StatelessWidget {
const MyPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
style: ButtonStyle(
backgroundColor:
WidgetStateColor.resolveWith((
Set<WidgetState> states,
) {
//states
if (states.contains(
WidgetState.pressed,
)) {
return Colors.amber.shade800;
}
return Colors.amber.shade100;
}),
foregroundColor:
WidgetStatePropertyAll<Color?>(
Colors.black54,
),
overlayColor: WidgetStateColor.resolveWith((
Set<WidgetState> states,
) {
if (states.contains(
WidgetState.pressed,
)) {
return Colors.blue;
} else {
return Colors.transparent;
}
}),
),
onPressed: () {},
child: Text('Press This'),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment