Tushar Rai
Tushar Rai

Reputation: 2519

How to override the custom padding in an App Bar of a flutter project?

My code gives me this output.

enter image description here

but I want to achieve this left padding in my appbar

enter image description here

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        brightness: Brightness.light,
        backgroundColor: Colors.transparent,
        elevation: 0.0,
        leading: Padding(
          padding: const EdgeInsets.only(left: 0),
          child: Container(
            width: 56,
            height: 56,
            padding: const EdgeInsets.all(8.0),
            child: Ink(
                child: IconButton(
                    icon: Icon(Icons.menu), iconSize: 16.0, onPressed: null),
                decoration: BoxDecoration(
                  color: Colors.grey.withOpacity(0.2),
                  borderRadius: BorderRadius.circular(20.0),
                )),
          ),
        ),
        actions: [
          Padding(
            padding: EdgeInsets.only(right: 48.0),
            child: Container(
              width: 56,
              height: 56,
              padding: const EdgeInsets.all(8.0),
              child: Ink(
                  child: IconButton(
                      icon: Icon(Icons.shopping_cart), iconSize: 16.0, onPressed: null),
                  decoration: BoxDecoration(
                    color: Colors.grey.withOpacity(0.2),
                    borderRadius: BorderRadius.circular(20.0),
                  )),
            ),
          )
        ],
      ),
      body: Center(),
    );
  }
}

Upvotes: 0

Views: 8289

Answers (2)

Tushar Rai
Tushar Rai

Reputation: 2519

This code helped me to achieve the above result.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: PreferredSize(
        preferredSize: const Size(0, 96),
        child: Padding(
          padding: const EdgeInsets.only(left: 40, right: 40),
          child: AppBar(
            brightness: Brightness.light,
            backgroundColor: Colors.transparent,
            elevation: 0.0,
            leading: Padding(
              padding: const EdgeInsets.only(left: 0),
              child: Container(
                width: 56,
                height: 56,
                padding: const EdgeInsets.all(8.0),
                child: Ink(
                    child: IconButton(
                        icon: Icon(Icons.menu, color: Colors.black,), iconSize: 20.0 ,onPressed: null),
                    decoration: BoxDecoration(
                      color: Colors.grey.withOpacity(0.2),
                      borderRadius: BorderRadius.circular(20.0),
                    )),
              ),
            ),
            actions: [
              Container(
                width: 56,
                height: 56,
                padding: const EdgeInsets.all(8.0),
                child: Ink(
                    child: IconButton(
                        icon: Icon(Icons.archive, color: Colors.black,), iconSize: 20.0, onPressed: null),
                    decoration: BoxDecoration(
                      color: Colors.grey.withOpacity(0.2),
                      borderRadius: BorderRadius.circular(20.0),
                    )),
              ),
            ],
          ),
        ),
      ),
      body: Row(
        children: [

        ],
      )
    );
  }
}

Upvotes: 0

Omar Hussein
Omar Hussein

Reputation: 59

In this case, you may use a custom app bar, the appBar property in the Scaffold widget implements a PreferredSizeWidget, therefore we need to use a widget that implements the same class which is PreferredSize:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        // You can set the size here, but it's left to zeros in order to expand based on its child.
        preferredSize: const Size(0, 0),
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Icon(Icons.account_box),
              Icon(Icons.account_circle),
            ],
          ),
        ),
      ),
      body: Container(),
    );
  }

Upvotes: 2

Related Questions