1

The action buttons on the appBar in Flutter are aligned to the right side as default, I would like to align my FlatButton to the left, next to the title/logo.

Can anyone advise please?

Below are my codes:

@override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.white,
          title: const Text('Logo'),
          elevation: 0.0,
           actions: <Widget>[

            FlatButton(
                onPressed: () {
                  _select(choices[0]);
                },
                child: Text(
                  'Portfolio',
                  style: TextStyle(
                      fontFamily: 'Futura',
                      fontSize: 12,
                      color: Color.fromRGBO(80, 86, 89, 100)),
                )),

Cheers, Karen

Karen Chan
  • 164
  • 1
  • 1
  • 16

3 Answers3

8

You can also use the leading property of AppBar which places a widget before the title. If you need more than one widget there, you can nest them within a Row.

knary
  • 301
  • 2
  • 5
  • 3
    This is the proper way to go. I just don't like approaches like the one in the accepted answer, they're likely to break in the future or not work as expected under certain conditions you may not have noticed yet. – maganap Dec 28 '20 at 13:44
5
import 'package:flutter/material.dart';

class TestPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Row(
          children: <Widget>[
            Text(
              'Actions Align Left',
            ),
            FlatButton(
              child: Text('FlatButton'),
            )
          ],
        )
      ),
    );
  }
}

I used Row for the title and just put in the FlatButton there.

VLXU
  • 719
  • 5
  • 11
-1

Add default widget like Container in actions and wrap it inside Expanded widget

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
            backgroundColor: Colors.white,
            title: const Text('Logo'),
            elevation: 0.0,
            actions: <Widget>[
              FlatButton(
                  onPressed: () {},
                  child: Text(
                    'Portfolio',
                    style: TextStyle(
                        fontFamily: 'Futura',
                        fontSize: 12,
                        color: Color.fromRGBO(80, 86, 89, 100)),
                  )),
              Expanded(
                child: Container(),
              ),
            ]),
      ),
    );
  }
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
  • I tried this. However, it ended up pushing everything all the way to the left end of the app bar, causing everything else to be pushed out of view. What did I do wrong? Does it have anything to do with flutter versions? `Spacer()` also did not work, when used with `Row()` already. – VLXU Jun 05 '20 at 11:53