Add Padding to Widget in Flutter – example

In this article, we will learn How To Add Padding To Any Widget In Flutter. Creating Padding Widget Above we have Container as the parent…

The post Add Padding to Widget in Flutter – example appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Jatin Hemnani

In this article, we will learn How To Add Padding To Any Widget In Flutter.

Creating Padding Widget

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.red,
      child: Padding(
        padding: EdgeInsets.all(50.0),
        child: Text(
          'CodeSource.io',
          style: TextStyle(color: Colors.white, fontSize: 20),
        ),
      ),
    );
  }
}

Above we have Container as the parent widget and the child is Padding() widget, it has a padding property that takes EdgeInsets with all() and other method with the float value (amount of padding). The Padding widget has a child as Text() with some styles such as font size, color.

NOTE: Container also has a property of padding but it will give padding to all its children tree and for the specific widget, we use Padding Widget.

Complete Code

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: Text('Home'),
        centerTitle: true,
      ),
      body: Home(),
    ));
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.red,
      child: Padding(
        padding: EdgeInsets.all(50.0),
        child: Text(
          'CodeSource.io',
          style: TextStyle(color: Colors.white, fontSize: 20),
        ),
      ),
    );
  }
}

Result

If you follow the above code you will get something like this:

Padding Widget
Before Padding
After Padding

The post Add Padding to Widget in Flutter – example appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Jatin Hemnani


Print Share Comment Cite Upload Translate Updates
APA

Jatin Hemnani | Sciencx (2021-02-25T13:06:30+00:00) Add Padding to Widget in Flutter – example. Retrieved from https://www.scien.cx/2021/02/25/add-padding-to-widget-in-flutter-example/

MLA
" » Add Padding to Widget in Flutter – example." Jatin Hemnani | Sciencx - Thursday February 25, 2021, https://www.scien.cx/2021/02/25/add-padding-to-widget-in-flutter-example/
HARVARD
Jatin Hemnani | Sciencx Thursday February 25, 2021 » Add Padding to Widget in Flutter – example., viewed ,<https://www.scien.cx/2021/02/25/add-padding-to-widget-in-flutter-example/>
VANCOUVER
Jatin Hemnani | Sciencx - » Add Padding to Widget in Flutter – example. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/25/add-padding-to-widget-in-flutter-example/
CHICAGO
" » Add Padding to Widget in Flutter – example." Jatin Hemnani | Sciencx - Accessed . https://www.scien.cx/2021/02/25/add-padding-to-widget-in-flutter-example/
IEEE
" » Add Padding to Widget in Flutter – example." Jatin Hemnani | Sciencx [Online]. Available: https://www.scien.cx/2021/02/25/add-padding-to-widget-in-flutter-example/. [Accessed: ]
rf:citation
» Add Padding to Widget in Flutter – example | Jatin Hemnani | Sciencx | https://www.scien.cx/2021/02/25/add-padding-to-widget-in-flutter-example/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.