Line data Source code
1 : import 'package:flutter/material.dart';
2 :
3 : /// A customizable wrapper that applies padding to its child widget.
4 : ///
5 : /// The [PaddingWrapper] widget simplifies the application of padding around a child widget.
6 : /// It provides multiple factory constructors to handle different padding configurations,
7 : /// such as uniform padding on all sides, symmetric padding, or padding on specific sides.
8 : ///
9 : /// Example usage:
10 : /// ```dart
11 : /// PaddingWrapper.all(
12 : /// padding: 20.0,
13 : /// child: Text('Padded Text'),
14 : /// );
15 : /// ```
16 : class PaddingWrapper extends StatelessWidget {
17 : /// The widget below this widget in the tree.
18 : ///
19 : /// Typically, this is the content that requires padding.
20 : final Widget child;
21 :
22 : /// The padding to apply around the [child].
23 : ///
24 : /// Defaults to 16.0 logical pixels on all sides.
25 : final EdgeInsetsGeometry padding;
26 :
27 : /// Creates a [PaddingWrapper] with customizable padding.
28 : ///
29 : /// The [child] parameter is required and must not be null.
30 : /// The [padding] parameter defaults to 16.0 pixels on all sides if not specified.
31 2 : const PaddingWrapper({
32 : super.key,
33 : required this.child,
34 : this.padding = const EdgeInsets.all(16.0), // Default padding value
35 : });
36 :
37 : /// Factory constructor for applying the same padding to all sides.
38 : ///
39 : /// The [padding] parameter specifies the uniform padding to apply.
40 : ///
41 : /// Example usage:
42 : /// ```dart
43 : /// PaddingWrapper.all(
44 : /// padding: 20.0,
45 : /// child: Text('Uniformly Padded Text'),
46 : /// );
47 : /// ```
48 1 : factory PaddingWrapper.all({
49 : required Widget child,
50 : double padding = 16.0,
51 : }) {
52 1 : return PaddingWrapper(
53 1 : padding: EdgeInsets.all(padding),
54 : child: child,
55 : );
56 : }
57 :
58 : /// Factory constructor for symmetric padding (vertical and horizontal).
59 : ///
60 : /// The [vertical] and [horizontal] parameters specify the padding for vertical
61 : /// and horizontal sides, respectively.
62 : ///
63 : /// Example usage:
64 : /// ```dart
65 : /// PaddingWrapper.symmetric(
66 : /// vertical: 10.0,
67 : /// horizontal: 20.0,
68 : /// child: Text('Symmetrically Padded Text'),
69 : /// );
70 : /// ```
71 1 : factory PaddingWrapper.symmetric({
72 : required Widget child,
73 : double vertical = 16.0,
74 : double horizontal = 16.0,
75 : }) {
76 1 : return PaddingWrapper(
77 1 : padding: EdgeInsets.symmetric(vertical: vertical, horizontal: horizontal),
78 : child: child,
79 : );
80 : }
81 :
82 : /// Factory constructor for specific padding on each side.
83 : ///
84 : /// Allows setting padding individually for the left, right, top, and bottom sides.
85 : ///
86 : /// Example usage:
87 : /// ```dart
88 : /// PaddingWrapper.only(
89 : /// left: 10.0,
90 : /// right: 20.0,
91 : /// top: 5.0,
92 : /// bottom: 15.0,
93 : /// child: Text('Custom Side Padded Text'),
94 : /// );
95 : /// ```
96 1 : factory PaddingWrapper.only({
97 : required Widget child,
98 : double left = 16.0,
99 : double right = 16.0,
100 : double top = 0.0,
101 : double bottom = 0.0,
102 : }) {
103 1 : return PaddingWrapper(
104 : padding:
105 1 : EdgeInsets.only(left: left, right: right, top: top, bottom: bottom),
106 : child: child,
107 : );
108 : }
109 :
110 : /// Factory constructor for horizontal padding only.
111 : ///
112 : /// Applies padding to the left and right sides while leaving the top and bottom unpadded.
113 : ///
114 : /// Example usage:
115 : /// ```dart
116 : /// PaddingWrapper.horizontal(
117 : /// horizontal: 25.0,
118 : /// child: Text('Horizontally Padded Text'),
119 : /// );
120 : /// ```
121 0 : factory PaddingWrapper.horizontal({
122 : required Widget child,
123 : double horizontal = 16.0,
124 : }) {
125 0 : return PaddingWrapper(
126 0 : padding: EdgeInsets.symmetric(horizontal: horizontal),
127 : child: child,
128 : );
129 : }
130 :
131 : /// Factory constructor for vertical padding only.
132 : ///
133 : /// Applies padding to the top and bottom sides while leaving the left and right unpadded.
134 : ///
135 : /// Example usage:
136 : /// ```dart
137 : /// PaddingWrapper.vertical(
138 : /// vertical: 30.0,
139 : /// child: Text('Vertically Padded Text'),
140 : /// );
141 : /// ```
142 0 : factory PaddingWrapper.vertical({
143 : required Widget child,
144 : double vertical = 16.0,
145 : }) {
146 0 : return PaddingWrapper(
147 0 : padding: EdgeInsets.symmetric(vertical: vertical),
148 : child: child,
149 : );
150 : }
151 :
152 1 : @override
153 : Widget build(BuildContext context) {
154 1 : return Padding(
155 1 : padding: padding,
156 1 : child: child,
157 : );
158 : }
159 : }
160 :
161 : /// A customizable list with padded children, providing default or custom padding.
162 : ///
163 : /// The [PaddedChildrenList] widget is designed to display a vertical list of widgets
164 : /// with consistent padding. It offers multiple factory constructors to handle different
165 : /// padding configurations, ensuring flexibility in various layout scenarios.
166 : ///
167 : /// Example usage:
168 : /// ```dart
169 : /// PaddedChildrenList.symmetric(
170 : /// vertical: 10.0,
171 : /// horizontal: 20.0,
172 : /// children: [
173 : /// Text('Item 1'),
174 : /// Text('Item 2'),
175 : /// Text('Item 3'),
176 : /// ],
177 : /// );
178 : /// ```
179 : class PaddedChildrenList extends StatelessWidget {
180 : /// The list of child widgets to display.
181 : ///
182 : /// These widgets are arranged vertically within the list.
183 : final List<Widget> children;
184 :
185 : /// The padding to apply around the entire list.
186 : ///
187 : /// Defaults to 16.0 pixels on the left, right, and bottom sides.
188 : final EdgeInsetsGeometry padding;
189 :
190 : /// The alignment of the children along the main axis (vertical).
191 : ///
192 : /// Defaults to [MainAxisAlignment.start], aligning children to the top.
193 : final MainAxisAlignment mainAxisAlignment;
194 :
195 : /// The alignment of the children along the cross axis (horizontal).
196 : ///
197 : /// Defaults to [CrossAxisAlignment.start], aligning children to the start (left).
198 : final CrossAxisAlignment crossAxisAlignment;
199 :
200 : /// Creates a [PaddedChildrenList] with customizable padding and alignment.
201 : ///
202 : /// The [children] parameter is required and must not be null.
203 : /// The [padding] parameter defaults to 16.0 pixels on the left, right, and bottom sides
204 : /// if not specified.
205 : /// The [mainAxisAlignment] and [crossAxisAlignment] parameters control the alignment
206 : /// of the child widgets within the list.
207 2 : const PaddedChildrenList({
208 : super.key,
209 : required this.children,
210 : this.padding = const EdgeInsets.only(left: 16.0, right: 16.0, bottom: 16.0),
211 : this.mainAxisAlignment = MainAxisAlignment.start,
212 : this.crossAxisAlignment = CrossAxisAlignment.start,
213 : });
214 :
215 : /// Factory constructor for default padding (all sides equal).
216 : ///
217 : /// Applies the same padding value to all sides of the list.
218 : ///
219 : /// Example usage:
220 : /// ```dart
221 : /// PaddedChildrenList.all(
222 : /// padding: 20.0,
223 : /// children: [
224 : /// Text('Item 1'),
225 : /// Text('Item 2'),
226 : /// Text('Item 3'),
227 : /// ],
228 : /// );
229 : /// ```
230 1 : factory PaddedChildrenList.all({
231 : required List<Widget> children,
232 : double padding = 16.0,
233 : MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
234 : CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.start,
235 : }) {
236 1 : return PaddedChildrenList(
237 1 : padding: EdgeInsets.all(padding),
238 : mainAxisAlignment: mainAxisAlignment,
239 : crossAxisAlignment: crossAxisAlignment,
240 : children: children,
241 : );
242 : }
243 :
244 : /// Factory constructor for symmetric padding (vertical and horizontal).
245 : ///
246 : /// Applies different padding values for vertical and horizontal sides.
247 : ///
248 : /// Example usage:
249 : /// ```dart
250 : /// PaddedChildrenList.symmetric(
251 : /// vertical: 10.0,
252 : /// horizontal: 20.0,
253 : /// children: [
254 : /// Text('Item 1'),
255 : /// Text('Item 2'),
256 : /// Text('Item 3'),
257 : /// ],
258 : /// );
259 : /// ```
260 1 : factory PaddedChildrenList.symmetric({
261 : required List<Widget> children,
262 : double vertical = 16.0,
263 : double horizontal = 16.0,
264 : MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
265 : CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.start,
266 : }) {
267 1 : return PaddedChildrenList(
268 1 : padding: EdgeInsets.symmetric(vertical: vertical, horizontal: horizontal),
269 : mainAxisAlignment: mainAxisAlignment,
270 : crossAxisAlignment: crossAxisAlignment,
271 : children: children,
272 : );
273 : }
274 :
275 : /// Factory constructor for custom padding (only specific sides).
276 : ///
277 : /// Allows setting padding individually for the left, right, top, and bottom sides.
278 : ///
279 : /// Example usage:
280 : /// ```dart
281 : /// PaddedChildrenList.only(
282 : /// left: 10.0,
283 : /// right: 20.0,
284 : /// top: 5.0,
285 : /// bottom: 15.0,
286 : /// children: [
287 : /// Text('Item 1'),
288 : /// Text('Item 2'),
289 : /// Text('Item 3'),
290 : /// ],
291 : /// );
292 : /// ```
293 0 : factory PaddedChildrenList.only({
294 : required List<Widget> children,
295 : double left = 16.0,
296 : double right = 16.0,
297 : double top = 0.0,
298 : double bottom = 16.0, // Ensure the bottom defaults to 16.0
299 : MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
300 : CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.start,
301 : }) {
302 0 : return PaddedChildrenList(
303 : padding:
304 0 : EdgeInsets.only(left: left, right: right, top: top, bottom: bottom),
305 : mainAxisAlignment: mainAxisAlignment,
306 : crossAxisAlignment: crossAxisAlignment,
307 : children: children,
308 : );
309 : }
310 :
311 1 : @override
312 : Widget build(BuildContext context) {
313 1 : return SingleChildScrollView(
314 1 : padding: padding,
315 1 : child: Column(
316 1 : mainAxisAlignment: mainAxisAlignment,
317 1 : crossAxisAlignment: crossAxisAlignment,
318 1 : children: children,
319 : ),
320 : );
321 : }
322 : }
|