I am stretching the image from the center. The image should be stretched from the center, image height should be the same as the content size. I am getting content height from GlobalKey. For ex. image height is 700px and after loading content(listview) height is 1000px. Image height should be stretched 300px. It should work like 9patch image. It can be done with the help of centerSlice property. attaching code and image below.
double heightTOadd = 0.0;
GlobalKey key = GlobalKey();
@override
void initState() {
super.initState();
Future.delayed(Duration(milliseconds: 1000), () async {
setState(() {
heightTOadd = key.currentContext!.size!.height;
});
});
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Stack(
children: [
Container(
width: MediaQuery.of(context).size.width,
height: heightTOadd,
decoration: BoxDecoration(
color: Colors.purple,
image: DecorationImage(
alignment : Alignment.topCenter,
fit: BoxFit.fill,
// centerSlice: Rect.fromLTRB(MediaQuery.of(context).size.width/ 2, heightTOadd / 2,MediaQuery.of(context).size.width/ 2, heightTOadd / 2),
image: AssetImage('images/homeBG.png'),
)),
),
listView(),
],
),
);
}
Widget listView() {
return ListView(
key: key,
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
children: [cell(), cell(), cell(), cell(), cell(), cell(), cell(), cell(), cell(), cell(), cell(), cell(), ],
);
}
Widget cell() {
return Container(
padding: const EdgeInsets.all(20),
child:Container(height: 150, color: Colors.red.withOpacity(0.0),)
);
}