Skip to content

Instantly share code, notes, and snippets.

@valterh4ck3r
Last active May 5, 2022 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valterh4ck3r/c809d64d9c023c9ab087b03320a8baf0 to your computer and use it in GitHub Desktop.
Save valterh4ck3r/c809d64d9c023c9ab087b03320a8baf0 to your computer and use it in GitHub Desktop.
Animated Button with States
import 'package:flutter/material.dart';
class AnimatedButton extends StatefulWidget {
bool Function() validate;
Function() onPressed;
bool isLoading;
bool isSuccess;
String text;
AnimatedButton(
{Key? key,
required this.validate,
required this.onPressed,
required this.isLoading,
required this.isSuccess,
required this.text})
: super(key: key);
@override
_AnimatedButtonState createState() => _AnimatedButtonState();
}
class _AnimatedButtonState extends State<AnimatedButton> {
@override
Widget build(BuildContext context) {
return Center(
child: AnimatedContainer(
duration: const Duration(milliseconds: 400),
curve: Curves.linear,
width: widget.isLoading || widget.isSuccess
? 55
: MediaQuery.of(context).size.width,
height: widget.isLoading || widget.isSuccess ? 55 : 50,
child: ElevatedButton(
style: ButtonStyle(
backgroundColor: widget.isSuccess
? MaterialStateProperty.all(Colors.green)
: MaterialStateProperty.all(Theme.of(context).primaryColor),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(
widget.isLoading || widget.isSuccess ? 50 : 14),
))),
onPressed: () {
if (widget.isLoading || widget.isSuccess) return;
widget.onPressed.call();
},
child: widget.isSuccess
? const Icon(
Icons.check,
size: 25,
color: Colors.white,
)
: widget.isLoading
? const SizedBox(
width: 25,
height: 25,
child: CircularProgressIndicator(
color: Colors.white,
))
: const Text(
"Enviar",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 16),
)),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment