【发布时间】:2020-11-21 04:03:34
【问题描述】:
我使用 GridView 来显示 2 列的产品列表。右列将被向下推一点,以形成交错的网格视图。
为此,我将OverflowBox 用于右栏中的产品。我得到了想要的用户界面。每个网格图块都由GestureDetector 包裹,当用户触摸网格图块时,它只会打印'Tapped'。
但是,被推入的网格瓦片部分是不可触摸的(图中红色框)。
这是我的代码 sn-p 和截图用于说明:
import 'package:flutter/material.dart';
import '../models/products.dart';
import './vertical_product_card.dart';
class ProductsGrid extends StatelessWidget {
final List<Product> products;
ProductsGrid(this.products);
@override
Widget build(BuildContext context) {
final screenWidth = MediaQuery.of(context).size.width;
return ClipRRect(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(40),
bottomRight: Radius.circular(40),
),
child: Container(
color: Theme.of(context).primaryColor,
child: GridView.builder(
shrinkWrap: true,
padding:
const EdgeInsets.only(top: 10, bottom: 50, left: 15, right: 15),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: (screenWidth - 30 - 15) / (2 * 290),
mainAxisSpacing: 15,
crossAxisSpacing: 15,
// Product Card has fixed height 290, screenWidth must substract the total horizontal padding of GridView
),
itemCount: products.length,
itemBuilder: (_, i) {
if (i % 2 == 0) {
return GestureDetector(
onTap: () {
print('Tapped');
},
child: VerticalProductCard(products[i]),
);
}
return GestureDetector(
onTap: () {
print('Tapped');
},
child: OverflowBox(
maxHeight: 290.0 + 70.0,
child: Container(
margin: EdgeInsets.only(top: 70),
child: VerticalProductCard(products[i]),
),
),
);
},
),
),
);
}
}
【问题讨论】:
-
我不是 100% 确定,但我认为溢出部分不是小部件的一部分。尝试交换
OverflowBox和GestureDetector,以便GestureDetector包裹容器。 -
我第一次这样做,但没有成功
标签: flutter dart flutter-layout