【发布时间】:2021-03-06 01:09:52
【问题描述】:
我是 Flutter 的初学者。我想用按钮(自定义按钮小部件)创建一个 8x2 网格视图。但是当 GridView 中的项目离开视口时,它们会丢失其状态。我也尝试过 SilverGrid。也有相同的问题。这是我的代码 sn-p .当我向下滚动并返回到 SoundCard 顶部时,SoundCard 的选定状态将丢失。我附上示例图片here。
import 'package:flutter/material.dart';
import 'package:relax/constants.dart';
import 'package:relax/soundcard.dart';
class Index extends StatefulWidget {
@override
_IndexState createState() => _IndexState();
}
class _IndexState extends State<Index> {
bool playState = false;
@override
Widget build(BuildContext context) {
return Container(
color: Color(0xFF55b9f3),
child: GridView.count(
primary: false,
padding: const EdgeInsets.all(20),
crossAxisSpacing: 40,
mainAxisSpacing: 40,
crossAxisCount: 2,
children: <Widget>[
SoundCard(
assetName: 'rain',
),
SoundCard(
assetName: 'summernight',
),
SoundCard(
assetName: 'water',
),
SoundCard(
assetName: 'wind',
),
SoundCard(
assetName: 'thunderstorm',
),
SoundCard(
assetName: 'forest',
),
SoundCard(
assetName: 'leaves',
),
SoundCard(
assetName: 'fireplace',
),
SoundCard(
assetName: 'waterstream',
),
SoundCard(
assetName: 'seaside',
),
SoundCard(
assetName: 'brownnoise',
),
SoundCard(
assetName: 'coffeeshop',
),
SoundCard(
assetName: 'fan',
),
SoundCard(
assetName: 'pinknoise',
),
SoundCard(
assetName: 'whitenoise',
),
SoundCard(
assetName: 'train',
),
],
)
);
}
}
SoundCard 就像按钮。当我滚动并返回顶部时,SoundCard 按钮的选定状态将失去其选定状态。有人可以帮我解决这个问题吗?
【问题讨论】:
-
你能告诉我们代码,当你按下按钮时它被选中了吗?
-
谢谢!,我通过在我的声卡小部件中添加 `AutomaticKeepAliveClientMixin` 解决了这个问题
-
@ARUNBALAJI 请添加您的解决方案作为答案并接受它,以便有类似问题的人可以找到它。
标签: flutter gridview widget flutter-layout flutter-animation