'Dynamic expandedHeight property in SliverAppBar based on flexibleSpace - Flutter

I have a SliverAppBar within a CustomScrollView that has a flexibleSpace with text. This text varies in size depending on the information it's presenting.

My problem is that I'm unable to determine what the expandedHeight property should be set to so that I can account for this variable height. Setting a static height causes overflows for the SliverToBoxAdapter if the text is too long.

I've been trying to determine the height of the text within the SliverAppBar at run time, but this is a bit out of the scope of my knowledge at the moment. If anyone knows how to do this, or has any other solutions to this problem that would work better, please let me know!

The code I've written for the CustomScrollView is below.

CustomScrollView(
            controller: widget._scrollController,
            physics: const BouncingScrollPhysics(),
            slivers: [
              SliverAppBar(
                backgroundColor: kPrimary,
                elevation: 100,
                leading: const AppBarLeading(function: LeadingFunction.back),
                actions: [AppBarAddButton(onTap: () {}, icon: Icons.edit)],
                expandedHeight: 203,
                flexibleSpace: FlexibleSpaceBar(
                  background: Column(
                    children: [
                      const Divider(
                        height: 55,
                        color: Color(0x00000000),
                      ),
                      RecipeDetailsHeader(
                        title: widget._recipeData.title,
                        description: widget._recipeData.description,
                      ),
                      const Divider(
                        height: 20,
                        color: Color(0x00000000),
                      ),
                    ],
                  ),
                ),
              ),
              SliverToBoxAdapter(
                child: RecipeDetailsBody(
                  recipeData: widget._recipeData,
                ),
              ),
            ],
          )


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source