I am using Flutter ElevatedButton, which is recommended over RaisedButton in the docs.
In raised button, you could specify and disabledColor. In ElevatedButton, I can not.
How can I stylize what the ElevatedButton looks like when it is disabled?
To change the Elevated Button color in Flutter, simply set the style property of Elevated Button from the ElevatedButton. styleFrom() static method and set the primary property to the appropriate color.
return MaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter ElevatedButton Color Change Example', theme: ThemeData( elevatedButtonTheme: ElevatedButtonThemeData( style: ElevatedButton. styleFrom( primary: Colors. red, ), ), ), Flutter Folder Picker Example.
You can custom the shape of an elevated button by using the shape parameter of the styleFrom method. Example: ElevatedButton( onPressed: () {}, child: const Text('Kindacode.com'), style: ElevatedButton.
To change the Outlined Button color in Flutter, simply set the style property of Outlined Button from the OutlinedButton. styleFrom() static method and set the backgroundColor property to change background color and primary property to change the text color.
You can copy paste run full code below
You can use ButtonStyle
and check states.contains(MaterialState.disabled)
return color you need
In demo code, disabled color is green
code snippet
ElevatedButton(
onPressed: null,
child: Text('Submit disable'),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed))
return Theme.of(context)
.colorScheme
.primary
.withOpacity(0.5);
else if (states.contains(MaterialState.disabled))
return Colors.green;
return null; // Use the component's default.
},
),
),
),
working demo
full code
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: null,
child: Text('Submit disable'),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed))
return Theme.of(context)
.colorScheme
.primary
.withOpacity(0.5);
else if (states.contains(MaterialState.disabled))
return Colors.green;
return null; // Use the component's default.
},
),
),
),
ElevatedButton(
onPressed: () {
print("clicked");
},
child: Text('Submit enable'),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed))
return Theme.of(context)
.colorScheme
.primary
.withOpacity(0.5);
else if (states.contains(MaterialState.disabled))
return Colors.green;
return null; // Use the component's default./ Use the component's default.
},
),
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With