gpt4 book ai didi

dart - ExpansionTile 不保持状态

转载 作者:IT王子 更新时间:2023-10-29 06:39:03 33 4
gpt4 key购买 nike

以下问题:我有一个列表 ExpansionTiles效果很好。我面临的唯一问题是,滚动到 View 之外的展开的 ExpansionTile 在再次滚动到 View 后将不再展开。这会导致不良的用户体验以及一种“跳跃式”滚动。

文档说明如下:

When used with scrolling widgets like ListView, a unique key must be specified to enable the ExpansionTile to save and restore its expanded state when it is scrolled in and out of view.

虽然这不起作用。到目前为止,我还没有找到任何方法来完成这项工作。
这是到目前为止的代码:

import 'package:flutter/material.dart';

void main() {
runApp(new MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'ExpansionTile Test',
home: new MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
List<Widget> _getChildren() {
List<Widget> elements = [];
for (int i = 0; i < 20; i++) {
elements.add(new ListChild());
}
return elements;
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('ExpansionTile Test'),
),
body: new ListView(
children: _getChildren(),
),
);
}
}

class ListChild extends StatefulWidget {
@override
State createState() => new ListChildState();
}

class ListChildState extends State<ListChild> {
GlobalKey<ListChildState> _key = new GlobalKey();
@override
Widget build(BuildContext context) {
return new ExpansionTile(
key: _key,
title: const Text('Test Tile'),
children: <Widget>[
const Text('body'),
],
);
}
}

最佳答案

使用 PageStorageKey 而不是 GlobalKey

关于dart - ExpansionTile 不保持状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44803911/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com