Created
March 22, 2025 00:19
-
-
Save prof3ssorSt3v3/5b97d5d89e4f6cd2d8ec53a8ca942992 to your computer and use it in GitHub Desktop.
Flutter example of setting colors with WidgetStatePropertyAll<Color> and WidgetStateColor.resolveWith
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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