3

我的 SliverAppBar 中有一个背景图像。我已经尝试过 BoxFit.contain、BoxFit.fill...等,但它们都不适合我想做的事情。

这是我能得到的:

不好

但这就是我想要的:

好的!

我看到有 BoxFit.values 但我找不到任何说明如何使用它的文档(如果它是正确的?)

这是我的代码:

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:my_app/Theme.dart' as MyTheme;
import 'package:my_app/ui/rule_section_details/RuleRow.dart';

@override
class SliverHeaderTest extends StatelessWidget {
  final DocumentSnapshot ruleGroup;

  SliverHeaderTest(this.ruleGroup);

  @override
  Widget build(BuildContext context) {
    return Material(
      child: CustomScrollView(slivers: <Widget>[
        SliverAppBar(
          floating: true,
          backgroundColor: Color(int.parse(ruleGroup['color'])),
          expandedHeight: 200.0,
          flexibleSpace: FlexibleSpaceBar(
            // background: Image.asset('assets/img/circular-image.png',
            // fit: BoxFit.contain),
            background: new Image(
              image: new AssetImage(ruleGroup['image']),
              height: MyTheme.Dimens.ruleGroupListIconHeight,
              width: MyTheme.Dimens.ruleGroupListIconWidth,
            ),
            title: Text(ruleGroup['name'],
                style: MyTheme.TextStyles.ruleSectionPageTitle),
            centerTitle: true,
          ),
          actions: <Widget>[
            IconButton(
              icon: const Icon(Icons.share),
              tooltip: 'Share',
              onPressed: () {/* ... */},
            ),
          ],
        ),
        StreamBuilder(
            stream: Firestore.instance
                .collection('rules')
                .where("section", isEqualTo: ruleGroup['id'])
                .orderBy("subsection")
                .orderBy("subsubsection")
                .orderBy("subsubsubsection")
                .snapshots(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) {
                return SliverList(
                  delegate: SliverChildListDelegate(
                    [
                      Container(
                        child: new Center(child: new Text('Loading...')),
                      )
                    ],
                  ),
                );
              }
              return SliverPadding(
                  padding: EdgeInsets.only(top: 16.0),
                  sliver: SliverList(
                      delegate: SliverChildBuilderDelegate((context, index) {
                    return new RuleRow(snapshot.data.documents[index]);
                  }, childCount: snapshot.data.documents.length)));
            })
      ]),
    );
  }
}
4

1 回答 1

3

这是background:属性的期望行为FlexibleSpaceBar- 假设填充 的所有背景区域Appbar,现在title这里不是要在背景下方呈现的单独元素,而是FlexibleSpaceBar要显示在顶部的前景小部件background:

如果您确实需要在此处分隔标题和图像,则不能使用background&title属性,而是需要使用ColumnorListView代替FlexibleSpaceBar.

您可以使用可能的选项尝试以下代码:

推荐解决方案:

SliverAppBar(
            backgroundColor: Colors.blue,
            expandedHeight: 200.0,
            floating: true,
            //  pinned: true,
            flexibleSpace: FlexibleSpaceBar(
                centerTitle: true,
                title: Text("Collapsing Toolbar",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 16.0,
                    )),
                background: Row(
                  children: <Widget>[
                    Spacer(),
                    CircleAvatar(
                      radius: 54.0,
                      backgroundImage: NetworkImage(
                        "https://placeimg.com/640/480/animals",
                      ),
                    ),
                    Spacer(),
                  ],
                )),
          ),

此图像与radius: 68.0,.

在此处输入图像描述

以下是使用固定边距,可能会导致响应式设计出现问题,但仍然有效。

ClipOval

SliverAppBar(
            backgroundColor: Colors.blue,
            expandedHeight: 200.0,
            floating: true,
            //  pinned: true,
            flexibleSpace: FlexibleSpaceBar(
                centerTitle: true,
                title: Text("Collapsing Toolbar",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 16.0,
                    )),
                background: Container(
                  margin:
                      EdgeInsets.symmetric(horizontal: 125.0, vertical: 50.0),
                  child: ClipOval(
                    child: Image.network(
                      "https://placeimg.com/640/480/animals",
                    ),
                  ),
                )),
          ),

在此处输入图像描述

CircleAvatar

SliverAppBar(
            backgroundColor: Colors.blue,
            expandedHeight: 200.0,
            floating: true,
            //  pinned: true,
            flexibleSpace: FlexibleSpaceBar(
                centerTitle: true,
                title: Text("Collapsing Toolbar",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 16.0,
                    )),
                background: Container(
                  margin:
                      EdgeInsets.symmetric(horizontal: 125.0, vertical: 50.0),
                  child: CircleAvatar(
                    radius: 30.0,
                    backgroundImage: NetworkImage(
                      "https://placeimg.com/640/480/animals",
                    ),
                  ),
                )),
          ),

在此处输入图像描述

更新:

ListView选项。注意:AppBar高度由expandedHeight:属性决定,不会随着图像半径的增加而增加。

SliverAppBar(
            backgroundColor: Colors.blue,
            expandedHeight: 200.0,
            floating: true,
            //  pinned: true,
            flexibleSpace: Center(
              child: ListView(
                shrinkWrap: true,
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      Spacer(),
                      CircleAvatar(
                        radius: 68.0,
                        backgroundImage: NetworkImage(
                          "https://placeimg.com/640/480/animals",
                        ),
                      ),
                      Spacer(),
                    ],
                  ),
                  Center(
                    child: Text("Collapsing Toolbar",
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 22.0,
                        )),
                  ),
                ],
              ),
            ),
          ),
于 2019-02-01T06:49:08.070 回答