How to create new widget with extending custom widget in flutter?
3,194
Let suppose you have one container which changes its size based on the screen so we can assign values to them in the method parameter like:
1. Common Widget Class
class CommonWidget {
static Widget getContainer(double width, double height, Widget body) {
return Container(
width: width,
height: height,
child: body,
);
}
2. Usage
Widget body = Center(
child: Text('Welcome to Home Screen'),
);
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: CommonWidget.getContainer(100, 100, body), // These params you can change at your preference.
);
Author by
rahul Kushwaha
Updated on December 20, 2022Comments
-
rahul Kushwaha over 1 year
In the flutter, I have a very complex widget and its working fine. but for the different part of the app, I want to slightly modify the widget,
To achieve that I have to copy the entire widget with a different name and add the modification,
instead of copying, can we create a new widget with inheriting the widget and overriding widget in flutter?
ex:- consider I have this widget:
class ParentWidget extends StatefulWidget { @override _ParentWidgetState createState() => _ParentWidgetState(); } class _ParentWidgetState extends State<ParentWidget> { @override Widget build(BuildContext context) { return Container( ); } }
and I want to create child widget ex:-
class ChildWidget extends ParentWidget { }
where I should we able to modify all the aspects of the parent widget.