Flutter 如何垂直居中AppBar条倾斜?

问题描述 投票:0回答:2

我正在尝试将 Sliver AppBar 的标题居中,并在其下方添加第二个文本。我做不到。

下面是现在的图像以及应该的样子。

有人可以帮助我吗?

这是我的代码。

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: () {/* ... */},
                ),
              ],
            ),
          ],
        ));
  }
}

“” ”” ”” “”“”“” ”” """""""""" ”” ”” """""""""""""""""""""""""""

flutter appbar flutter-sliver
2个回答
6
投票

您可以使用您需要的

Column
创建一个
children
小部件:

    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: () {/* ... */},
                    ),
                  ],
                ),
              ],
            )); 

0
投票

尝试使用下面的代码片段

@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"),
            );
          },
        ),
      )
    ],
  ),
);
}
© www.soinside.com 2019 - 2024. All rights reserved.