【发布时间】:2020-02-12 09:26:49
【问题描述】:
我们有 3 个 ExpansionTiles 部分,在展开其中一个部分时,其他部分应该会折叠。很简单……但是:
在颤振文档中找不到任何有用的东西。 有谁知道如何做到这一点?
这是来自 Flutter 文档页面的示例代码。如果您单击第 A 章,B 和 C 应该折叠(如果可见),并且如果您然后单击 B 章,A 和 C 应该折叠......等等。
示例代码:
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
class ExpansionTileSample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('ExpansionTile'),
),
body: ListView.builder(
itemBuilder: (BuildContext context, int index) =>
EntryItem(data[index]),
itemCount: data.length,
),
),
);
}
}
// One entry in the multilevel list displayed by this app.
class Entry {
Entry(this.title, [this.children = const <Entry>[]]);
final String title;
final List<Entry> children;
}
// The entire multilevel list displayed by this app.
final List<Entry> data = <Entry>[
Entry(
'Chapter A',
<Entry>[
Entry('Section A1'),
Entry('Section A2'),
],
),
Entry(
'Chapter B',
<Entry>[
Entry('Section B0'),
Entry('Section B1'),
],
),
Entry(
'Chapter C',
<Entry>[
Entry('Section C0'),
Entry('Section C1'),
],
),
];
// Displays one Entry. If the entry has children then it's displayed
// with an ExpansionTile.
class EntryItem extends StatelessWidget {
const EntryItem(this.entry);
final Entry entry;
Widget _buildTiles(Entry root) {
if (root.children.isEmpty) return ListTile(title: Text(root.title));
return ExpansionTile(
key: PageStorageKey<Entry>(root),
title: Text(root.title),
children: root.children.map(_buildTiles).toList(),
);
}
@override
Widget build(BuildContext context) {
return _buildTiles(entry);
}
}
void main() {
runApp(ExpansionTileSample());
}
【问题讨论】: