【问题标题】:Set background image height, width, position in Flutter在 Flutter 中设置背景图片的高度、宽度、位置
【发布时间】:2019-06-20 10:35:59
【问题描述】:

我的 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)));
            })
      ]),
    );
  }
}

【问题讨论】:

  • 尝试将其放在 SizedBox 中并提供高度和宽度参数。
  • 您能在此处添加您的代码吗?
  • @MichaelHathi 这似乎无效(我当然可能做错了)。
  • @yashthakkar1173 代码已添加。

标签: flutter flutter-layout flutter-sliver


【解决方案1】:

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

如果您确实需要在这里分隔标题和图像,则不能使用background & title 属性,而是需要使用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,

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

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

【讨论】:

  • 你能解释一下为什么这些可能有效吗?乍一看(和第一次尝试),它们只有在我使圆圈足够小以至于它不与文本重叠时才起作用,但它仍然可以与文本重叠并且不会像我希望/期望的那样真正将它向下推.
  • 这是 FlexibleSpaceBarbackground: 属性的期望行为 - 假设填充 appbar 的所有背景区域,现在 title 这里不是单独的元素,而是前景小部件FlexibleSpaceBar 的。如果你真的需要在这里分开标题和图片你不能使用background&title属性,而是使用列或列表视图而不是FlexibleSpaceBar
  • 我已经用 ListView 选项更新了答案。
猜你喜欢
  • 2019-09-12
  • 1970-01-01
  • 2012-07-24
  • 2016-10-08
  • 2013-06-25
  • 2013-02-24
  • 2023-02-01
  • 1970-01-01
  • 2017-07-13
相关资源
最近更新 更多