如何在groupBy中使用ListString?颤抖

时间:2020-10-31 09:48:35

标签: flutter

您好,我尝试显示按日期分组的数据列表。

首先,我加载所有使用sharedpreference保存的键/值 在我使用正则表达式为每个日期匹配数据键和数据值之后 在我使用列表添加所有日期/数据/值之后:

 List<String>  MylistString=[{"release_date":"2020-10-30","note3":"2222222"}, {"release_date":"2020-10-29","note3":"1111111"}, {"release_date":"2020-10-31","note3":"3333333"}, {"release_date":"2020-10-29","note4":"bdlbd"}] 

即使添加.toString之后,我仍未成功将groupBy与MyListString一起使用

var foo = groupBy({MylistString.toString()}, (i) => i["release_date"]); 

但是,如果我用相同的数据创建一个静态字符串,它就会起作用

以下是完整的代码,它们显示每个日期的键值列表,我搜索按日期对数据进行分组:

import 'package:flutter/material.dart';
import 'package:flutter_timeline/flutter_timeline.dart';
import 'package:flutter_timeline/indicator_position.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';


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

class TimelineDemoApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Timeline',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: PlainTimelineDemoScreen(),
    );
  }
}

class PlainTimelineDemoScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _PlainTimelineDemoScreenState();
}

class _PlainTimelineDemoScreenState extends State<PlainTimelineDemoScreen> {
  List<EventData> eventsData;
  List<TimelineEventDisplay> events;

  String Data1;
  String Data2;

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


  List<String> stringList_note_formatted = [];

  List<String> stringList_date_note1 =[] ;
  String value_note1;

  List<String> stringList_date_note2 =[] ;
  String value_note2;

  load_data() async {

//---------------------------------------------------------------------------------------------------

    var input = '"2020-10-30","note1":"value1","2020-10-30","note2":"value2","2020-10-31","note1":"value3","2020-10-32","note2":"value4",';



    var inputItarable = input.split(',').where((s) => s.isNotEmpty);
    var i = inputItarable.iterator;
    var tmp = {};
    while (i.moveNext()) {
      var key = i.current; i.moveNext();
      var value = i.current.split(':'); (tmp[key] ??= []).add(value); } print(tmp);
    var output = tmp.keys.map((key) {
      var map = {}; map['"release_date"'] = key; tmp[key].forEach((e) => map[e[0]] = e[1]);
      return map; }).toList();
    print(output);


    if (output == null) {
      //no data available
      eventsData = [];
    } else {
      //deserialize event data from json
      var items = jsonDecode(output) as List;
      eventsData = items.map((i) => EventData.fromJson(i)).toList();
    }
    //render event data
    events = eventsData.map(plainEventDisplay).toList();
  }
  final myController2 = TextEditingController();
  final myController3 = TextEditingController();


  String note2;

  dialog_text() async{

    await showDialog<String>(
        barrierDismissible: false, // user must tap button!

        context: context,
        builder: (BuildContext context){
          return AlertDialog(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(20.0))),
            title:    Text(("Write something"),textAlign: TextAlign.center, style: TextStyle(color: Colors.black, fontWeight: FontWeight.w600, fontSize: 20)),
            content:

            Container(
              width: MediaQuery.of(context).size.width*1,

              child:
              new ListView(
                children: <Widget>[
                  Card(
                      color: Colors.grey[200],
                      child: Padding(
                        padding: EdgeInsets.all(8.0),
                        child: TextField(
                          controller: myController2,
                          maxLines:2,
                          decoration: InputDecoration.collapsed(hintText: "Your text"),
                        ),
                      )

                  ),
                ],
              ),
            ),



            actions: <Widget>[


              FlatButton(
                onPressed: () {
                  Navigator.pop(context,);
                },
                child:  Text('Validate', textAlign:TextAlign.center,style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize:15),
                ),
              ),
            ],
          );
        }
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Timeline test"),
      ),
      body: FutureBuilder(
        future: load_data(),
        builder: (context, snapshot) {
          return _buildTimeline();
        },
      ),

    );
  }

  TimelineEventDisplay plainEventDisplay(EventData eventData) {
    var formattedTime = DateFormat('dd-MM-yyyy').format(eventData.dateTime);

    Widget text1() {
      if (eventData.data1==null){
        return       Container();
      }else{
        return       Text("note1 : ${eventData.data1}");
      }
    }

    Widget text2() {
      if (eventData.data2==null){
        return       Container();
      }else{
        return       Text("note2 : ${eventData.data2}");
      }
    }
    return TimelineEventDisplay(

      anchor: IndicatorPosition.top,
      indicatorOffset: Offset(0, 0),
      child: TimelineEventCard(
          title: Text("${formattedTime}"),
          content: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              new Row(
                children: [
                  new Expanded(
                      child : new Card(
                        elevation: 3,
                        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
                        child:  new Container(
                          padding: const EdgeInsets.all (10.0),
                          child: Column(
                            children: [
                              text1(),
                              Padding(
                                padding: EdgeInsets.all(5.0),),
                              text2(),
                            ],
                          ),
                        ),
                      )
                  )
                ],
              )
            ],
          )),
      indicator: TimelineDots.of(context).circleIcon,
    );
  }

  Widget _buildTimeline() {
    return TimelineTheme(
        data: TimelineThemeData(
            lineColor: Colors.blueAccent, itemGap: 5, lineGap: 0),
        child: Timeline(
          anchor: IndicatorPosition.center,
          indicatorSize: 56,
          altOffset: Offset(10, 10),
          events: events,
        ));
  }

}

class EventData {
  final DateTime dateTime;
  final String data1;
  final String data2;

  EventData(this.dateTime, this.data1, this.data2);

  EventData.fromJson(Map<String, dynamic> json)
      : dateTime = DateTime.parse(json['release_date']),
        data1 = json['note1'],
        data2 = json['note2'];

  Map<String, dynamic> toJson() => {
    'release_date': dateTime.toIso8601String(),
    'note1': data1,
    'note2': data2,
  };
}

0 个答案:

没有答案