集的背景图像的高度,宽度,在颤振位置

问题描述 投票:1回答:1

我在我的SliverAppBar的背景图像。我试着BoxFit.contain,BoxFit.fill ...等,但它们都没有对想什么,我做的工作。

这是我能得到什么:

not good

但在这里就是我想要的:

good!

我看到有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)));
            })
      ]),
    );
  }
}
flutter flutter-layout flutter-sliver
1个回答
1
投票

这是background:FlexibleSpaceBar财产所期望的行为 - 它的假设,以填补Appbar的所有背景区域,现在title这里不是单独的元素来渲染背景下方,但FlexibleSpaceBar的前景部件上显示background:的顶部

如果你真的需要分开的标题和图片在这里你不能使用backgroundtitle财产,而是你需要使用ColumnListView代替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,

enter image description here

使用固定保证金以下,可能导致问题响应式设计,但仍然有效。

随着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",
                    ),
                  ),
                )),
          ),

enter image description here

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",
                    ),
                  ),
                )),
          ),

enter image description here

更新:

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