Issue
This Content is from Stack Overflow. Question asked by chuanpham
Please take a look at: https://im.ge/i/gif.1sFufF
I’m trying to disable shrink effect of FlexibleSpaceBar’s title
Here is the code:
return Scaffold(
backgroundColor: Colors.white,
body: DefaultTabController(
length: 3,
child: NestedScrollView(
//physics: const BouncingScrollPhysics(),
controller: _scrollController,
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
elevation: 0,
backgroundColor: Colors.white,
expandedHeight: maxExtent,
centerTitle: false,
leading: addLeadingIcon(),
title: _isSliverAppBarExpanded
? Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'BMW i8',
style: TextStyle(color: _textColor),
),
Text(
'BMW i8',
style: TextStyle(color: _textColor, fontSize: 12),
),
],
)
: null,
floating: true,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
centerTitle: true,
titlePadding: EdgeInsets.zero,
background: Image.network(
"https://images.pexels.com/photos/396547/pexels-photo-396547.jpeg?auto=compress&cs=tinysrgb&h=350",
fit: BoxFit.cover,
),
title: !_isSliverAppBarExpanded ? _floatingCard() : null,
),
),
SliverPersistentHeader(
delegate: _SliverAppBarDelegate(
TabBar(
padding: EdgeInsets.symmetric(horizontal: 20),
labelColor: Colors.black87,
unselectedLabelColor: Colors.grey,
labelPadding: EdgeInsets.zero,
tabs: [
Tab(text: "Overview"),
Tab(text: "Photos"),
Tab(text: "Review"),
],
),
),
pinned: true,
),
];
},
body: Center(
child: Text("Sample text"),
),
),
),
);
}
_floatingCard() {
return Stack(
children: [
Align(
alignment: Alignment(0, 1.2),
child: Container(
height: 115,
color: Colors.white,
),
),
Align(
alignment: Alignment(0, 0.85),
child: Container(
width: MediaQuery.of(context).size.width * 0.6,
height: 115,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
blurRadius: 2,
offset: Offset(0, 3),
),
],
),
child: Padding(
padding: const EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Some car",
style: TextStyle(color: Colors.black),
),
Row(
children: [
Icon(Icons.star),
Text(
"4.7",
style: TextStyle(color: Colors.black),
)
],
)
],
),
Padding(
padding: const EdgeInsets.only(bottom: 10.0),
child: Text(
"Some seat, some bags",
style: TextStyle(color: Colors.black, fontSize: 10),
),
),
Row(
children: [
Expanded(
child: _item(isLastItem: false),
),
Expanded(
child: _item(isLastItem: false),
),
Expanded(
child: _item(isLastItem: true),
),
],
)
],
),
),
),
),
],
);
}
I’ve tried the FittedBox solution (Prevent SliverAppBar title from wrapping while it shrinks in Flutter) but it didn’t work, does anyone have solution for this problem, thank you!!!
Solution
You can use a SizedBox
wrapped inside a FittedBox
:
title: FittedBox(
child: SizedBox(
width: MediaQuery.of(context).size.width,
child: Center(
child: Text(
'This is a very long title that will wrap several times while it shrinks',
style: TextStyle(fontSize: 30),
),
),
),
),
Result:
This Question was asked in StackOverflow by Jani and Answered by Mobina It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.