Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // import 'package:date_utils/date_utils.dart';
- import 'package:firecek/modules/home/user_pro.dart';
- import 'package:firecek/modules/schedule/schedule_create.dart';
- import 'package:firecek/modules/schedule/schedule_reschedule.dart';
- import 'package:flutter/material.dart';
- // import 'package:intl/date_symbol_data_local.dart';
- import 'package:table_calendar/table_calendar.dart';
- import 'schedule_modal.dart';
- import 'package:firecek/database/modal.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- import 'package:http/http.dart' as http;
- import 'dart:convert';
- import 'package:after_layout/after_layout.dart';
- Future<ScheduleResponse> fetchSchedule(http.Client client) async {
- SharedPreferences prefs = await SharedPreferences.getInstance();
- String kodeUser = prefs.getString('kodeUser');
- String kodeCustomer = prefs.getString('kodeCustomer');
- String token = prefs.getString('apiKey');
- final response = await client.get(Modal.schedule +
- '?kode_user=$kodeUser&token=$token&kode_customer=$kodeCustomer&datetime=${DateTime.now()}');
- final responseDecode = jsonDecode(response.body);
- ScheduleResponse data = ScheduleResponse.fromJson(responseDecode);
- return data;
- }
- Future<ScheduleCreateDeleteUpdateResponse> post(String url, var body) async {
- return await http.post(Uri.encodeFull(url),
- body: body,
- headers: {'Accept': 'application/json'}).then((http.Response response) {
- final int statusCode = response.statusCode;
- if (statusCode < 200 || statusCode > 400 || json == null) {
- throw new Exception("Error while fetching data");
- }
- return ScheduleCreateDeleteUpdateResponse.fromJson(json.decode(response.body));
- });
- }
- Future<SelectedScheduleResponse> postSelectedSchedule(String url, var body) async {
- return await http.post(Uri.encodeFull(url),
- body: body,
- headers: {'Accept': 'application/json'}).then((http.Response response) {
- final int statusCode = response.statusCode;
- if (statusCode < 200 || statusCode > 400 || json == null) {
- throw new Exception("Error while fetching data");
- }
- return SelectedScheduleResponse.fromJson(json.decode(response.body));
- });
- }
- class Schedule extends StatefulWidget {
- final msg;
- Schedule({this.msg});
- @override
- State<StatefulWidget> createState() => _ScheduleState();
- }
- class _ScheduleState extends State<Schedule> with AfterLayoutMixin<Schedule>{
- DateTime _selectedDay;
- Map<DateTime, List> _events;
- Map<DateTime, List> _getEvents;
- Map<DateTime, List> _visibleEvents;
- DateTime _selectedDate;
- SelectedScheduleResponse _selectedDateData;
- bool _selectedDateApiCall = false;
- final GlobalKey<ScaffoldState> _scheduleScaffoldKey = GlobalKey<ScaffoldState>();
- @override
- void initState(){
- _selectedDay = DateTime.now();
- _events = {};
- _getEvents = {};
- _visibleEvents = _events;
- super.initState();
- }
- @override
- void afterFirstLayout(BuildContext context) {
- setState(() {
- _events = _getEvents;
- _visibleEvents = _events;
- });
- if (widget.msg != null) {
- _scheduleScaffoldKey.currentState.showSnackBar(
- SnackBar(
- content: Text(widget.msg),
- )
- );
- }
- }
- Future _deleteSchedule(String noJadwal) async {
- SharedPreferences prefs = await SharedPreferences.getInstance();
- String kodeUser = prefs.getString('kodeUser');
- String token = prefs.getString('apiKey');
- post(Modal.scheduleDelete, {
- 'kode_user': kodeUser,
- 'token': token,
- 'no_jadwal': noJadwal
- }).then((response){
- print(response.msg);
- if (response.msgCode == 200) {
- Navigator.push(context, MaterialPageRoute(
- builder: (context) => Schedule(msg: 'Agenda dihapus')
- ));
- }
- },onError: (error){
- print(error.toString());
- });
- }
- Future _getScheduleOnSelectedDate(DateTime datetime) async {
- SharedPreferences prefs = await SharedPreferences.getInstance();
- String kodeUser = prefs.getString('kodeUser');
- String token = prefs.getString('apiKey');
- String kodeCustomer = prefs.getString('kodeCustomer');
- // EventList(mode: 'selectedDate');
- postSelectedSchedule(Modal.schedule, {
- 'kode_user': kodeUser,
- 'token': token,
- 'kode_customer': kodeCustomer,
- 'datetime': datetime.toString()
- }).then((response){
- print(response.msg);
- setState(() {
- _selectedDateData = response;
- _selectedDateApiCall = false;
- });
- },onError: (error){
- print(error.toString());
- });
- }
- Future _navigateAndDisplayInput(BuildContext context) async {
- // Navigator.push returns a Future that will complete after we call
- // Navigator.pop on the Selection Screen!
- final result = await Navigator.push(
- context,
- MaterialPageRoute(builder: (context) => ScheduleCreate()),
- );
- // After the Selection Screen returns a result, hide any previous snackbars
- // and show the new result!
- if (result != null) {
- _scheduleScaffoldKey.currentState.showSnackBar(
- SnackBar(
- content: Text("$result"),
- )
- );
- }
- }
- Future _rescheduleEvent(BuildContext context, String noJadwal) async {
- // Navigator.push returns a Future that will complete after we call
- // Navigator.pop on the Selection Screen!
- final result = await Navigator.push(
- context,
- MaterialPageRoute(builder: (context) => ScheduleReschedule(noJadwal: noJadwal,)),
- );
- // After the Selection Screen returns a result, hide any previous snackbars
- // and show the new result!
- if (result != null) {
- _scheduleScaffoldKey.currentState.showSnackBar(
- SnackBar(
- content: Text("$result"),
- )
- );
- }
- }
- Future _deleteConfirmation(String scheduleTitle, String noJadwal) {
- return showDialog(
- context: context,
- builder: (context){
- return AlertDialog(
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.all(Radius.circular(10.0))
- ),
- content: Column(
- mainAxisSize: MainAxisSize.min,
- children: <Widget>[
- Row(
- children: <Widget>[
- Expanded(
- child: Text('Hapus agenda $scheduleTitle?'),
- )
- ],
- ),
- SizedBox(height: 25.0),
- Row(
- children: <Widget>[
- Expanded(
- child: OutlineButton(
- onPressed: (){
- _deleteSchedule(noJadwal);
- Navigator.pop(context);
- },
- child: Text('Hapus',style: TextStyle(fontWeight: FontWeight.bold,color: Colors.red),),
- ),
- ),
- SizedBox(width: 20.0),
- Expanded(
- child: OutlineButton(
- onPressed: (){
- Navigator.pop(context);
- },
- child: Text('Batal',style: TextStyle(fontWeight: FontWeight.bold,color: Theme.of(context).primaryColor),),
- ),
- )
- ],
- )
- ],
- ),
- );
- }
- );
- }
- @override
- Widget build(BuildContext context) {
- return WillPopScope(
- onWillPop: (){
- return Navigator.pushReplacement(context, MaterialPageRoute(
- builder: (context) => Home()
- ));
- },
- child: Scaffold(
- key: _scheduleScaffoldKey,
- floatingActionButton: FloatingActionButton(
- child: Icon(Icons.add),
- tooltip: 'Tambah Agenda',
- onPressed: () {
- _navigateAndDisplayInput(context);
- },
- ),
- appBar: AppBar(
- leading: IconButton(
- icon: Icon(Icons.arrow_back),
- onPressed: (){
- Navigator.pushReplacement(context, MaterialPageRoute(
- builder: (context) => Home()
- ));
- },
- ),
- title: Text('Agenda Inspeksi'),
- centerTitle: true,
- elevation: 0.0,
- ),
- body: Container(
- child: ListView(
- children: <Widget>[
- Stack(
- children: <Widget>[
- Column(
- children: <Widget>[
- Container(
- height: MediaQuery.of(context).size.height * .25,
- color: Theme.of(context).primaryColor,
- ),
- Container(
- height: MediaQuery.of(context).size.height * .75,
- color: Colors.white,
- )
- ],
- ),
- Padding(
- padding: EdgeInsets.only(left: 10.0,top: 20.0,right: 10.0),
- child: Row(
- children: <Widget>[
- Expanded(
- child: SizedBox(
- child: Column(
- children: <Widget>[
- FutureBuilder(
- future: fetchSchedule(http.Client()),
- builder: (context, snapshot){
- print('>>>>>>> ${snapshot.connectionState}');
- if (snapshot.hasError) print(snapshot.error);
- if (snapshot.connectionState == ConnectionState.done) {
- return _scheduleCalendar(snapshot.data);
- } else {
- return Center(
- child: CircularProgressIndicator(
- backgroundColor: Colors.white,
- ),
- );
- }
- },
- ),
- ],
- ),
- ),
- )
- ],
- ),
- ),
- ],
- ),
- Container(height: 20.0)
- ],
- ),
- ),
- )
- );
- }
- Widget _buildTableCalendar() {
- return TableCalendar(
- locale: 'en_US',
- events: _visibleEvents,
- // holidays: _visibleHolidays,
- initialCalendarFormat: CalendarFormat.month,
- formatAnimation: FormatAnimation.slide,
- startingDayOfWeek: StartingDayOfWeek.monday,
- availableGestures: AvailableGestures.all,
- availableCalendarFormats: const {
- CalendarFormat.month: 'Month',
- CalendarFormat.twoWeeks: '2 weeks',
- CalendarFormat.week: 'Week',
- },
- onDaySelected: (datetime, list){
- setState(() {
- _selectedDate = datetime;
- _selectedDateApiCall = true;
- });
- _getScheduleOnSelectedDate(datetime);
- },
- calendarStyle: CalendarStyle(
- selectedColor: Colors.red,
- todayColor: Theme.of(context).primaryColor,
- markersColor: Colors.blue,
- ),
- headerStyle: HeaderStyle(
- formatButtonTextStyle:
- TextStyle().copyWith(color: Colors.white, fontSize: 15.0),
- formatButtonDecoration: BoxDecoration(
- color: Theme.of(context).primaryColor,
- borderRadius: BorderRadius.circular(16.0),
- ),
- )
- );
- }
- _updateCalendar(int day) {
- if (day > 0) {
- _getEvents[_selectedDay.subtract(Duration(days: day.abs()))] = [''];
- } else {
- _getEvents[_selectedDay.add(Duration(days: day.abs()))] = [''];
- }
- return Container();
- }
- Widget _updateMarker(ScheduleResponse data) {
- data.data.map(
- (event) => _updateCalendar(event['day_margin'])
- ).toList();
- return Container();
- }
- Widget _eventList(ScheduleResponse data) {
- if (_selectedDate == null) {
- if (data.currentDate.length > 0) {
- data.data.map(
- (event) => _updateCalendar(event['day_margin'])
- ).toList();
- return Column(
- children: data.currentDate.map((index) {
- return Column(
- children: <Widget>[
- Divider(),
- SizedBox(height: 10.0),
- Row(
- children: <Widget>[
- Container(
- height: 165.0,
- decoration: BoxDecoration(
- borderRadius: BorderRadius.all(Radius.circular(5.0)),
- color: index['category'] == 'USER'
- ? Colors.orange
- : Theme.of(context).primaryColor,
- ),
- width: 8.0,
- ),
- SizedBox(width: 10.0),
- Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- // _updateCalendar(index['day_margin']),
- Text(index['title'] ?? '-',style: TextStyle(fontWeight: FontWeight.bold)),
- SizedBox(height: 5.0),
- Row(
- children: <Widget>[
- Icon(Icons.calendar_today,size: 13.0,),
- SizedBox(width: 5.0),
- Text('Mulai: ${index['date_from'] ?? '-'}'),
- ],
- ),
- Row(
- children: <Widget>[
- Icon(Icons.calendar_today,size: 13.0,),
- SizedBox(width: 5.0),
- Text('Berakhir: ${index['date_to'] ?? '-'}'),
- ],
- ),
- SizedBox(height: 10.0),
- Text('Jenis Agenda: ${index['type']}'),
- SizedBox(height: 10.0),
- Text('Keterangan: ${index['note'] ?? '-'}'),
- SizedBox(height: 10.0),
- Row(
- children: <Widget>[
- OutlineButton(
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.all(Radius.circular(10.0))
- ),
- child: Text('Ubah Agenda'),
- onPressed: (){
- _rescheduleEvent(context, index['no_jadwal']);
- },
- ),
- SizedBox(width: 10.0),
- OutlineButton(
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.all(Radius.circular(10.0))
- ),
- child: Text('Hapus'),
- onPressed: (){
- _deleteConfirmation('${index["title"] ?? ""}','${index["no_jadwal"]}');
- },
- )
- ],
- )
- ],
- )
- ],
- ),
- SizedBox(height: 10.0)
- ],
- );
- }).toList(),
- );
- } else {
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- Divider(),
- SizedBox(height: 10.0),
- Text('Tidak ada agenda',style: TextStyle(color: Colors.grey)),
- SizedBox(height: 10.0),
- ],
- );
- }
- } else {
- return _selectedDateApiCall
- ? Center(
- child: CircularProgressIndicator(),
- )
- : Column(
- children: _selectedDateData.data.map((index) {
- return Column(
- children: <Widget>[
- Divider(),
- SizedBox(height: 10.0),
- Row(
- children: <Widget>[
- Container(
- height: 165.0,
- decoration: BoxDecoration(
- borderRadius: BorderRadius.all(Radius.circular(5.0)),
- color: index.category == 'USER'
- ? Colors.orange
- : Theme.of(context).primaryColor,
- ),
- width: 8.0,
- ),
- SizedBox(width: 10.0),
- Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- Text(index.title ?? '-',style: TextStyle(fontWeight: FontWeight.bold)),
- SizedBox(height: 5.0),
- Row(
- children: <Widget>[
- Icon(Icons.calendar_today,size: 13.0,),
- SizedBox(width: 5.0),
- Text('Mulai: ${index.dateFrom ?? '-'}'),
- ],
- ),
- Row(
- children: <Widget>[
- Icon(Icons.calendar_today,size: 13.0,),
- SizedBox(width: 5.0),
- Text('Berakhir: ${index.dateTo ?? '-'}'),
- ],
- ),
- SizedBox(height: 10.0),
- Text('Jenis Agenda: ${index.type}'),
- SizedBox(height: 10.0),
- Text('Keterangan: ${index.note ?? '-'}'),
- SizedBox(height: 10.0),
- Row(
- children: <Widget>[
- OutlineButton(
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.all(Radius.circular(10.0))
- ),
- child: Text('Ubah Agenda'),
- onPressed: (){
- // Navigator.push(context, MaterialPageRoute(
- // builder: (context) => ScheduleReschedule(noJadwal: index['no_jadwal'])
- // ));
- _rescheduleEvent(context, index.noJadwal);
- },
- ),
- SizedBox(width: 10.0),
- OutlineButton(
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.all(Radius.circular(10.0))
- ),
- child: Text('Hapus'),
- onPressed: (){
- _deleteConfirmation('${index.title ?? ""}','${index.noJadwal}');
- },
- )
- ],
- )
- ],
- )
- ],
- ),
- SizedBox(height: 10.0)
- ],
- );
- }).toList(),
- );
- }
- }
- Widget _scheduleCalendar(ScheduleResponse data) {
- return Column(
- children: <Widget>[
- Card(
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.all(
- Radius.circular(10.0))),
- elevation: 3.0,
- child: Column(
- children: <Widget>[
- _buildTableCalendar(),
- ],
- ),
- ),
- SizedBox(height: 10.0),
- Card(
- child: Column(
- children: <Widget>[
- Container(
- child: Padding(
- padding: EdgeInsets.only(left: 10.0,right: 10.0,top: 10.0),
- child: Column(
- children: <Widget>[
- Row(
- children: <Widget>[
- Text('Agenda Inspeksi',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 20.0))
- ],
- ),
- _eventList(data)
- // _updateMarker(data),
- // EventList(todayDate: data, mode: 'todayDate'),
- ],
- ),
- ),
- )
- ],
- ),
- elevation: 3.0,
- )
- ],
- );
- }
- }
- class EventList extends StatefulWidget {
- final ScheduleResponse todayDate;
- final SelectedScheduleResponse selectedDate;
- final String mode;
- EventList({this.todayDate, this.selectedDate, this.mode});
- @override
- State<StatefulWidget> createState() => EventListState();
- }
- class EventListState extends State<EventList> {
- String test;
- @override
- Widget build(BuildContext context) {
- print(widget.mode);
- return widget.mode == 'todayDate'
- ? Column(
- children: widget.todayDate.currentDate.map((index) {
- return Column(
- children: <Widget>[
- Divider(),
- SizedBox(height: 10.0),
- Row(
- children: <Widget>[
- Container(
- height: 165.0,
- decoration: BoxDecoration(
- borderRadius: BorderRadius.all(Radius.circular(5.0)),
- color: index['category'] == 'USER'
- ? Colors.orange
- : Theme.of(context).primaryColor,
- ),
- width: 8.0,
- ),
- SizedBox(width: 10.0),
- Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- // _updateCalendar(index['day_margin']),
- Text(index['title'] ?? '-',style: TextStyle(fontWeight: FontWeight.bold)),
- SizedBox(height: 5.0),
- Row(
- children: <Widget>[
- Icon(Icons.calendar_today,size: 13.0,),
- SizedBox(width: 5.0),
- Text('Mulai: ${index['date_from'] ?? '-'}'),
- ],
- ),
- Row(
- children: <Widget>[
- Icon(Icons.calendar_today,size: 13.0,),
- SizedBox(width: 5.0),
- Text('Berakhir: ${index['date_to'] ?? '-'}'),
- ],
- ),
- SizedBox(height: 10.0),
- Text('Jenis Agenda: ${index['type']}'),
- SizedBox(height: 10.0),
- Text('Keterangan: ${index['note'] ?? '-'}'),
- SizedBox(height: 10.0),
- Row(
- children: <Widget>[
- OutlineButton(
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.all(Radius.circular(10.0))
- ),
- child: Text('Ubah Agenda'),
- onPressed: (){
- // _rescheduleEvent(context, index['no_jadwal']);
- },
- ),
- SizedBox(width: 10.0),
- OutlineButton(
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.all(Radius.circular(10.0))
- ),
- child: Text('Hapus'),
- onPressed: (){
- // _deleteConfirmation('${index["title"] ?? ""}','${index["no_jadwal"]}');
- },
- )
- ],
- )
- ],
- )
- ],
- ),
- SizedBox(height: 10.0)
- ],
- );
- }).toList(),
- )
- : Column(
- children: widget.selectedDate.data.map((index) {
- return Column(
- children: <Widget>[
- Divider(),
- SizedBox(height: 10.0),
- Row(
- children: <Widget>[
- Container(
- height: 165.0,
- decoration: BoxDecoration(
- borderRadius: BorderRadius.all(Radius.circular(5.0)),
- color: index.category== 'USER'
- ? Colors.orange
- : Theme.of(context).primaryColor,
- ),
- width: 8.0,
- ),
- SizedBox(width: 10.0),
- Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- // _updateCalendar(index['day_margin']),
- Text(index.title ?? '-',style: TextStyle(fontWeight: FontWeight.bold)),
- SizedBox(height: 5.0),
- Row(
- children: <Widget>[
- Icon(Icons.calendar_today,size: 13.0,),
- SizedBox(width: 5.0),
- Text('Mulai: ${index.dateFrom ?? '-'}'),
- ],
- ),
- Row(
- children: <Widget>[
- Icon(Icons.calendar_today,size: 13.0,),
- SizedBox(width: 5.0),
- Text('Berakhir: ${index.dateTo ?? '-'}'),
- ],
- ),
- SizedBox(height: 10.0),
- Text('Jenis Agenda: ${index.type}'),
- SizedBox(height: 10.0),
- Text('Keterangan: ${index.note ?? '-'}'),
- SizedBox(height: 10.0),
- Row(
- children: <Widget>[
- OutlineButton(
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.all(Radius.circular(10.0))
- ),
- child: Text('Ubah Agenda'),
- onPressed: (){
- // _rescheduleEvent(context, index['no_jadwal']);
- },
- ),
- SizedBox(width: 10.0),
- OutlineButton(
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.all(Radius.circular(10.0))
- ),
- child: Text('Hapus'),
- onPressed: (){
- // _deleteConfirmation('${index["title"] ?? ""}','${index["no_jadwal"]}');
- },
- )
- ],
- )
- ],
- )
- ],
- ),
- SizedBox(height: 10.0)
- ],
- );
- }).toList(),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement