I am often coming across situations where I would like to style a Widget based on some conditional which might look like the following:
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(home: Home()));
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
bool condition = true;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: OutlinedButton(
style: OutlinedButton.styleFrom(
side:
BorderSide(color: condition ? Colors.blue : /** DEFAULT Color**/)),
onPressed: () => _changeColor(),
child: Text('Press')),
),
);
}
void _changeColor() => setState(() => condition = !condition);
}
The above code does not compile as Flutter wants me to specify an alternative color if condition is false. But I do not want to specify any color but simply use the default color set by my Theme in case of condition evaluating to false. How is this possible?
I know that when using the ternary operator in situations where a Widget is to be returned (e.g. after child: condition ? Container() : ...), we can simply specify SizedBox.shrink() in order to have no effect. What would I do respectively when not wanting to specify any style but fall back to default behaviour?