支付宝赞助帐号:service@kuitao8.com 

flutter下拉展开

Created2020-03-19   Views 482    Author 懒人程序
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'ExpansionTile Test',
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Future<http.Response> _responseFuture;

  @override
  void initState() {
    super.initState();
    _responseFuture = http.get('https://sdkapi.zhaodaima.net/api/v1/get_year');
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('ExpansionTile Test'),
      ),
      body: new FutureBuilder(
        future: _responseFuture,
        builder: (BuildContext context, AsyncSnapshot<http.Response> response) {
          if (!response.hasData) {
            return const Center(
              child: const Text('Loading...'),
            );
          } else if (response.data.statusCode != 200) {
            return const Center(
              child: const Text('Error loading data'),
            );
          } else {
            List<dynamic> json2 = json.decode(response.data.body);
            return new MyExpansionTileList(json2);
          }
        },
      ),
    );
  }
}

class MyExpansionTileList extends StatelessWidget {
  final List<dynamic> elementList;

  MyExpansionTileList(this.elementList);

  List<Widget> _getChildren() {
    List<Widget> children = [];
    elementList.forEach((element) {
      children.add(
        new MyExpansionTile(element['year'], element['money']),
      );
    });
    return children;
  }

  @override
  Widget build(BuildContext context) {
    return new ListView(
      children: _getChildren(),
    );
  }
}

class MyExpansionTile extends StatefulWidget {
  final String did;
  final String name;
  MyExpansionTile(this.did, this.name);
  @override
  State createState() => new MyExpansionTileState();
}

class MyExpansionTileState extends State<MyExpansionTile> {
  PageStorageKey _key;
  Future<http.Response> _responseFuture;

  @override
  void initState() {
    super.initState();
    _responseFuture =
        http.get('https://sdkapi.zhaodaima.net/api/v1/get_month?year=${widget.did}');
  }

  @override
  Widget build(BuildContext context) {
    _key = new PageStorageKey('${widget.did}');
    return new ExpansionTile(
      key: _key,
     // title: new Text(widget.did),
     title: Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: <Widget>[
                              Expanded(
                                child: Container(

                                  child: Text("${widget.did}年"),
                                ),
                                flex: 1,
                              ),
                              Expanded(
                                child: Container(

                                  child: Text("总收入"),
                                ),
                                flex: 1,
                              ),
                               Expanded(
                                child: Container(

                                  child: Text("${widget.name}元"),
                                ),
                                flex: 1,
                              ),
                            ],
                          ),
      children: <Widget>[
        new FutureBuilder(
          future: _responseFuture,
          builder:
              (BuildContext context, AsyncSnapshot<http.Response> response) {
            if (!response.hasData) {
              return const Center(
                child: const Text('Loading...'),
              );
            } else if (response.data.statusCode != 200) {
              return const Center(
                child: const Text('Error loading data'),
              );
            } else {
              List<dynamic> json2 = json.decode(response.data.body);
              print(json2);
              List<Widget> reasonList = [];
              json2.forEach((element) {
                reasonList.add(new ListTile(
                  dense: true,
                  title: Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: <Widget>[
                              Expanded(
                                child: Container(

                                  child: Text("${element['month']}月", textAlign: TextAlign.left),
                                 // alignment: AlignmentDirectional.center,
                                ),
                                flex: 1,

                              ),

                               Expanded(
                                child: Container(

                                  child: Text("${element['money']}元", textAlign: TextAlign.right),
                                  padding: EdgeInsets.only(right:30.0),
                                ),
                                flex: 1,
                              ),
                            ],
                          ),
                ));
              });
              return new Column(children: reasonList);
            }
          },
        )
      ],
    );
  }
}
上一篇: flutter http post基础用法
下一篇: flutter post删除方法
支持键盘 ← →

邮件订阅

订阅我们的精彩内容