[Homepage] 3. add date & time widget

This commit is contained in:
Stephen
2024-01-11 14:55:38 +07:00
parent 17f451edb6
commit 9cff5e1db8
2 changed files with 77 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
import 'package:absensi_sas_flutter/app/constant.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class RealTimeFormattedDate extends StatefulWidget {
@override
_RealTimeFormattedDateState createState() => _RealTimeFormattedDateState();
}
class _RealTimeFormattedDateState extends State<RealTimeFormattedDate> {
late Stream<DateTime> _clockStream;
@override
void initState() {
super.initState();
_clockStream = Stream<DateTime>.periodic(Duration(seconds: 1), (i) => DateTime.now());
}
@override
Widget build(BuildContext context) {
return StreamBuilder<DateTime>(
stream: _clockStream,
builder: (context, snapshot) {
if (snapshot.hasData) {
String formattedDate = DateFormat('EEEE, d MMMM y', 'id_ID').format(snapshot.data!);
return Text(
formattedDate,
style: Constant.date_600(context: context).copyWith(
color: Constant.textBlack),
);
} else {
return Text('Loading...');
}
},
);
}
}

View File

@@ -0,0 +1,39 @@
import 'package:absensi_sas_flutter/app/constant.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class RealTimeClock extends StatefulWidget {
@override
_RealTimeClockState createState() => _RealTimeClockState();
}
class _RealTimeClockState extends State<RealTimeClock> {
late Stream<DateTime> _clockStream;
@override
void initState() {
super.initState();
_clockStream = Stream<DateTime>.periodic(Duration(seconds: 1), (i) => DateTime.now());
}
@override
Widget build(BuildContext context) {
return StreamBuilder<DateTime>(
stream: _clockStream,
builder: (context, snapshot) {
if (snapshot.hasData) {
String formattedTime = DateFormat('HH:mm:ss').format(snapshot.data!);
return Text(
formattedTime,
style: Constant.time_700(context: context).copyWith(
color: Constant.textBlack,
),
);
} else {
return Text('Loading...');
}
},
);
}
}