how to set margin for a button in flutter ?

Edge property is utilized to set void space around an article or Widget in Flutter. The majority of vacillate gadgets straightforwardly portion not help edge property yet utilizing the Child Wrapping procedure we can apply edge to any gadget in shudder utilizing Container gadget. Holder gadget uphold edge property with all the Left, Top, Right and Bottom side. So in this instructional exercise we would Add Set Margin for Raised Button in Flutter Android iOS Example Tutorial.

Contents in this project Add Set Margin for Raised Button in Flutter Android iOS Example Tutorial:

  1. Import material.dart bundle in your application’s main.dart document.
import 'package:flutter/material.dart';

2. Make void fundamental runApp() strategy and call our principle Root parent class named as MyApp here.

void main() => runApp(MyApp());

3. Make our fundamental class named as MyApp stretches out with StatelessWidget.

class MyApp extends StatelessWidget {


}

4. Make a Random capacity with some print log message in MyApp class. We would call this capacity on catch onPress occasion.

  sampleFunction() {
    print('Function Called');
  }

5. Make gadget construct zone in MyApp class. Here we would initially make the Scaffold gadget in MaterialApp gadget . At that point we would make a Center gadget – > Column gadget. We would put our catch inside the Column gadget.

Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
        body: Center(
        child: Column(
          children: <Widget>[

        
        
        ])
      )
     )
   );
  }

6. Making the Container gadget and put the Raised Button inside the Container gadget as Child. We would apply the edge on Container gadget and it will naturally apply on the kid button gadget.

  • margin : Used to set the edge around the youngster gadget.
  • EdgeInsets.fromLTRB() : Here LTRB implies Left, Top, Right and Bottom side.
Container(
   margin: const EdgeInsets.fromLTRB(20, 100, 20, 20),
   child:
   RaisedButton(
   onPressed: () => sampleFunction(),
   child: Text(' Button in Flutter With Margin '),
   textColor: Colors.white,
   color: Colors.green,
   padding: EdgeInsets.fromLTRB(12, 12, 12, 12),
    )
  )

7. Complete source code for main.dart record:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

  sampleFunction() {
    print('Function Called');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
        body: Center(
        child: Column(
          children: <Widget>[

            Container(
              margin: const EdgeInsets.fromLTRB(20, 100, 20, 20),
              child:
              RaisedButton(
              onPressed: () => sampleFunction(),
              child: Text(' Button in Flutter With Margin '),
              textColor: Colors.white,
              color: Colors.green,
              padding: EdgeInsets.fromLTRB(12, 12, 12, 12),
            )
          )
        
        ])
      )
     )
   );
  }
}

Screenshot:

Add Set Margin for Raised Button in Flutter Android iOS Example

Leave a Comment