[英]How to pass the height of parent widget to child widget in flutter?
我正在嘗試從 Flutter 中的父小部件傳遞堆棧的高度,但我無法做到這一點。請幫助我找到解決方案,我將我的代碼作為參考。
父小部件
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Stack(
children: [
Picturebackground(//pass Stack height ),
Center(
child: Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text('varying text'),
],
),
),
],
),
);
}
兒童小部件
dynamic listImagesnotFound = ['assets/svg1.svg','assets/svg2.svg','assets/svg3.svg','assets/svg4.svg'];
Random rnd;
class Picturebackground extends StatelessWidget {
final double stack_height;
Picturebackground ({Key key, this.stack_height}) : super(key: key);
Widget build(BuildContext context) {
int min = 0;
int max = listImagesnotFound.length - 1;
rnd = new Random();
int r = min + rnd.nextInt(max - min);
String image_name = listImagesnotFound[r].toString();
return SvgPicture.asset(image_name,
fit: BoxFit.cover,
width: MediaQuery.of(context).size.width,
height: // get the height of the Stack
);
}
}
我如何將堆棧高度傳遞給 SvgPicture。
您可以使用LayoutBuilder
來獲取Picturebackground
小部件的約束:
dynamic listImagesnotFound = ['assets/svg1.svg','assets/svg2.svg','assets/svg3.svg','assets/svg4.svg'];
Random rnd;
class Picturebackground extends StatelessWidget {
Widget build(BuildContext context) {
int min = 0;
int max = listImagesnotFound.length - 1;
rnd = new Random();
int r = min + rnd.nextInt(max - min);
String image_name = listImagesnotFound[r].toString();
return LayoutBuilder(
builder: (context, constraints) {
// get here
final height = constraints.maxHeight;
return SvgPicture.asset(image_name,
fit: BoxFit.cover,
width: MediaQuery.of(context).size.width,
height: height // use here
);
}
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.