【问题标题】:Flutter How to vertically center the AppBar sliver tilte?Flutter 如何垂直居中 AppBar 条子标题?
【发布时间】:2019-01-23 17:24:00
【问题描述】:

我正在尝试将 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: () {/* ... */},
                ),
              ],
            ),
          ],
        ));
  }
}

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

【问题讨论】:

  • 我们看不到你的代码:D
  • @diegoveloper,我在添加它时遇到了麻烦。我现在明白了。
  • 我发布了我的答案

标签: flutter appbar flutter-sliver


【解决方案1】:

您可以使用您需要的children 创建一个Column 小部件:

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

【讨论】:

    猜你喜欢
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2011-03-09
    • 2021-05-16
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多