如何在 flutter 中将列表添加到 End drawer

How to add a list to End drawer in flutter

我正在尝试制作一个应用程序,我想在其中使用我已经完成的网络调用来显示一些日期。现在我想提前在我的 endDrawer.Thanks 中显示那个日期列表。

这是我的代码:

    Future<List> addDateList(int counter,String date) async {
    DateTime oops = DateTime.now();
    String date = DateFormat('dd/MM/yyyy').format(oops);
    List<String> dateList = List();

    http.Response response = await http.get(
        'https://www.example.com/json/' + date + '-17.json');

    if (response.statusCode == 200) {
      dateList.add(date);
    } else {
      date = getpreviousdate(date);
      return addDateList(counter,date);
    }
    if (dateList.length == 7) {
      print(dateList);

      return dateList;
    } else {
      date = getpreviousdate(date);
      return addDateList(++counter, (date));
    }
  }

  getpreviousdate(String date) {
    DateTime pastday = DateTime.now().subtract(Duration(days: 1));
    date = DateFormat('dd/MM/yyyy').format(pastday);

    return date;
  }

因此,如果您只想从收集的日期字符串创建一个 ListView,您可以这样做:

@immutable
class DrawerView extends StatelessWidget {
  const DrawerView({
    @required this.items,
  }) : assert(items != null);

  final List<String> items;

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        // Important: Remove any padding from the ListView.
        padding: EdgeInsets.zero,
        children: items.map<Widget>((String item) => ListTile(title: Text(item))),
      ),
    );
  }
}

我更喜欢为这样的事情创建一个新的class。


编辑

好的,你可以试试这个(我没有运行它,因为我没有这样的API来测试它,但它应该工作得很好):

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:http/http.dart' as http;

void main() => runApp(MyApp());

/// [MyApp] class - where it all begins! :D
class MyApp extends StatefulWidget {
  MyApp({Key key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  int _counter = 0;
  List<String> _dateList = <String>[];

  @override
  void initState() {
    super.initState();
    addDateList();
  }

  Future addDateList({ DateTime date }) async {
    final DateTime d = date ?? DateTime.now();
    String dateString = DateFormat('dd/MM/yyyy').format(d);

    http.Response response = await http.get('https://www.example.com/json/$dateString-17.json');

    if (response.statusCode == 200) {
      setState(() {
        _dateList.add(dateString);
        _counter++;
      });
    }

    // if you reach the desired length or the counter gets too high break the execution
    if (_dateList.length >= 7 || _counter > 50) return true;

    setState(() {
      _counter++;
    });

    return addDateList(date: d.subtract(Duration(days: 1)));
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Drawer Demo',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('HOME'),
        ),
        endDrawer: MyDrawer(items: _dateList),
        body: Center(
          child: const Text('DRAWER WITH LISTVIEW - DEMO'),
        ),
      ),
    );
  }
}

class MyDrawer extends StatelessWidget {
  const MyDrawer({
    @required this.items,
  }) : assert(items != null);

  final List<String> items;

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        // Important: Remove any padding from the ListView.
        padding: EdgeInsets.zero,
        children: items.map<Widget>((String item) => ListTile(title: Text(item))).toList(),
      ),
    );
  }
}