如何在FlexibleSpaceBar(SliverAppBar)中修复标题的textSize?

时间:2019-08-04 03:57:03

标签: flutter

如何在FlexibleSpaceBar中修复标题的detextSize?

...
child: CustomScrollView(
                slivers: <Widget>[
                  new SliverAppBar(
                    backgroundColor:
                        Colors.white,
                    automaticallyImplyLeading: false,
                    actions: <Widget>[new Container()],
                    expandedHeight: 230.0,
                    floating: false,
                    pinned: true,
                    primary: true,
                    snap: false,

                    flexibleSpace: new FlexibleSpaceBar(
                    collapseMode: CollapseMode.pin,
                    titlePadding: EdgeInsetsDirectional.only(start: 10, bottom: 0),
                    title: new Text('Some Text Here', style: TextStyle(color: Colors.black, fontSize: 14)),
])

当我折叠SliverAppBar并向上滚动时,FlexibleSpaceBar的标题会自动调整大小。我需要修复标题的文本大小。

1 个答案:

答案 0 :(得分:1)

您可以将FlexibleSpaceBar小部件包装在FlexibleSpaceBar.createSettings内并设置所需的值,如下所示:

  ScrollController _controller = ScrollController();
  final maxExtent = 230.0;
  double currentExtent = 0.0;

  @override
  void initState() {
    _controller.addListener(() {
      setState(() {
        currentExtent = maxExtent - _controller.offset;
        if (currentExtent < 0) currentExtent = 0.0;
        if (currentExtent > maxExtent) currentExtent = maxExtent;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(controller: _controller, slivers: <Widget>[
        new SliverAppBar(
          backgroundColor: Colors.red,
          automaticallyImplyLeading: false,
          actions: <Widget>[new Container()],
          expandedHeight: maxExtent,
          floating: false,
          pinned: true,
          primary: true,
          snap: false,
          flexibleSpace: FlexibleSpaceBar.createSettings(
            currentExtent: currentExtent,
            minExtent: 0,
            maxExtent: maxExtent,
            child: FlexibleSpaceBar(
              background: Placeholder(),
              collapseMode: CollapseMode.pin,
              titlePadding: EdgeInsetsDirectional.only(start: 10, bottom: 0),
              title: new Text(
                'Some Text Here',
                style: TextStyle(color: Colors.black, fontSize: 20),
              ),
            ),
          ),
        ),