Comments (7)
I solved the problem earlier today, here it is:
import 'package:flutter/material.dart';
import 'package:flutter_week_view/flutter_week_view.dart';
import 'package:calendar_app/services/model.dart';
import 'package:calendar_app/services/events.dart';
import 'package:flutter/services.dart';
import 'package:calendar_app/services/day_events.dart';
class Day extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Dia'),
backgroundColor: Colors.lightBlueAccent,
),
body: _DemoDayView(),
);
}
}
class _DemoDayView extends StatefulWidget {
@override
__DemoDayViewState createState() => __DemoDayViewState();
}
class __DemoDayViewState extends State<_DemoDayView> {
Model model = Model();
List<FlutterWeekViewEvent> events = DayEvents.getDayEvents();
List _selectedEvents;
static DateTime now = DateTime.now();
DateTime date = DateTime(now.year, now.month, now.day);
Map<DateTime, List> _events = Events.getEvents();
@override
void initState() {
super.initState();
SystemChrome.setPreferredOrientations([//Disable Screen Rotation Orientation
DeviceOrientation.portraitUp,
]);
buildEvent();
}
void addEvent(FlutterWeekViewEvent event) {
print(_selectedEvents);
setState(() => events.add(event));
}
void buildEvent() {
_selectedEvents = _events[date];
_selectedEvents.map((event) =>
setState(() => events.add(FlutterWeekViewEvent(
title: event['name'].toString(),
description: (event['confirm'])
? 'Confirmado'
: 'Cancelado',
start: date.add(Duration(hours: int.parse(
event['time'].toString().substring(
0, 2)))),
end: date.add(Duration(hours: int.parse(
event['time'].toString().substring(
0, 2)) + 2)),
),
))
).toList();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Row(
children: <Widget>[
Expanded(child: buildDay())
],
),
),
);
}
Widget buildDay(){
DateTime now = DateTime.now();
return DayView(
date: now,
dayBarHeight: 50,
dayBarBackgroundColor: Colors.white,
dayBarTextStyle: const TextStyle(fontSize: 30),
events: events,
);
}
}
from flutterweekview.
You can wrap your DayView
or WeekView
in a StatefulWidget
and then refresh your events using setState
.
from flutterweekview.
When I did as you suggested, printed only the first event of the day from a list map. Here's the code:
Widget buildEvent(){
DateTime now = DateTime.now();
DateTime date = DateTime(now.year, now.month, now.day);
_selectedEvents = _events[date];
return Expanded(
child: Container(
child:
ListView(
shrinkWrap: true,
children: _selectedEvents.map((event) =>
Container(
child: DayView(
date: now,
inScrollableWidget: true,
scrollToCurrentTime: true,
events: [
FlutterWeekViewEvent(
title: event['name'].toString(),
description: (event['confirm'])
? 'Confirmado'
: 'Cancelado',
start: date.add(Duration(hours: int.parse(
event['time'].toString().substring(
0, 2)))),
end: date.add(Duration(hours: int.parse(
event['time'].toString().substring(
0, 2)) + 2)),
),
],
currentTimeCircleColor: Colors.red,
),
)
) .toList()
)
)
);
}
from flutterweekview.
Please post me your full widget code but if you want an idea of what you can do (untested) :
/// Your state.
class MyWidgetState extends State<MyWidget> {
/// Events added to the widget.
List<FlutterWeekView> events = [];
@override
Widget build(BuildContext context) => WeekView(
dates: dates,
events: events,
);
/// Allows to get the dates we need for the WeekView widget.
List<DateTime> get dates {
List<DateTime> dates = [];
for (FlutterWeekView event in events) {
if (!dates.contains(event.start)) {
dates.add(event.start);
}
if (!dates.contains(event.end)) {
dates.add(event.end);
}
}
return dates;
}
/// Adds an event to the list.
void addEvent(WeekViewEvent event) {
setState(() => events.add(event));
}
/// Removes an event from the list.
void removeEvent(WeekViewEvent event) {
setState(() => events.remove(event));
}
}
Swap on the left or on the right to see other days.
from flutterweekview.
This code prints one event per day in multiple screens, instead of prints all events of the day in one screen. here's the code:
events.dart
class Events {
static Map<DateTime, List> getEvents() {
return {
DateTime(2020, 2, 5): [
{
'name': 'Alex',
'time': '08:00',
'confirm': true},
],
DateTime(2020, 2, 6): [
{'name': 'Ana', 'time': '08:00', 'confirm': true},
{'name': 'Alan', 'time': '17:00', 'confirm': false},
]
}}}
day.dart
import 'package:flutter/material.dart';
import 'package:flutter_week_view/flutter_week_view.dart';
import 'package:calendar_app/services/model.dart';
import 'package:calendar_app/services/events.dart';
import 'package:flutter/services.dart';
class Day extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('day'),
),
body: _DemoDayView(),
);
}
}
class _DemoDayView extends StatefulWidget {
@override
__DemoDayViewState createState() => __DemoDayViewState();
}
class __DemoDayViewState extends State<_DemoDayView> {
Map<DateTime, List> _events = Events.getEvents();
List _selectedEvents;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Row(
children: <Widget>[
buildEvent()
],
),
);
}
Widget buildEvent(){
DateTime now = DateTime.now();
DateTime date = DateTime(now.year, now.month, now.day);
_selectedEvents = _events[date];
return Expanded(
child: Container(
child:
ListView(
shrinkWrap: true,
children: _selectedEvents.map((event) =>
Container(
child: DayView(
date: now,
inScrollableWidget: true,
scrollToCurrentTime: true,
events: [
FlutterWeekViewEvent(
title: event['name'].toString(),
description: (event['confirm'])
? 'Confirmado'
: 'Cancelado',
start: date.add(Duration(hours: int.parse(
event['time'].toString().substring(
0, 2)))),
end: date.add(Duration(hours: int.parse(
event['time'].toString().substring(
0, 2)) + 2)),
),
],
currentTimeCircleColor: Colors.red,
),
)
) .toList()
)
)
);
}
}
from flutterweekview.
Oops, haven't tested it, nevermind. You should check only the year / month / day (instead of year / month / day / hour / minute). Something like this should work :
/// Your state.
class MyWidgetState extends State<MyWidget> {
/// Events added to the widget.
List<FlutterWeekView> events = [];
@override
Widget build(BuildContext context) => WeekView(
dates: dates,
events: events,
);
/// Allows to get the dates we need for the WeekView widget.
List<DateTime> get dates {
List<DateTime> dates = [];
for (FlutterWeekView event in events) {
DateTime start = dayMonthYear(event.start);
DateTime end = dayMonthYear(event.end);
if (!dates.contains(start)) {
dates.add(start);
}
if (!dates.contains(end)) {
dates.add(end);
}
}
return dates;
}
/// Adds an event to the list.
void addEvent(WeekViewEvent event) {
setState(() => events.add(event));
}
/// Removes an event from the list.
void removeEvent(WeekViewEvent event) {
setState(() => events.remove(event));
}
/// Truncates the given date.
DateTime dayMonthYear(DateTime date) => DateTime(date.year, date.month, date.day);
}
But you're not even using setState
in your code ?
from flutterweekview.
Cool !
from flutterweekview.
Related Issues (20)
- Custom widget as Event HOT 1
- Press and drag causes UI issues on the web
- dayBarVisible
- Can I hide the daybar? HOT 2
- Show multiple days on screen HOT 6
- Navigating throught date programmatically HOT 6
- hourRowHeight not available
- Ability to scroll by one item in WeekView. HOT 1
- Setting inScrollableWidget to false throws error.
- DayViewController minZoom/maxZoom does not work
- ScrollController not attached to any scroll views HOT 3
- Full week on a single page HOT 1
- Add Ripple effect (using InkWell) to the events HOT 1
- How to get click event, if no event is set? HOT 1
- [FLUTTER 3] Warning: Operand of null-aware operation '?.' has type 'WidgetsBinding' which excludes null.
- Option to add an horizontal scrollbar
- Need current date of scrolled position HOT 3
- not responsive HOT 3
- Current time indicator is not on the exact current time
- how to set 12 hours format in day view ?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutterweekview.