flutter - 如何在颤动中更新 SilverGrid 或 SilverList 的项目?Flutter中是否有与notifyDatasetChanged()等价的东西?
问题描述
使用Emily Fortuna 在 Medium 文章“Slivers, Demystified”中提供的示例代码,我使用SilverList和SilverGrid类创建了一个 Flutter 列表。但是创建列表后我无法更改数据。
import 'package:flutter/material.dart';
import 'dart:math' as math;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Collapsing List Demo')),
body: CollapsingList(),
),
);
}
}
class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
_SliverAppBarDelegate({
@required this.minHeight,
@required this.maxHeight,
@required this.child,
});
final double minHeight;
final double maxHeight;
final Widget child;
@override
double get minExtent => minHeight;
@override
double get maxExtent => math.max(maxHeight, minHeight);
@override
Widget build(
BuildContext context,
double shrinkOffset,
bool overlapsContent)
{
return new SizedBox.expand(child: child);
}
@override
bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
return maxHeight != oldDelegate.maxHeight ||
minHeight != oldDelegate.minHeight ||
child != oldDelegate.child;
}
}
class CollapsingList extends StatelessWidget {
SliverPersistentHeader makeHeader(String headerText) {
return SliverPersistentHeader(
pinned: true,
delegate: _SliverAppBarDelegate(
minHeight: 60.0,
maxHeight: 200.0,
child: Container(
color: Colors.lightBlue, child: Center(child:
Text(headerText))),
),
);
}
@override
Widget build(BuildContext context) {
return CustomScrollView(
slivers: <Widget>[
makeHeader('Header Section 1'),
SliverGrid.count(
crossAxisCount: 3,
children: [
Container(color: Colors.red, height: 150.0),
Container(color: Colors.purple, height: 150.0),
Container(color: Colors.green, height: 150.0),
Container(color: Colors.orange, height: 150.0),
Container(color: Colors.yellow, height: 150.0),
Container(color: Colors.pink, height: 150.0),
Container(color: Colors.cyan, height: 150.0),
Container(color: Colors.indigo, height: 150.0),
Container(color: Colors.blue, height: 150.0),
],
),
makeHeader('Header Section 2'),
SliverFixedExtentList(
itemExtent: 150.0,
delegate: SliverChildListDelegate(
[
Container(color: Colors.red),
Container(color: Colors.purple),
Container(color: Colors.green),
Container(color: Colors.orange),
Container(color: Colors.yellow),
],
),
),
makeHeader('Header Section 3'),
SliverGrid(
gridDelegate:
new SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 200.0,
mainAxisSpacing: 10.0,
crossAxisSpacing: 10.0,
childAspectRatio: 4.0,
),
delegate: new SliverChildBuilderDelegate(
(BuildContext context, int index) {
return new Container(
alignment: Alignment.center,
color: Colors.teal[100 * (index % 9)],
child: new Text('grid item $index'),
);
},
childCount: 20,
),
),
makeHeader('Header Section 4'),
// Yes, this could also be a SliverFixedExtentList. Writing
// this way just for an example of SliverList construction.
SliverList(
delegate: SliverChildListDelegate(
[
Container(color: Colors.pink, height: 150.0),
Container(color: Colors.cyan, height: 150.0),
Container(color: Colors.indigo, height: 150.0),
Container(color: Colors.blue, height: 150.0),
],
),
),
],
);
}
}
解决方案
也许是因为你把它提取出来作为一个例子在这里展示,但现在里面的一切CollapsingList
都是固定的——就像没有可以改变的变量一样,所以内容也可以改变。基本上有很多不同的方法来实现不断变化的数据的维护和相应的 UI 的变化(状态管理解决方案)。
Flutter 环境中更新 UI 最基本的setState((){})
方法是StatefulWidgets
. CollapsingList
因此,作为一个示例,您可以StatefulWidget
设置一些变量,这些变量应该在任何交互(如单击按钮)后更改并调用setState((){})
,因此StatefulWidget
在这种情况下,您CollapsingList
将重建并且变量的新值将被显示/使用。
推荐阅读
- r - 如何从同一页面上的for循环绘制多个ggscatter图
- json - 质量管理体系 | ListModel 中的持久内存(存储 ListModel 供以后使用)
- vb.net - 如何在不插入零的情况下将空白 DGV 列默认为数字...相当于字符串的“”?
- mongodb - 得到“MongoError: $or must be an array”,即使它有两个表达式?
- java - 在包上使用 @ParametersAreNonnullByDefault 注释时,IntelliJ 可以自动生成代码以检查参数
- python - 我不明白 screen.fill() 是如何工作的
- javascript - 为什么 IF 语句会影响对象是否被定义?
- python - python3中'from x import variable'的作用是什么
- c++ - 编译器决定不明确的参数
- .net - 使用 Shell.Application 在 zip 文件中为组织创建子文件夹