We will create a custom button which will take string, icon, onPress event and fontSize. But our will have default values for string, icon, onPress event and fontSize.
void main() async {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primaryColor: Colors.blue,
fontFamily: "Lato",
),
home: Scaffold(
backgroundColor: Colors.white,
body: Center(
child: CustomButton(buttonText: 'test', fontSize: 16, icon: Icons.person_outline,
),
),
),
);
}
}
class CustomButton extends StatelessWidget {
final VoidCallback? onPressed;
final String buttonText;
final bool transparent;
EdgeInsets? margin;
final double? height;
final double? width;
final double? fontSize;
final double radius;
final IconData? icon;
CustomButton({ this.onPressed, required this.buttonText, this.transparent = false, this.margin, this.width, this.height,
this.fontSize, this.radius = 5, this.icon});
@override
Widget build(BuildContext context) {
final ButtonStyle _flatButtonStyle = TextButton.styleFrom(
backgroundColor: onPressed == null ? Theme.of(context).disabledColor : transparent
? Colors.transparent : Theme.of(context).primaryColor,
minimumSize: Size(width ?? 1200,
height ?? 50),
padding: EdgeInsets.zero,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(radius),
),
);
return Center(child: SizedBox(width: width != null ? width : 1200, child: Padding(
padding: margin ?? EdgeInsets.all(0) ,
child: TextButton(
onPressed: onPressed,
style: _flatButtonStyle,
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
icon != null ? Padding(
padding: EdgeInsets.only(right: 5),
child: Icon(icon, color: transparent ? Theme.of(context).primaryColor : Theme.of(context).cardColor),
) : SizedBox(),
Text(buttonText ??'', textAlign: TextAlign.center, style: TextStyle(
color: transparent ? Theme.of(context).primaryColor : Theme.of(context).cardColor,
fontSize: fontSize != null ? fontSize : 20,
)),
]),
),
)));
}
}
Your email address will not be published. Required fields are marked *
can we create coustom hook in flutter redux and error handling am getting confustion so much there my number 7376019481