flutterlistradio-buttonmenuitemexpansion

How to put "RadioButton" or any kind of menu item list inside an "ExpansionPanelList"?


I am looking to create something like the following pic:

enter image description here

But it seems there is no proper example or tutorial on the internet, so I ask here.

I have the following code for simple ExpansionPanelList:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        // Remove the debug banner
        debugShowCheckedModeBanner: false,
        title: 'Epnasion Radio',
        theme: ThemeData(
          primarySwatch: Colors.indigo,
        ),
        home: const HomePage());
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // Generating some dummy data
  final List<Map<String, dynamic>> _items = List.generate(
      20,
      (index) => {
            'id': index,
            'title': 'Item $index',
            'description':
                'This is the description of the item $index. There is nothing important here. In fact, it is meaningless.',
            'isExpanded': false
          });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Expansion List'),
      ),
      body: SingleChildScrollView(
        child: ExpansionPanelList(
          elevation: 3,
          // Controlling the expansion behavior
          expansionCallback: (index, isExpanded) {
            setState(() {
              _items[index]['isExpanded'] = !isExpanded;
            });
          },
          animationDuration: const Duration(milliseconds: 600),
          children: _items
              .map(
                (item) => ExpansionPanel(
                  canTapOnHeader: true,
                  backgroundColor:
                      item['isExpanded'] == true ? Colors.grey : Colors.white,
                  headerBuilder: (_, isExpanded) => Container(
                      padding: const EdgeInsets.symmetric(
                          vertical: 15, horizontal: 30),
                      child: Text(
                        item['title'],
                        style: const TextStyle(fontSize: 20),
                      )),
                  body: Container(
                    padding: const EdgeInsets.symmetric(
                        vertical: 15, horizontal: 30),
                    child: Text(item['description']),
                  ),
                  isExpanded: item['isExpanded'],
                ),
              )
              .toList(),
        ),
      ),
    );
  }
}

Solution

  • This should work for you

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @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 StatefulWidget {
      final String title;
    
      const MyHomePage({
        Key? key,
        required this.title,
      }) : super(key: key);
    
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      
      List<String> chapterAAnswers = ["A","B"];
      List<String> chapterBAnswers = ["A","B","C","D"];
    
      late String selectedAnswerChapterA;
      late String selectedAnswerChapterB;
      
      @override
      void initState() {
        selectedAnswerChapterA = chapterAAnswers[0];
        selectedAnswerChapterB = chapterBAnswers[0];
        super.initState();
      }
     
     
      @override
      Widget build(BuildContext context) {
        
        
        
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body:   Column(
          children: <Widget>[
             ExpansionTile(
              title: Text('Chapter A'),
             
              children: <Widget>[
                 RadioListTile<String>(
              title: const Text('A'),
              value: chapterAAnswers[0],
              groupValue: selectedAnswerChapterA,
              onChanged: (String? value) {
                setState(() {
                  selectedAnswerChapterA = value!;
                });
              },
            ),
            RadioListTile<String>(
              title: const Text('B'),
              value: chapterAAnswers[1],
              groupValue: selectedAnswerChapterA,
              onChanged: (String? value) {
                setState(() {
                  selectedAnswerChapterA = value!;
                });
              },
            ),
              ],
            ),
               ExpansionTile(
              title: Text('Chapter B'),
             
              children: <Widget>[
                 RadioListTile<String>(
              title: const Text('A'),
              value: chapterBAnswers[0],
              groupValue: selectedAnswerChapterB,
              onChanged: (String? value) {
                setState(() {
                  selectedAnswerChapterB = value!;
                });
              },
            ),
            RadioListTile<String>(
              title: const Text('B'),
              value: chapterBAnswers[1],
              groupValue: selectedAnswerChapterB,
              onChanged: (String? value) {
                setState(() {
                  selectedAnswerChapterB = value!;
                });
              },
            ),
                 RadioListTile<String>(
              title: const Text('C'),
              value: chapterBAnswers[2],
              groupValue: selectedAnswerChapterB,
              onChanged: (String? value) {
                setState(() {
                  selectedAnswerChapterB = value!;
                });
              },
            ),
                 RadioListTile<String>(
              title: const Text('D'),
              value: chapterBAnswers[3],
              groupValue: selectedAnswerChapterB,
              onChanged: (String? value) {
                setState(() {
                  selectedAnswerChapterB = value!;
                });
              },
            ),
              ],
            ),
         
          ],
            ),
        );
      }
    }