扩展列表下的Flutter ListTile

时间:2018-09-04 08:46:34

标签: dart flutter

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

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

这是文件 list.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重复。

Repeating "Johor" for each new ListTile

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

1 个答案:

答案 0 :(得分:0)

因为您构建

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,而不创建整个卡片小部件