我目前有一个ExpansionTile,其中有一个包含ListTile的ListView.builder。

我正在做的任务是将多个listTiles放在一个Expansiontile下。

这是文件列表。dart

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;
import 'dart:convert';
import 'package:emas_app/model/accounts_model.dart';


Future<String> _loadAsset() async{
  return await rootBundle.loadString('Assets/accounts.json');
}

Future<Accounts> loadAccounts() async{

  final response = await _loadAsset();
  final jsonResponse = json.decode(response);

  Accounts accounts = new Accounts.fromJson(jsonResponse);

  return accounts;
}

class ProviderList extends StatefulWidget {

  @override
  ListState createState() {
    return new ListState();
  }
}

class ListState extends State<ProviderList> {

  @override
  Widget build(BuildContext context) {

    Widget body = new FutureBuilder<Accounts>(
        future: loadAccounts(),
        builder: (context, snapshot){
          if(snapshot.hasData){
            return new ListView.builder(
                itemCount: snapshot.data.accountinfo.length,
                itemBuilder: (context,index){
                  return new Card(
                    child: new Column(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        new ExpansionTile(
                            title: new Text("Johor"),
                            children: <Widget>[
                              new ListTile(
                                title: new Text("${snapshot.data.accountinfo[index].name}"),
                              )
                            ]
                        ),
                      ],
                    ),
                  );
            });
          }else{
            return new Center(
              child: new CircularProgressIndicator(),
            );
          }
        });

    return new Scaffold(
      appBar: new AppBar(title: new Text("Providers")),
      body: body
    );
  }
}

我当前面临的主要问题是ExpansionTile对每个新的ListTile不断重复。

dart - 扩展列表下的Flutter ListTile-LMLPHP

关于如何正确放置ExpansionTile的任何想法?

最佳答案

因为你建立

Card(
                    child: new Column(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        new ExpansionTile(
                            title: new Text("Johor"),
                            children: <Widget>[
                              new ListTile(
                                title: new Text("${snapshot.data.accountinfo[index].name}"),
                              )
                            ]
                        ),
                      ],

直到= snapshot.data.accountinfo.length

尝试仅创建具有该长度的ListTile,而不创建整个卡小部件

关于dart - 扩展列表下的Flutter ListTile,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52162314/

10-10 17:49