Skip to content

Commit

Permalink
fix: Removed FutureBuilders for dateFormat that were causing UI reloads
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-bak committed Feb 22, 2024
1 parent 350a540 commit ae21329
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 386 deletions.
11 changes: 11 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_displaymode/flutter_displaymode.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:openreads/core/constants/constants.dart';
import 'package:openreads/core/constants/locale.dart';
import 'package:openreads/core/helpers/old_android_http_overrides.dart';
Expand All @@ -34,6 +35,7 @@ late BookCubit bookCubit;
late Directory appDocumentsDirectory;
late Directory appTempDirectory;
late GlobalKey<ScaffoldMessengerState> snackbarKey;
late DateFormat dateFormat;

void main() async {
WidgetsFlutterBinding.ensureInitialized();
Expand Down Expand Up @@ -159,6 +161,8 @@ class _OpenreadsAppState extends State<OpenreadsApp>

@override
Widget build(BuildContext context) {
_initDateFormat(context);

return DynamicColorBuilder(
builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
if (widget.themeState.amoledDark) {
Expand Down Expand Up @@ -248,3 +252,10 @@ _setAndroidConfig() async {
}
}
}

Future _initDateFormat(BuildContext context) async {
await initializeDateFormatting();

// ignore: use_build_context_synchronously
dateFormat = DateFormat.yMMMMd(context.locale.toString());
}
2 changes: 0 additions & 2 deletions lib/ui/add_book_screen/add_book_screen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/add_book_screen/widgets/reading_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class _ReadingRowState extends State<ReadingRow> {
padding: EdgeInsets.symmetric(vertical: 20),
child: Icon(Icons.delete),
),
)
),
],
),
);
Expand Down
138 changes: 60 additions & 78 deletions lib/ui/add_book_screen/widgets/set_date_button.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:openreads/core/themes/app_theme.dart';
import 'package:openreads/main.dart';

class SetDateButton extends StatefulWidget {
const SetDateButton({
Expand All @@ -28,92 +27,75 @@ class SetDateButton extends StatefulWidget {
}

class _SetDateButtonState extends State<SetDateButton> {
late DateFormat dateFormat;

Future _initDateFormat() async {
await initializeDateFormatting();

// ignore: use_build_context_synchronously
dateFormat = DateFormat.yMMMMd(context.locale.toString());
}

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _initDateFormat(),
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return const SizedBox();
}

return SizedBox(
height: widget.defaultHeight,
child: Stack(
children: [
InkWell(
customBorder: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(cornerRadius),
),
onTap: widget.onPressed,
child: Ink(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(cornerRadius),
color: Theme.of(context)
.colorScheme
.surfaceVariant
.withOpacity(0.5),
border: Border.all(color: dividerColor),
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 10,
),
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(
widget.icon,
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(width: 10),
FittedBox(
child: Text(
widget.date != null
? dateFormat.format(widget.date!)
: widget.text,
maxLines: 1,
style: const TextStyle(fontSize: 12),
),
),
],
),
),
),
),
return SizedBox(
height: widget.defaultHeight,
child: Stack(
children: [
InkWell(
customBorder: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(cornerRadius),
),
onTap: widget.onPressed,
child: Ink(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(cornerRadius),
color: Theme.of(context)
.colorScheme
.surfaceVariant
.withOpacity(0.5),
border: Border.all(color: dividerColor),
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 10,
),
Center(
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
(widget.showClearButton)
? IconButton(
icon: const Icon(
Icons.close,
size: 20,
),
onPressed: widget.onClearPressed,
)
: const SizedBox(),
Icon(
widget.icon,
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(width: 10),
FittedBox(
child: Text(
widget.date != null
? dateFormat.format(widget.date!)
: widget.text,
maxLines: 1,
style: const TextStyle(fontSize: 12),
),
),
],
),
),
),
),
),
Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
(widget.showClearButton)
? IconButton(
icon: const Icon(
Icons.close,
size: 20,
),
onPressed: widget.onClearPressed,
)
: const SizedBox(),
],
),
);
});
),
],
),
);
}
}
103 changes: 43 additions & 60 deletions lib/ui/book_screen/widgets/book_detail_date_added_updated.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:intl/date_symbol_data_local.dart';

import 'package:openreads/core/themes/app_theme.dart';
import 'package:openreads/generated/locale_keys.g.dart';
import 'package:openreads/main.dart';

class BookDetailDateAddedUpdated extends StatefulWidget {
const BookDetailDateAddedUpdated({
Expand All @@ -22,82 +22,65 @@ class BookDetailDateAddedUpdated extends StatefulWidget {

class _BookDetailDateAddedUpdatedState
extends State<BookDetailDateAddedUpdated> {
late DateFormat dateFormat;

Future _initDateFormat() async {
await initializeDateFormatting();

// ignore: use_build_context_synchronously
dateFormat = DateFormat.yMMMMd(context.locale.toString());
}

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _initDateFormat(),
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return const SizedBox();
}

return Card(
shadowColor: Colors.transparent,
shape: RoundedRectangleBorder(
side: BorderSide(color: dividerColor, width: 1),
borderRadius: BorderRadius.circular(cornerRadius),
return Card(
shadowColor: Colors.transparent,
shape: RoundedRectangleBorder(
side: BorderSide(color: dividerColor, width: 1),
borderRadius: BorderRadius.circular(cornerRadius),
),
elevation: 0,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
SelectableText(
LocaleKeys.added_on.tr(),
style: const TextStyle(
fontSize: 12,
fontWeight: FontWeight.normal,
),
),
SelectableText(
'${dateFormat.format(widget.dateAdded)} ${widget.dateAdded.hour}:${widget.dateAdded.minute.toString().padLeft(2, '0')}',
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.normal,
),
),
],
),
elevation: 0,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Column(
widget.dateAdded.millisecondsSinceEpoch !=
widget.dateModified.millisecondsSinceEpoch
? Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
const SizedBox(height: 10),
SelectableText(
LocaleKeys.added_on.tr(),
LocaleKeys.modified_on.tr(),
style: const TextStyle(
fontSize: 12,
fontWeight: FontWeight.normal,
),
),
SelectableText(
'${dateFormat.format(widget.dateAdded)} ${widget.dateAdded.hour}:${widget.dateAdded.minute.toString().padLeft(2, '0')}',
'${dateFormat.format(widget.dateModified)} ${widget.dateModified.hour}:${widget.dateModified.minute}',
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.normal,
),
),
],
),
widget.dateAdded.millisecondsSinceEpoch !=
widget.dateModified.millisecondsSinceEpoch
? Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
const SizedBox(height: 10),
SelectableText(
LocaleKeys.modified_on.tr(),
style: const TextStyle(
fontSize: 12,
fontWeight: FontWeight.normal,
),
),
SelectableText(
'${dateFormat.format(widget.dateModified)} ${widget.dateModified.hour}:${widget.dateModified.minute}',
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.normal,
),
),
],
)
: const SizedBox(),
],
),
),
);
});
)
: const SizedBox(),
],
),
),
);
}
}
Loading

0 comments on commit ae21329

Please sign in to comment.