Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

recursion - Flutter how to know Expansion Tile List depth?

I'm using this example code and it works well. But I don't know any way yet to know the level of the recursive expansion tile "tree".

The function _buildTiles() is recursive and I would like to add a "level" integer parameter to the function parameter list, but if I do that then the line "root.children.map(_buildTiles).toList()" will cause it to no longer compile.

import 'package:flutter/material.dart';

class ExpansionTileExample extends StatelessWidget {
  const ExpansionTileExample({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return 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 {
  const Entry(this.title, [this.children = const <Entry>[]]);
  final String title;
  final List<Entry> children;
}

// Data to display.
const List<Entry> data = <Entry>[
  Entry(
    'Chapter A',
    <Entry>[
      Entry(
        'Section A0',
        <Entry>[
          Entry('Item A0.1'),
          Entry('Item A0.2'),
        ],
      ),
      Entry('Section A1'),
      Entry('Section A2'),
    ],
  ),
  Entry(
    'Chapter B',
    <Entry>[
      Entry('Section B0'),
      Entry('Section B1'),
    ],
  ),
];

// 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(),  // <<---- how to add a parameter here to _buildTiles ??
    );
  }

  @override
  Widget build(BuildContext context) {
    return _buildTiles(entry);
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We could add another parameter to the _buildTiles method, that would do the trick:

Widget _buildTiles(Entry root, {int level = 0}) {
 if (root.children.isEmpty) return ListTile(title: Text(root.title));
 return ExpansionTile(
   key: PageStorageKey<Entry>(root),
   title: Text(root.title), // do something with level
   children: root.children.map(
      (child) => _buildTiles(child, level: level+1)
   ).toList(),  
 );
}

The root.children.map() function will invoke the following method for every element (named child) in children:

function (child) {
   return _buildTiles(child, level: level + 1);
}

Which we can abbreviate with the arrow denomination:

(child) => _buildTiles(child, level: level + 1)

/* is equal to :

   function (child) {
      return _buildTiles(child, level: level + 1);
   }

*/

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...