Cleudice Santos
Cleudice Santos

Reputation: 241

Flutter How to vertically center the AppBar sliver tilte?

I'm trying to center the title of Sliver AppBar and add a second text below this. I can not do it.

Below is the image that is now and how it should be.

Can anyone help me?

This is my code.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Slive AppBar',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      debugShowCheckedModeBanner: false,
      home: new MyHomePage(title: 'Slive AppBar'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        drawer: new Drawer(),
        body: new CustomScrollView(
          scrollDirection: Axis.vertical,
          slivers: <Widget>[
            new SliverAppBar(
              expandedHeight: 150.0,
              flexibleSpace: const FlexibleSpaceBar(
                title: const Text("US\$ 123.456.78"),
                centerTitle: true,
              ),
              backgroundColor: Colors.redAccent,
              pinned: true,
              actions: <Widget>[
                new IconButton(
                  icon: const Icon(Icons.add_circle),
                  tooltip: 'Balance',
                  onPressed: () {/* ... */},
                ),
              ],
            ),
          ],
        ));
  }
}

"" "" "" """""" "" """""""""" "" "" """"""""""""""""""""""""""""

Upvotes: 3

Views: 8743

Answers (3)

Kalpesh Khandla
Kalpesh Khandla

Reputation: 746

SliverAppBar(
flexibleSpace: Center(
    child: Text("27 is my favorite number")
  )
)

Upvotes: 0

Kalpesh Khandla
Kalpesh Khandla

Reputation: 746

Try with below code snippet

@override
Widget build(BuildContext context) {
return Scaffold(
  body: CustomScrollView(
    slivers: <Widget>[
      SliverAppBar(
        pinned: true,
        expandedHeight: 200,
        //backgroundColor: Colors.transparent,
        flexibleSpace: FlexibleSpaceBar(
          titlePadding: EdgeInsets.zero,
          centerTitle: true,
          title: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Flexible(
                flex: 3,
                child: Container(),
              ),
              Flexible(
                flex: 1,
                child:
                    Text("Should be centered", textAlign: TextAlign.center),
              ),
              Flexible(
                flex: 1,
                child: Container(),
              ),
            ],
          ),
          background: Image.asset("assets/earth.png", fit: BoxFit.cover),
        ),
        actions: <Widget>[
          IconButton(
            icon: const Icon(Icons.menu),
            tooltip: "Menu",
            onPressed: () {
              // onPressed handler
            },
          ),
        ],
      ),
      SliverFixedExtentList(
        itemExtent: 50,
        delegate: SliverChildBuilderDelegate(
          (BuildContext context, int index) {
            return Container(
              alignment: Alignment.center,
              color: Colors.green,
              child: Text("Index n°$index"),
            );
          },
        ),
      )
    ],
  ),
);
}

Upvotes: 0

diegoveloper
diegoveloper

Reputation: 103541

You can create a Column widget with the children that you need:

    return new Scaffold(
            drawer: new Drawer(),
            body: new CustomScrollView(
              scrollDirection: Axis.vertical,
              slivers: <Widget>[
                new SliverAppBar(
                  expandedHeight: 140.0,
                  flexibleSpace:  FlexibleSpaceBar(
                    title:  Column(
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                        mainAxisAlignment: MainAxisAlignment.start,
                        mainAxisSize: MainAxisSize.min,
                        children: <Widget>[
                          const Text("US\$ 123.456.78", textAlign: TextAlign.center,),
                          const Text("Anything", style: TextStyle(fontSize: 12.0),textAlign: TextAlign.center,),
                        ],
                      ),
                    centerTitle: true,
                  ),
                  backgroundColor: Colors.redAccent,
                  pinned: true,
                  actions: <Widget>[
                    new IconButton(
                      icon: const Icon(Icons.add_circle),
                      tooltip: 'Balance',
                      onPressed: () {/* ... */},
                    ),
                  ],
                ),
              ],
            )); 

Upvotes: 7

Related Questions