Json 如何在Flatter上使用my API在桌面日历上显示事件

Json 如何在Flatter上使用my API在桌面日历上显示事件,json,api,flutter,dart,android-calendar,Json,Api,Flutter,Dart,Android Calendar,我有显示事件日历的UI,我需要显示API中的事件。但我不知道怎么做。我试图更改事件列表,但没有响应。我需要在日历上显示它,这样我的公司日历才能显示事件 这是我的UI日历 import 'package:intl/intl.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:urus_flutter/persentation/custom_co

我有显示事件日历的UI,我需要显示API中的事件。但我不知道怎么做。我试图更改事件列表,但没有响应。我需要在日历上显示它,这样我的公司日历才能显示事件

这是我的UI日历

import 'package:intl/intl.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:urus_flutter/persentation/custom_color.dart';
import 'package:urus_flutter/persentation/custom_text_style.dart';
import 'package:table_calendar/table_calendar.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Calender extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => CalenderState();
}

class CalenderState extends State<Calender> {
  CalendarController _controller;
  Map<DateTime, List<dynamic>> _events;
  List<dynamic> _selectedEvents;
  DateTime _selectedDate;
  SharedPreferences prefs;


  @override
  void initState(){
    super.initState();
    _controller = CalendarController();
    _events = {
      DateTime(2021, 6, 22) : ['Meeting URUS', 'Testing Danai Mobile', 'Weekly Report', 'Weekly Meeting'],
      DateTime(2021, 6, 25) : ['Weekly Testing'],
      DateTime(2021, 6, 4) : ['Weekly Testing'],
      DateTime(2021, 6, 11) : ['Weekly Testing'],
      DateTime(2021, 6, 18) : ['Weekly Testing'],
    };
  }


  Map<String, dynamic> encodeMap(Map<DateTime, dynamic> map) {
    Map<String, dynamic> newMap = {};
    map.forEach((key, value) {
      newMap[key.toString()] = map[key];
    });
    return newMap;
  }

