flutter - 如何在颤动中从另一个自定义小部件分配小部件子(容器)的高度和宽度
问题描述
我想更改自定义小部件 forProducts 内的容器小部件的宽度和高度,并且我想从另一个自定义 ProductContainer 小部件传递该宽度和高度,但是当我在 ProductContainer 中传递它时,它给出了未定义的名称高度和宽度。
这是我的代码,感谢您的帮助,谢谢,
import 'package:myEcomm/screens/info_page.dart';
import 'package:flutter/material.dart';
class ProductContainer extends StatefulWidget {
final List products;
final double height;
final double width;
const ProductContainer({
Key key,
this.products,
this.height,
this.width,
}) : super(key: key);
@override
_ProductContainerState createState() => _ProductContainerState();
}
class _ProductContainerState extends State<ProductContainer> {
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
height: 150,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: widget.products.length,
itemBuilder: (context, index) {
return forProducts(
image: widget.products[index]['imageLink'],
tag: widget.products[index]['tag'],
name: widget.products[index]['name'],
category: widget.products[index]["category"],
price: widget.products[index]["price"],
context: context);
},
),
),
],
),
);
}
}
Widget forProducts(
{String image = "images/",
@required String tag,
@required String name,
@required String category,
@required String price,
@required BuildContext context}) {
return Column(children: [
Material(
child: Hero(
tag: tag,
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => InfoPage(
image: image,
tag: tag,
name: name,
category: category,
price: price,
)));
},
child: Container(
padding: EdgeInsets.all(2),
margin: EdgeInsets.all(5),
height:
height, // this is the height which I want to assign from productcontainer(height: anyValue) widget
width:
width, // this is the width which I want to assign from productcontainer(width: anyValue) widget
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
image: DecorationImage(
fit: BoxFit.cover, image: AssetImage(image)),
),
),
))),
Text(name),
]);
}
解决方案
这对我有用
import 'package:myEcomm/screens/info_page.dart';
import 'package:flutter/material.dart';
class ProductContainer extends StatefulWidget {
final List products;
final double height;
final double width;
const ProductContainer({
Key key,
this.products,
this.height,
this.width,
}) : super(key: key);
@override
_ProductContainerState createState() => _ProductContainerState();
}
class _ProductContainerState extends State<ProductContainer> {
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
height: 150,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: widget.products.length,
itemBuilder: (context, index) {
return forProducts(
height: widget.height,
width: widget.width,
image: widget.products[index]['imageLink'],
tag: widget.products[index]['tag'],
name: widget.products[index]['name'],
category: widget.products[index]["category"],
price: widget.products[index]["price"],
context: context);
},
),
),
],
),
);
}
}
Widget forProducts(
{String image = "images/",
@required String tag,
@required String name,
@required String category,
@required String price,
@required double height,
@required double width,
@required BuildContext context}) {
return Column(children: [
Material(
child: Hero(
tag: tag,
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => InfoPage(
image: image,
tag: tag,
name: name,
category: category,
price: price,
)));
},
child: Container(
padding: EdgeInsets.all(2),
margin: EdgeInsets.all(5),
height: height,
width: width,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
image: DecorationImage(
fit: BoxFit.cover, image: AssetImage(image)),
),
),
))),
Text(name),
]);
}
推荐阅读
- angular - 如何在 tab-header-pagination 中触发下一个或上一个箭头单击的功能?我想在每个箭头单击时加载下一个或上一个 3 个选项卡
- apache-flink - Flink Logger nullPointException
- listview - 如何使 ListView 在颤动中可点击?
- core-data - 在迭代数组后通过 .sheet 显示模态时显示相同的图像
- flutter - 仅在 dart/flutter 中取十进制数
- php - 如何跟踪 Laravel 框架入口点
- javascript - 用 JS 删除 unselectable="on" ( chrome extension )
- c# - 收集动态程序集:在没有 RunAndCollect 的情况下,从一个程序集移动到多个模块和程序集的内存使用量增加了三倍
- hp-uft - hp-uft:这个应用程序错误日志说什么?
- java - 使用java lambda对列中具有不同值的值进行分组