【发布时间】:2019-04-02 10:26:58
【问题描述】:
基本上我正在更新gridview 中使用的列表值,当我点击gridview 卡片时,我正在更改该卡片的颜色。当我打印颜色值时它确实发生了变化,但在 gridview 中没有效果。请建议我该怎么做?我的逻辑方法是错误的吗?
class FilterDialog extends ModalRoute<void> {
List<Grades> mGradeList = [];
List<Styles> mStyleList = [];
List<Types> mTypeList = [];
List<Specials> mSpecialList = [];
FilterDialog(this.mGradeList, this.mStyleList, this.mTypeList, this.mSpecialList);
@override
Widget buildPage(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
// This makes sure that text and other content follows the material style
return Material(
type: MaterialType.transparency,
// make sure that the overlay content is not cut off
child: SafeArea(
child: _buildOverlayContent(context),
),
);
}
Widget _buildOverlayContent(BuildContext context) {
return new Container(
padding: EdgeInsets.only(left: 15, right: 15),
child: Column(
children: <Widget>[
titleWidget("Grade"),
gridViewWidget(mGradeList, 3, 2.2),
spaceWidget(),
titleWidget("Style"),
gridViewWidget(mStyleList, 2, 3.5),
spaceWidget(),
titleWidget("Type"),
gridViewWidget(mTypeList, 2, 3.5),
spaceWidget(),
titleWidget("Special"),
gridViewWidget(mSpecialList, 2, 3.5),
],
)
);
}
Widget gridViewWidget(list, int count, double ratio) {
return GridView.count(
shrinkWrap: true,
crossAxisCount: count,
mainAxisSpacing: 2.0,
crossAxisSpacing: 1.0,
childAspectRatio: ratio,
physics: new NeverScrollableScrollPhysics(),
children: _getTiles(list)
);
}
void _onTileClicked(int index, value, list){
assert(index != null);
assert(value != null);
setState(() {
list[index].setSelected(!value);
});
debugPrint("You tapped on item $index with $value");
}
// Get grid tiles
List<Widget> _getTiles(list) {
final List<Widget> tiles = <Widget>[];
for (int i = 0; i < list.length; i++) {
tiles.add(new GridTile(
child: new Center(
child: Container(
height: 35.0,
child: new RaisedButton(
onPressed: () => _onTileClicked(i, list[i]["selected"], list),
color: list[i]["selected"] ? backgroundColor : white, //here value not changing
shape: new RoundedRectangleBorder(
borderRadius:
new BorderRadius.circular(30.0)),
child: new Text(
list[i]["label"],
textAlign: TextAlign.center,
style: new TextStyle(
fontSize: 14.0,
color: Colors.grey,
),
),
),
),
),
));
}
return tiles;
}
类模型:
class Grades {
String label;
bool selected;
Grades(this.label, this.selected);
void setSelected(bool val) {
this.selected = val;
}
}
一些数据:
var typeList = [{"label": "Crack", "selected": false}, {"label": "Face", "selected": false},
{"label": "Slab", "selected": false}, {"label": "Multi-pitch", "selected": false} ];
【问题讨论】:
-
你不应该在
ModalRoute中创建你的用户界面。您应该将其移至小部件 -
@RémiRousselet 我明白这一点,但是 ModalRoute 有什么办法吗? bcz 要求是在透明的全屏对话框中制作过滤视图。
-
_buildOverlayContent应该传递给构造函数而不是方法