  Map<DateTime, dynamic> decodeMap(Map<String, dynamic> map) {
    Map<DateTime, dynamic> newMap = {};
    map.forEach((key, value) {
      newMap[DateTime.parse(key)] = map[key];
    });
    return newMap;
  }


  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          ListView(
            padding: EdgeInsets.only(left: 16, right: 16, top: 52, bottom: 126),
            children: [
              Text("Kalender Kegiatan",
                style: CustomTextStlye.proxima_bold_18_black,),
              Container(
                child: Padding(
                  padding: const EdgeInsets.only(top: 28.0),
                  child: Text("Kalender Anda",
                          style: CustomTextStlye.proxima_bold_16_black,),
                ),
              ),

              SizedBox(
                height: 20,
              ),

              Container(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(5.0),
                        boxShadow: [
                          BoxShadow(
                              offset: Offset(0, -1),
                              color: CustomColor.border_grey,
                              blurRadius: 3.0,
                              spreadRadius: 1.0)
                        ]
                      ),

                      child: TableCalendar(
                        initialCalendarFormat: CalendarFormat.month,
                        calendarStyle: CalendarStyle(
                          todayColor: Color(0x9429AAE1),
                          todayStyle: CustomTextStlye.proxima_bold_12_white,
                          selectedColor: Color(0xFF29AAE1),
                          selectedStyle: CustomTextStlye.proxima_bold_12_white,
                          weekdayStyle: CustomTextStlye.proxima_bold_12_black,
                          weekendStyle: CustomTextStlye.proxima_bold_12_red,
                          unavailableStyle: CustomTextStlye.proxima_bold_12,
                          holidayStyle: CustomTextStlye.proxima_bold_12_red,
                          markersColor: Color(0xFFA2CD3A),
                        ),
                        headerStyle: HeaderStyle(
                          centerHeaderTitle: true,
                          formatButtonVisible: false,
                          titleTextStyle: CustomTextStlye.proxima_bold_14_black,
                        ),
                        availableCalendarFormats: const {CalendarFormat.month: '',},
                        startingDayOfWeek: StartingDayOfWeek.monday,
                        calendarController: _controller,
                        events: _events,
                        onDaySelected: (date, events,holidays) {
                          setState(() {
                            _selectedEvents = events;
                            _selectedDate = date;
                          });
                        },
                      ),
                    )
                  ],
                ),
              ),

              Container(
                child:Padding(
                  padding: const EdgeInsets.only(top: 28.0),
                  child: Text("Kegiatan Anda",
                    style: CustomTextStlye.proxima_bold_16_black,),
                ),
              ),

              Container(
                child: _selectedEvents != null ? Column(
                  children: List.generate(_selectedEvents.length, (index) =>
                      Container(
                        padding: const EdgeInsets.all(8.0),
                        child: Container(
                          height: MediaQuery.of(context).size.height/15,
                          decoration: BoxDecoration(
                              border: Border(bottom: BorderSide(color: Color.fromRGBO(228, 228, 228, 1)))

                          ),
                          child:
                          Center(
                            child:
                                Container(child:
                                Row(
                                  children: [
                                    Padding(
                                      padding: const EdgeInsets.all(8.0),
                                      child: Container(
                                        padding: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 10.0),
                                        height: MediaQuery.of(context).size.height/10,
                                        decoration: BoxDecoration(
                                          border: Border.all(color:Color(0xFF29AAE1)),
                                          color:Color(0xFF29AAE1),
                                          borderRadius: BorderRadius.circular(3.0),
                                        ),
                                        child: Text(DateFormat('d').format(_selectedDate),
                                          style: CustomTextStlye.proxima_bold_18_white,
                                        ),
                                      ),
                                    ),

                                    Text(_selectedEvents[index],
                                      style: CustomTextStlye.proxima_bold_14_black,
                                    ),
                                  ],
                                ),
                                )

                          ),
                        ),
                      ),
                  ),
                ) : Container(),
              )
            ],
          ),
        ],
      ),
    );
  }
}
import'包:intl/intl.dart';
进口“包装:颤振/cupertino.dart”;
进口“包装:颤振/材料.省道”;
进口“包装:urus_flight/percentation/custom_color.dart”;
导入“包:urus_flatter/percentation/custom_text_style.dart”;
导入“package:table_calendar/table_calendar.dart”;
导入“package:shared_preferences/shared_preferences.dart”;
类日历扩展StatefulWidget{
@凌驾
State createState()=>压延状态();
}
类CalenderState扩展了状态{
日历控制器\u控制器;
地图事件;
列出所选事件;
日期时间\u选择日期;
共享引用优先权;
@凌驾
void initState(){
super.initState();
_控制器=日历控制器();
_事件={
日期时间(2021年6月22日):[“会见乌鲁斯”、“测试达奈手机”、“每周报告”、“每周会议”],
日期时间(2021年6月25日):[“每周测试”],
日期时间(2021年6月4日):[“每周测试”],
日期时间(2021年6月11日):[“每周测试”],
日期时间(2021年6月18日):[“每周测试”],
};
}
地图编码地图(地图地图){
Map newMap={};
map.forEach((键,值){
newMap[key.toString()]=map[key];
});
返回newMap;
}
地图解码地图(地图地图){
Map newMap={};
map.forEach((键,值){
newMap[DateTime.parse(key)]=map[key];
});
返回newMap;
}
小部件构建(构建上下文){
返回脚手架(
主体:堆栈(
儿童:[
列表视图(
填充:仅限边集(左:16,右:16,顶部:52,底部:126),
儿童:[
文本(“Kalender Kegiatan”,
样式:CustomTextStlye.proxima_bold_18_black,),
容器(
孩子:填充(
填充:仅限常量边集(顶部:28.0),
子:文本(“Kalender Anda”,
样式:CustomTextStlye.proxima_bold_16_black,),
),
),
大小盒子(
身高:20,
),
容器(
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
容器(
装饰:盒子装饰(
颜色:颜色,白色,
边界半径:边界半径。圆形(5.0),
boxShadow:[
箱形阴影(
偏移量:偏移量(0,-1),
颜色:CustomColor.border\u灰色,
半径:3.0,
扩展半径:1.0)
]
),
孩子:台历(
initialCalendarFormat:CalendarFormat.month,
calendarStyle:calendarStyle(
todayColor:Color(0x9429AAE1),
今日风格:CustomTextStlye.proxima\u bold\u 12\u white,
selectedColor:Color(0xFF29AAE1),
所选样式:CustomTextStlye.proxima\u粗体\u 12\u白色,
工作日样式:CustomTextStlye.proxima_bold_12_black,
周末风格:CustomTextStlye.proxima_bold_12_red,
不可用样式:CustomTextStlye.proxima\u bold\u 12,
度假风格:CustomTextStlye.proxima\u bold\u 12\u红色,
标记颜色:颜色(0xFFA2CD3A),
),
headerStyle:headerStyle(
是的,
formatButtonVisible:false,
titleTextStyle:CustomTextStlye.proxima_粗体_14_黑色,
),
availableCalendarFormats:const{CalendarFormat.month:'',},
星期一开始:星期一开始,
日历控制器:_控制器,
事件:_事件,
onDaySelected:(日期、事件、假日){
设置状态(){
_selectedEvents=事件;
_selectedDate=日期;
});
},
),
)
],
),
),
容器(
孩子:填充(
填充:仅限常量边集(顶部:28.0),
子:文本(“Kegiatan Anda”,
样式:CustomTextStlye.proxima_bold_16_black,),
),
),
容器(
子项:_selectedEvents!=null?列(
子项:List.generate(_selectedEvents.length,(index)=>
容器(
填充:常数边集全部(8.0),
子:容器(
高度:MediaQuery.of(context).size.height/15,
装饰:盒子装饰(
边框:边框(底部:边框边(颜色:color.fromRGBO(2282282282281)))
),
儿童:
居中(
儿童:
容器(子容器:
划船(
儿童:[
填充物(
填充:常数边集全部(8.0),
子:容器(
import 'dart:convert';
import 'dart:io';

import 'package:http/io_client.dart';
import 'package:urus_flutter/data/eventController.dart';
import 'package:urus_flutter/data/model/base/event_calendar.dart';

Future<List<Event_Calendar>> fetchEventCalendar(String id, int company_id, {String date}) async {
  String requestBody = '';
  print(requestBody);

  final ioc = new HttpClient();
  ioc.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
  final http = new IOClient(ioc);
  final response = await http.get(
    getStringUrl+'staffs/GetCalendar?companyid=$company_id&month=$date',
  );

  print(response.statusCode);
  print(response.body);

  if (response.statusCode == 200) {
    var parsed = jsonDecode(response.body);
    return List<Event_Calendar>.from(parsed.map((model) => Event_Calendar.fromJson(model)));
  } else {
    throw Exception(response.body);
  }
}
class Event_Calendar {
  final String id;
  final String type;
  final String date;
  final String event_name;
  final int company_id;


  Event_Calendar(
      {
        this.id,
        this.type,
        this.date,
        this.event_name,
        this.company_id,
      }
  );

  factory Event_Calendar.fromJson(Map<String, dynamic> json) {
    return Event_Calendar(
        id: json['id'] as String,
        type: json['type'] as String,
        date: json['date'] as String,
        event_name: json['event_name'] as String,
        company_id: json['company_id'] as int,
    );
  }
}