Created
January 23, 2022 15:27
-
-
Save osaxma/6802f0f362f0c408b149715ce3e8590b to your computer and use it in GitHub Desktop.
nested topic example for SO https://stackoverflow.com/q/70823362/10976714
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const NestedTopicsExample()); | |
} | |
class NestedTopicsExample extends StatelessWidget { | |
const NestedTopicsExample({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
final String title; | |
const MyHomePage({Key? key, required this.title}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(title), | |
), | |
body: const Center(child: NestedTopicWidget()), | |
); | |
} | |
} | |
class NestedTopicWidget extends StatelessWidget { | |
const NestedTopicWidget({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return ListView.builder( | |
itemCount: topics.length, | |
itemBuilder: (context, index) { | |
return TopicWidget(topic: topics[index]); | |
}, | |
); | |
} | |
} | |
class TopicWidget extends StatelessWidget { | |
final Topic topic; | |
const TopicWidget({Key? key, required this.topic}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return ExpansionTile( | |
title: Text(topic.text), | |
children: List.generate(topic.subTopics.length, (index) => TopicWidget(topic: topic.subTopics[index])), | |
); | |
} | |
} | |
final topics = <Topic>[ | |
// topic 1 | |
Topic( | |
text: 'Topic 1', | |
subTopics: [ | |
Topic(text: 'Sub Topic 1'), | |
Topic( | |
text: 'Sub Topic 2', | |
subTopics: [ | |
Topic( | |
text: 'sub sub topic 1', | |
), | |
], | |
), | |
Topic(text: 'Sub Topic 3'), | |
], | |
), | |
// topic 2 | |
Topic( | |
text: 'Topic 2', | |
subTopics: [ | |
Topic( | |
text: 'Sub Topic 1', | |
), | |
Topic( | |
text: 'Sub Topic 2', | |
), | |
], | |
), | |
// topic 3 | |
Topic( | |
text: 'Topic 3', | |
) | |
]; | |
class Topic { | |
final String text; | |
final List<Topic> subTopics = []; | |
Topic({required this.text, List<Topic> subTopics = const []}) { | |
this.subTopics.addAll(subTopics); | |
} | |
} | |
List<String> l = [ | |
"Topic 1", | |
" Sub Topic 1", | |
" Sub Topic 2", | |
" SubSub Topic 1", | |
" Sub Topic 3", | |
"Topic 2", | |
" Sub Topic 1", | |
" Sub Topic 2", | |
"Topic 3", | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment