feat: added datetime_picker

This commit is contained in:
Freek van de Ven 2022-09-01 10:30:08 +02:00
parent c8e010e3ec
commit c775e06f77
8 changed files with 276 additions and 5 deletions

View file

@ -1,7 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iconica.agenda_example"> package="com.iconica.agenda_example">
<application <application
android:label="agenda_example" android:label="Flutter Agenda Example"
android:name="${applicationName}" android:name="${applicationName}"
android:icon="@mipmap/ic_launcher"> android:icon="@mipmap/ic_launcher">
<activity <activity

View file

@ -1,3 +1,4 @@
import 'package:agenda/agenda.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
void main() { void main() {
@ -9,6 +10,69 @@ class AgendaDemo extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return const Scaffold(body: Text('AgendaDemo')); return Scaffold(
body: SafeArea(
child: AgendaWidget(
header: Text('Agenda', style: Theme.of(context).textTheme.headline6),
blockWidth: 50,
highlightToday: false,
blocks: [
AgendaEvent(
start: DateTime.now().subtract(const Duration(hours: 3)),
end: DateTime.now().add(const Duration(hours: 2)),
),
AgendaEvent(
start: DateTime.now().subtract(const Duration(hours: 2)),
end: DateTime.now().add(const Duration(hours: 1)),
),
AgendaEvent(
start: DateTime.now().subtract(const Duration(hours: 1)),
end: DateTime.now().add(const Duration(hours: 1)),
),
AgendaEvent(
start: DateTime.now().add(const Duration(hours: 3)),
end: DateTime.now().add(const Duration(hours: 4)),
id: 4,
content: const Text('event 4'),
),
AgendaEvent(
start: DateTime.now().add(const Duration(hours: 3)),
end: DateTime.now().add(const Duration(hours: 4)),
id: 4,
content: const Text('event 5'),
),
AgendaEvent(
start: DateTime.now().add(const Duration(hours: 3)),
end: DateTime.now().add(const Duration(hours: 4)),
id: 4,
content: const Text('event 6'),
),
AgendaEvent(
start: DateTime.now().add(const Duration(days: 1)),
end: DateTime.now()
.add(const Duration(days: 1))
.add(const Duration(hours: 2)),
),
AgendaEvent(
start: DateTime.now().subtract(const Duration(hours: 2)),
end: DateTime.now().add(const Duration(hours: 1)),
),
],
disabledDates: [
// yesterday
DateTime.now().subtract(const Duration(days: 1)),
],
highlightedDates: [
// tomorrow
DateTime.now().add(const Duration(days: 1)),
],
theme: const AgendaTheme(
tableTheme: TableTheme(
blockPaddingBetween: 10,
),
),
),
),
);
} }
} }

View file

@ -62,6 +62,15 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
flutter_date_time_picker:
dependency: transitive
description:
path: "."
ref: main
resolved-ref: "7a3c66d28c2d29c1983d186435ed559a19fe34ff"
url: "https://github.com/Iconica-Development/flutter_date_time_picker"
source: git
version: "0.0.1"
flutter_lints: flutter_lints:
dependency: "direct dev" dependency: "direct dev"
description: description:
@ -74,6 +83,13 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
intl:
dependency: transitive
description:
name: intl
url: "https://pub.dartlang.org"
source: hosted
version: "0.17.0"
lints: lints:
dependency: transitive dependency: transitive
description: description:
@ -156,6 +172,15 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.4.12" version: "0.4.12"
timetable:
dependency: transitive
description:
path: "."
ref: "v0.0.2"
resolved-ref: "62fdaa443818bd646058b536a12304725d1619fd"
url: "https://github.com/Iconica-Development/timetable"
source: git
version: "0.0.1"
vector_math: vector_math:
dependency: transitive dependency: transitive
description: description:
@ -165,4 +190,4 @@ packages:
version: "2.1.2" version: "2.1.2"
sdks: sdks:
dart: ">=2.18.0 <3.0.0" dart: ">=2.18.0 <3.0.0"
flutter: ">=1.17.0" flutter: ">=2.0.0"

View file

@ -1 +1,7 @@
library agenda; library agenda;
export 'package:agenda/src/agenda.dart';
export 'package:agenda/src/models/agenda_event.dart';
export 'package:agenda/src/models/agenda_theme.dart';
export 'package:timetable/timetable.dart';

128
lib/src/agenda.dart Normal file
View file

@ -0,0 +1,128 @@
import 'package:agenda/src/models/agenda_event.dart';
import 'package:agenda/src/models/agenda_theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter_date_time_picker/flutter_date_time_picker.dart';
import 'package:timetable/timetable.dart';
class AgendaWidget extends StatefulWidget {
const AgendaWidget({
required this.blocks,
this.highlightedDates = const [],
this.disabledDates = const [],
this.initialDate,
this.header,
this.scrollController,
this.scrollPhysics,
this.startHour = 0,
this.endHour = 24,
this.hourHeight = 80,
this.highlightToday = true,
this.blockWidth = 50,
this.blockColor = const Color(0x80FF0000),
this.theme = const AgendaTheme(),
super.key,
});
/// Header widget that is displayed above the datepicker.
final Widget? header;
final List<AgendaEvent> blocks;
final List<DateTime> highlightedDates;
final List<DateTime> disabledDates;
/// The date that is initially selected.
final DateTime? initialDate;
final bool highlightToday;
/// Hour at which the timetable starts.
final int startHour;
/// Hour at which the timetable ends.
final int endHour;
/// The heigh of one hour in the timetable.
final double hourHeight;
/// The width of the agendaItem if there is no child
final double blockWidth;
/// The color of the agendaItem if there is no child
final Color blockColor;
/// The theme used by the agenda.
/// The [TableTheme] used by the timetable is included.
final AgendaTheme theme;
/// The scroll controller to control the scrolling of the timetable.
final ScrollController? scrollController;
/// The scroll physics used for the SinglechildScrollView.
final ScrollPhysics? scrollPhysics;
@override
State<AgendaWidget> createState() => _AgendaWidgetState();
}
class _AgendaWidgetState extends State<AgendaWidget> {
DateTime? _selectedDate;
@override
void initState() {
super.initState();
_selectedDate = widget.initialDate ?? DateTime.now();
}
@override
Widget build(BuildContext context) {
// filter out the blocks that are not on the selected date.
var events =
widget.blocks.where((block) => block.start.day == _selectedDate?.day);
return DateTimePicker(
initialDate: _selectedDate,
pickTime: false,
highlightToday: widget.highlightToday,
header: widget.header,
onTapDay: (p0) {
setState(() {
_selectedDate = p0;
});
},
disabledDates: widget.disabledDates,
markedDates: widget.highlightedDates,
dateTimePickerTheme: widget.theme.timePickerTheme,
child: Timetable(
scrollPhysics: widget.scrollPhysics,
scrollController: widget.scrollController,
blockColor: widget.blockColor,
blockWidth: widget.blockWidth,
hourHeight: widget.hourHeight,
startHour: widget.startHour,
endHour: widget.endHour,
timeBlocks: events.isEmpty
? []
: events
.map(
(e) => TimeBlock(
start: TimeOfDay(
hour: e.start.hour,
minute: e.start.minute,
),
end: TimeOfDay(
hour: e.end.hour,
minute: e.end.minute,
),
id: e.id ?? 0,
child: e.content,
),
)
.toList(),
theme: widget.theme.tableTheme,
combineBlocks: true,
mergeBlocks: true,
),
);
}
}

View file

@ -0,0 +1,23 @@
import 'package:flutter/material.dart';
class AgendaEvent {
const AgendaEvent({
required this.start,
required this.end,
this.id,
this.content,
});
/// The start time of the event.
final DateTime start;
/// The end time of the event.
final DateTime end;
///
final Widget? content;
/// The identifier of the event that is used to combine events
/// with the same id. Leave empty or 0 if you don't want to combine events.
final int? id;
}

View file

@ -0,0 +1,15 @@
import 'package:flutter_date_time_picker/flutter_date_time_picker.dart';
import 'package:timetable/timetable.dart';
class AgendaTheme {
const AgendaTheme({
this.tableTheme = const TableTheme(),
this.timePickerTheme = const DateTimePickerTheme(),
});
/// The theme for the timetable.
final TableTheme tableTheme;
/// The theme for the datetime picker.
final DateTimePickerTheme timePickerTheme;
}

View file

@ -3,17 +3,27 @@ description: Agenda widget with timetable
version: 0.0.1 version: 0.0.1
homepage: https://github.com/Iconica-Development/agenda homepage: https://github.com/Iconica-Development/agenda
publish_to: none
environment: environment:
sdk: '>=2.18.0 <3.0.0' sdk: ">=2.18.0 <3.0.0"
flutter: ">=1.17.0" flutter: ">=1.17.0"
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
flutter_date_time_picker:
git:
url: https://github.com/Iconica-Development/flutter_date_time_picker
ref: main
timetable:
git:
url: https://github.com/Iconica-Development/timetable
ref: v0.0.2
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
sdk: flutter sdk: flutter
flutter_lints: ^2.0.0 flutter_lints: ^2.0.0
flutter: flutter: