// Copyright 2014 The Flutter 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'; /// Flutter code sample for [TreeSliver]. void main() => runApp(const TreeSliverExampleApp()); class TreeSliverExampleApp extends StatelessWidget { const TreeSliverExampleApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( home: TreeSliverExample(), ); } } class TreeSliverExample extends StatefulWidget { const TreeSliverExample({super.key}); @override State createState() => _TreeSliverExampleState(); } class _TreeSliverExampleState extends State { TreeSliverNode? _selectedNode; final TreeSliverController controller = TreeSliverController(); final List> _tree = >[ TreeSliverNode('First'), TreeSliverNode( 'Second', children: >[ TreeSliverNode( 'alpha', children: >[ TreeSliverNode('uno'), TreeSliverNode('dos'), TreeSliverNode('tres'), ], ), TreeSliverNode('beta'), TreeSliverNode('kappa'), ], ), TreeSliverNode( 'Third', expanded: true, children: >[ TreeSliverNode('gamma'), TreeSliverNode('delta'), TreeSliverNode('epsilon'), ], ), TreeSliverNode('Fourth'), ]; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('TreeSliver Demo'), ), body: CustomScrollView( slivers: [ TreeSliver( tree: _tree, controller: controller, treeNodeBuilder: ( BuildContext context, TreeSliverNode node, AnimationStyle animationStyle, ) { Widget child = GestureDetector( behavior: HitTestBehavior.translucent, onTap: () { setState(() { controller.toggleNode(node); _selectedNode = node as TreeSliverNode; }); }, child: TreeSliver.defaultTreeNodeBuilder( context, node, animationStyle, ), ); if (_selectedNode == node as TreeSliverNode) { child = ColoredBox( color: Colors.purple[100]!, child: child, ); } return child; }, ), ], ), ); } }