是否有可能在 gridview.builder 中有一个比其他项目更大的特定项目,并且像下面 Flutter 中的图像那样重叠它们?
我尝试添加孩子的纵横比,但它不是特定于孩子的,我无法访问索引来使用它进行函数调用。如果我尝试手动更改容器的宽度和高度,它会给我一个“RenderFlex 溢出”错误
我也尝试通过做替换特定的孩子
child: selectedIndex != index ? Dice(index: index) : Dice2(index: index)
但由于我猜是孩子的纵横比,那是不工作的艾格峰。但即使删除比率后,我仍然无法手动更改宽度或高度。
这是我的代码:
import 'dart:collection';
import 'package:bordered_text/bordered_text.dart';
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
import 'package:randomdice2/declarations/constants.dart';
import 'package:randomdice2/functions/functions.dart';
class GreenScreen extends StatefulWidget {
const GreenScreen({Key? key}) : super(key: key);
@override
State<GreenScreen> createState() => _GreenScreenState();
}
class _GreenScreenState extends State<GreenScreen> {
bool showingOriginalWidget = true;
int selectedIndex = -1;
@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
final double itemHeight = (size.height - kToolbarHeight - 24) / 2.5;
final double itemWidth = size.width / 2;
return Stack(
fit: StackFit.expand,
children: [
Lottie.asset("assets/lottie/background.json", fit: BoxFit.cover),
Column(
children: [
ElevatedButton(onPressed: () {}, child: const Text("Press me")),
Expanded(
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
mainAxisSpacing: 10,
crossAxisSpacing: 10,
childAspectRatio: (itemWidth / itemHeight),
),
itemCount: dices.length,
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
onTap: () {
setState(() {
if (selectedIndex == index) {
selectedIndex = -1;
} else {
selectedIndex = index;
}
});
},
child: Container(
height: 120, // cant set this, neither width
decoration: BoxDecoration(
color: getColor2(index),
border: Border.all(color: Colors.transparent),
borderRadius:
const BorderRadius.all(Radius.circular(4)),
boxShadow: const [
BoxShadow(
color: Colors.black,
blurRadius: 2.0,
spreadRadius: 0.0,
offset: Offset(0, 1),
)
],
),
child: Column(
children: [
Container(
height: 80,
width: 80,
color: getColor(index),
child: Image.asset(
'assets/dice/174x192/${dices.keys.elementAt(index)}',
scale: 3,
),
),
Padding(
padding: const EdgeInsets.all(3.0),
child: BorderedText(
strokeWidth: 3,
child: const Text(
"Class 10",
style: TextStyle(color: Colors.white),
),
),
),
const Padding(
padding: EdgeInsets.all(3.0),
child: ClipRRect(
borderRadius: BorderRadius.all(
Radius.circular(4),
),
child: LinearProgressIndicator(
value: 0.5,
color: Color(0xff55bef3),
backgroundColor: Color(0xff2b2b2b),
minHeight: 15,
),
),
)
],
),
),
);
},
),
),
),
ElevatedButton(
onPressed: () {
setState(() {
selectedIndex = -1;
dices = LinkedHashMap.fromEntries(
dices.entries.toList().reversed);
});
},
child: const Text("Press me")),
],
),
],
);
}
}