'Custom Padding Widget on Flutter
Hello Everyone;
I'm trying to make a custom widget that extends EdgeInsets class. I want a custom widget because i will use it in my project like that;
CustomPadding.all(20) - CustomPadding.only(top: 10)
How can i use like this? Thanks you all.
I am using like this for now;
class ProjectDecorations {
static const EdgeInsets homepage3dotsPadding = EdgeInsets.only(right: 5);
static const EdgeInsets centerImgPadding = EdgeInsets.only(top: 180);
static const EdgeInsets loginButtonTopPadding = EdgeInsets.only(top: 100);
static const EdgeInsets createAccountTopPadding = EdgeInsets.only(top: 20);
Solution 1:[1]
Create a file that you can name constants.dart, inside this file set your custom padding and you can name it depending on the context you'll use each one of them as follows:
const EdgeInsets kRegularPadding = EdgeInsets.all(20);
const EdgeInsets kTopPadding = EdgeInsets.only(top: 10);
Now you can use it in your code as follows:
class SampleCode extends StatelessWidget {
const SampleCode({ Key? key }) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
padding: kRegularPadding,
margin: kTopPadding,
child: YourWidget()
);
}
}
Don't forget to import the file constants.dart
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | App Developer |
