I am trying to create a dynamic elevated button widget.
class CustomeElevatedBtn extends StatelessWidget {
const CustomeElevatedBtn(
{required this.child_, required this.onPressed_, required this.style_});
final Widget child_;
final VoidCallback onPressed_;
final ??? style_;
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: onPressed_,
child: child_,
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25),
),
padding: EdgeInsets.symmetric(horizontal: 16),
textStyle: TextStyle(fontSize: 14),
backgroundColor: Colors.orangeAccent,
foregroundColor: Colors.white),
);
}
}
I want to make the style
widget dynamic, I mean ElevatedButton.styleFrom
values to be dynamic but I am unable to do so. How can I do it?
3
Answers
Try to replace
final ??? style_;
withfinal ButtonStyle? style_;
Declare
ButtonStyle
as nullable and use like belowJust replace
???
toButtonStyle
and use wherever you want like this:Try below code I have used it my big project. Refer ButtonStyle
Custom Button Class:
Use any where in project
If you want to using styleFrom then refer below code