Looking to create a custom button in flutter that is based on the CupertioButton
.
the end result should be looking like this:
MainCustomButton.success(label: 'ok', onPressed: (){});
MainCustomButton.danger(label: 'delete', onPressed: (){});
with each of the 'clones' have it's custom styles applied.
This is the code I have been messing with but I couldn't go further than this:
class MainCustomButton extends StatefulWidget {
MainCustomButton.success({
Key? key,
}) : super(key: key);
MainCustomButton.danger({
Key? key,
}) : super(key: key);
@override
State<MainCustomButton> createState() => _MainCustomButtonState();
}
class _MainCustomButtonState extends State<MainCustomButton> {
@override
Widget build(BuildContext context) {
return CupertinoButton(child: Text('click me'), onPressed: () {});
}
}
The right way to do this is using custom constructors:
class MainCustomButton extends StatelessWidget {
const MainCustomButton({Key? key, required this.label}) : super(key: key);
final String label;
const MainCustomButton.success({super.key, this.label = 'ok'});
const MainCustomButton.danger({super.key, this.label = 'delete'});
@override
Widget build(BuildContext context) {
return CupertinoButton(
child: Text(label),
onPressed: () {},
);
}
}
And the final result:
Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: const [
MainCustomButton.success(),
MainCustomButton.danger(),
],
),
);