我怎样才能使类别和子类别按钮颤抖

时间:2020-02-15 16:52:55

标签: user-interface flutter button dart

当我按下按钮时,如何才能使之扑朔迷离?进行交流,将展开并显示几个子类别按钮,将其他主按钮向下推,如下面的照片 enter image description here

1 个答案:

答案 0 :(得分:0)

使用 ExpansionTile

ExpansionTiles可用于生成两级或多级列表

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 A0',
    <Entry>[
      Entry('Item A0.1'),
      Entry('Item A0.2'),
      Entry('Item A0.3'),
    ],
  ),
  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'),
  Entry(
    'Section C2',
    <Entry>[
      Entry('Item C2.0'),
      Entry('Item C2.1'),
      Entry('Item C2.2'),
      Entry('Item C2.3'),
    ],
  ),
],
  ),
];

// 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());
}