I want a button to change between ElevatedButton
and OutlinedButton
. I have this code snipped:
class Home extends StatefulWidget {
const Home({super.key});
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
var _highlighted = false;
void _onPressed() {
setState(() {
_highlighted = !_highlighted;
});
}
@override
Widget build(BuildContext context) {
const buttonContent = Text('Button');
return Scaffold(
body: Center(
child: _highlighted
? ElevatedButton(
onPressed: _onPressed,
child: buttonContent,
)
: OutlinedButton(
onPressed: _onPressed,
child: buttonContent,
),
),
);
}
}
Here is how it looks:
I would like the transition to be "smooth". When the button changes from one style to another, it would like it to be animated. How to do that?
2
Answers
I managed to get the desired effect using
AnimatedTheme
.Look at the widget
AnimatedButton
in the following code snippet:The result is (with "slow animation" enabled):
Simply use AnimatedSwitcher widget