Home > Software engineering >  Trying to build Listview but I can't add any method to the Expansion tile
Trying to build Listview but I can't add any method to the Expansion tile

Time:06-29

I have the following code to build a listview from local JSON file and it works perfectly fine. However, when I try to add a method such as onTap: (){} to the ExpansionTile in the _buildList Widget I got the following error

Error: No named parameter with the name 'onTap'. onTap: (){}, ^^^^^ /C:/src/flutter/packages/flutter/lib/src/material/expansion_tile.dart:51:9: Context: Found this candidate, but the arguments don't match. const ExpansionTile({ ^^^^^^^^^^^^^

The code in Main.dart is

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'datamodel.dart';

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

class MyApp extends StatefulWidget {
  @override
  State<MyApp> createState() => _MyAppState();
}


class _MyAppState extends State<MyApp> {

    Future<List<Menu>> ReadJsonData() async {
    //read json file
    final jsondata = await rootBundle.loadString('assets/data0.json');
    //decode json data as list
    final list = json.decode(jsondata) as List<dynamic>;
    //map json and initialize using Model
    return list.map((e) => Menu.fromJson(e)).toList();
  }
  @override
  void initState() {
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home:Scaffold(
            appBar: AppBar(
              title: const Text('My Title'),
            ),
        body: FutureBuilder(
          future: ReadJsonData(),
          builder: (context,data){
            if(data.hasError){
              return Center(child: Text("${data.error}"));
            }else if(data.hasData){
              var items =data.data as List<Menu>;
              return ListView.builder(
                itemCount: items.length,
                itemBuilder: (BuildContext context, int index) =>
                    _buildList(items[index]),
              );
            }else{
              return Center(child: CircularProgressIndicator(),);
            }
          },
        )
    )
    );
  }

    Widget _buildList(Menu list) {
    return ExpansionTile(
      leading: Icon(list.icon),
      // line causing error
      onTap: (){},
      title: Text(
        list.name!,// I added !
        style: TextStyle(fontSize: list.font?.toDouble(), fontWeight: FontWeight.bold),
      ),
      children: list.subMenu!.map(_buildList).toList(),// I added !
    );
  }
}

So is there any way to add the Method for each Expansion tile?

Thank you in advance!

CodePudding user response:

ExpansionTile does not have an onTap property. This is because it has a default behaviour on tapping, expands or collapses.

If you'd like to execute some specific logic on expanded or collapsed, you can use onExpansionChanged:

return ExpansionTile(
  onExpansionChanged: (bool expanded) {
    // do what you want
  },
);
  • Related