4

我正在尝试显示标题,但如您所见,它没有正确显示。

在此处输入图像描述

我试图将 softWrap 设置为 true,但它仍然是一样的。

代码来自flutter contacts_demo 库

flexibleSpace: FlexibleSpaceBar(
          title: const Text('Fantastic Beasts And Where To Find Them'),
            background: Stack(
               fit: StackFit.expand,
              children: <Widget>[
                Image.asset(
                  'people/ali_landscape.png',
                  package: 'flutter_gallery_assets',
                  fit: BoxFit.cover,
                  height: _appBarHeight,
                ),
                // This gradient ensures that the toolbar icons are distinct
                // against the background image.
                const DecoratedBox(
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                      begin: Alignment(0.0, -1.0),
                      end: Alignment(0.0, -0.4),
                      colors: <Color>[Color(0x60000000), Color(0x00000000)],
                    ),
                  ),
                ),
              ],
            ),
          ),
4

4 回答 4

5

您可以ConstrainedBox使用MediaQuery.of(context).size.width

final mediaQuery = MediaQuery.of(context);

final availableWidth = mediaQuery.size.width - 160;

随着

title: ConstrainedBox(
    constraints: BoxConstraints(
        maxWidth: availableWidth,
    ),
    child: const Text('Fantastic Beasts And Where To Find Them'),
),
于 2018-11-11T21:24:13.663 回答
5

我挖掘了一个FlexibleSpaceBar消息来源,至少现在我明白会发生什么。事实证明,在展开状态下title会按比例放大到1.5它的大小,所以它自然会溢出屏幕。当用户向上滚动时,title将按比例缩小到其源大小1.0。在这个大小下,它将位于顶部工具栏中。

也许这些信息将帮助某人建立他们的解决方法,直到这个问题得到解决。

我想知道为什么我ConstraintedBox用 with包装标题的技巧maxWidth: MediaQuery.of(context).size.width不起作用。现在我知道了:我必须把它 maxWidth除以1.5

另请参阅Flutter github 问题跟踪器上的此错误。

于 2019-08-16T22:57:24.453 回答
1

由于显而易见的原因,标题长度和您设置的字体大小无法在较小的设备上显示在单行上。

您可能想要MediaQuery.of(context).size.width获取设备宽度并将标题文本 fontSize 相应地设置为其中的一部分。尝试在模拟器中查看哪个最适合您的文本长度。

const Text(
    'Fantastic Beasts And Where To Find Them', 
    style: TextStyle(fontSize: MediaQuery.of(context).size.width/ SOME_NUMBER),
),

或者只是根据一些宽度间隔对一些字体大小进行硬编码:

int _getFontSize(BuildContext context) {
    int width = MediaQuery.of(context).size.width;
    if (width < 300) {
        return 10;
    } else if (width < 600) {
        return 13;
    // etc ...
    } else {
        return 18;
    }

}

...

const Text(
    'Fantastic Beasts And Where To Find Them', 
    style: _getFontSize(context),
),
于 2018-11-11T20:41:39.063 回答
0

感谢 dimsuz 的回答,可以消除 a 中文本的放大FlexibleSpaceBar

SliverAppBar(
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.save),
            onPressed: () {},
          ),
        ],
        floating: true,
        pinned: false,
        snap: false,
        flexibleSpace: FlexibleSpaceBar(
          title: Text(title, style: TextStyle(fontSize: 20.0 / 1.5)),
          centerTitle: false,
          background: Container(
            color: Theme.of(context).primaryColor,
          ),
        ),
        expandedHeight: 120,
      ),

我从这里20.0拿的。fontSize

于 2019-11-29T11:46:21.960 回答