作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用 GridView
显示 2 列的产品列表。右列将被向下推一点,以形成交错的 GridView 。
为此,我使用 OverflowBox
对于右栏中的产品。我得到了想要的用户界面。每个网格图 block 由 GestureDetector
包裹仅打印 'Tapped'
当用户触摸网格图 block 时。
然而,被插入的网格瓦片部分是不可触摸的(图中的红色框)。
这是我的代码片段和屏幕截图用于说明:
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]),
),
),
);
},
),
),
);
}
}
最佳答案
您可以创建具有两个 ViewPort 的行的 Scrollable Widget
import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primaryColor: Colors.orange[50],
),
home: Scaffold(
appBar: AppBar(
title: Text('All products'),
),
body: SafeArea(
child: MyHomePage(),
),
),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return ProductsGrid([
Product(
manufacturer: 'Cadbury',
title: 'Dairy Milk Chocolate Freddo',
perHundredGramm: 143,
price: 0.5,
),
Product(
manufacturer: 'Cadbury',
title: 'Caranello Koala Chocolate',
perHundredGramm: 143,
price: 0.5,
),
Product(
manufacturer: 'Mama',
title: 'Shrimp Tom Yam Noodles Jumbo Pack',
perHundredGramm: 143,
price: 0.5,
),
Product(
manufacturer: 'Mama',
title: 'Pork Noodle Jumbo Pack',
perHundredGramm: 0.62,
price: 0.72,
),
Product(
manufacturer: 'Cadbury',
title: 'Dairy Milk Chocolate Freddo',
perHundredGramm: 0.62,
price: 0.72,
),
Product(
manufacturer: 'Someone',
title: 'Something',
perHundredGramm: 0.1,
price: 1,
),
Product(
manufacturer: 'Someone',
title: 'Something',
perHundredGramm: 0.1,
price: 1,
),
Product(
manufacturer: 'Someone',
title: 'Something',
perHundredGramm: 0.1,
price: 1,
),
Product(
manufacturer: 'Someone',
title: 'Something',
perHundredGramm: 0.1,
price: 1,
),
Product(
manufacturer: 'Someone',
title: 'Something',
perHundredGramm: 0.1,
price: 1,
)
]);
}
}
class ProductsGrid extends StatelessWidget {
ProductsGrid(this.products);
final List<Product> products;
@override
Widget build(BuildContext context) {
var oddList = <Product>[];
var evenList = <Product>[];
products.asMap().forEach((key, value) {
key.isEven ? evenList.add(value) : oddList.add(value);
});
return ClipRRect(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(40),
bottomRight: Radius.circular(40),
),
child: Scrollable(
physics: ClampingScrollPhysics(),
viewportBuilder: (_, viewportOffset) => Container(
color: Theme.of(context).primaryColor,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Flexible(
flex: 1,
child: Viewport(
axisDirection: AxisDirection.down,
offset: viewportOffset,
slivers: oddList
.map((el) => SliverToBoxAdapter(
child: VerticalProductCard(el),
))
.toList(),
),
),
Flexible(
flex: 1,
child: Viewport(
axisDirection: AxisDirection.down,
offset: viewportOffset,
slivers: evenList
.asMap()
.map(
(key, el) => MapEntry(
key,
SliverToBoxAdapter(
child: Padding(
padding:
EdgeInsets.only(top: key == 0 ? 70 : 0),
child: VerticalProductCard(el),
),
),
),
)
.values
.toList(),
),
),
],
),
),
));
}
}
@immutable
class Product {
final double price;
final String manufacturer;
final String title;
final double perHundredGramm;
Product({
@required this.price,
@required this.manufacturer,
@required this.title,
@required this.perHundredGramm,
});
}
class VerticalProductCard extends StatefulWidget {
const VerticalProductCard(this.product, {Key key}) : super(key: key);
final Product product;
@override
_VerticalProductCardState createState() => _VerticalProductCardState();
}
class _VerticalProductCardState extends State<VerticalProductCard> {
bool isFavorite = false;
_onTap() {
setState(() {
isFavorite = !isFavorite;
});
}
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
),
padding: EdgeInsets.all(10),
margin: EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(
'coles',
style: TextStyle(color: Colors.red),
textAlign: TextAlign.center,
),
Center(
child: Container(
width: 50,
child: Placeholder(
fallbackHeight: 50,
fallbackWidth: 30,
),
),
),
Text('\$ ${widget.product.price}'),
Text(widget.product.manufacturer),
Text(widget.product.title),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'\$${widget.product.perHundredGramm} per 100G',
style: TextStyle(fontSize: 12),
),
IconButton(
icon: Icon(
Icons.favorite,
color: isFavorite ? Colors.redAccent : Colors.grey,
),
onPressed: _onTap,
)
],
)
],
),
);
}
}
关于flutter - GestureDetector 在带有溢出框的交错 GridView 中是不可触及的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63189609/
我正在尝试创建一个 React 组件来抽象化为我的表单创建一个输入组。所有输入都具有相同的布局 - 一个标签,下面是输入,如果存在错误/信息文本,它们将显示在输入下。 以前我处理自己的表单状态/处理程
我正在尝试将子 shell 的输出行读取到数组中,但我不愿意设置 IFS,因为它是全局的。我不希望脚本的一部分影响后面的部分,因为那是不好的做法,我拒绝这样做。在命令后恢复 IFS 不是一个选项,因为
我是一名优秀的程序员,十分优秀!