Skip to content

Commit acc4c32

Browse files
authored
Merge pull request #26 from DutchCodingCompany/feature/improve_dialog
Minor dialog improvements
2 parents 28c2e78 + 5c2508b commit acc4c32

File tree

8 files changed

+29
-23
lines changed

8 files changed

+29
-23
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 0.0.15
4+
* Added `FutureOr<T?>` to `showNativeDialog` as return type;
5+
36
## 0.0.14
47
* Added Pagination
58
* Added RefreshStreamMixin

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ packages:
212212
path: ".."
213213
relative: true
214214
source: path
215-
version: "0.0.13"
215+
version: "0.0.14"
216216
equatable:
217217
dependency: transitive
218218
description:

lib/dcc_toolkit.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ export 'common/mixins/refresh_stream_mixin.dart';
88
export 'common/result/result.dart';
99
export 'common/type_defs.dart';
1010
export 'logger/bolt_logger.dart';
11-
export 'pagination/paginated_scroll_view.dart';
12-
export 'pagination/pagination_interface.dart';
13-
export 'pagination/pagination_mixin.dart';
14-
export 'pagination/pagination_state.dart';
11+
export 'pagination/paginated.dart';
1512
export 'style/style.dart';
1613
export 'test_util/devices_sizes.dart';
1714
export 'test_util/presentation_event_catcher.dart';

lib/pagination/paginated.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export 'package:dcc_toolkit/pagination/paginated_scroll_view.dart';
2+
export 'package:dcc_toolkit/pagination/pagination_interface.dart';
3+
export 'package:dcc_toolkit/pagination/pagination_mixin.dart';
4+
export 'package:dcc_toolkit/pagination/pagination_state.dart';

lib/pagination/pagination_state.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// State for pagination.
22
class PaginationState<T> {
33
/// Creates a new [PaginationState] with the given values.
4-
PaginationState({
4+
const PaginationState({
55
this.items = const [],
66
this.currentPage = 1,
77
this.lastPage = 1,

lib/ui/native_dialog.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/cupertino.dart';
24
import 'package:flutter/foundation.dart';
35
import 'package:flutter/material.dart';
@@ -15,14 +17,14 @@ import 'package:flutter/material.dart';
1517
/// ],
1618
/// );
1719
/// ```
18-
void showNativeDialog(
20+
Future<T?> showNativeDialog<T>(
1921
BuildContext context, {
2022
required String title,
21-
required List<DialogAction> actions,
23+
required List<DialogAction<T>> actions,
2224
required String content,
2325
}) {
2426
if (defaultTargetPlatform == TargetPlatform.iOS) {
25-
showCupertinoDialog<void>(
27+
return showCupertinoDialog<T>(
2628
context: context,
2729
builder:
2830
(dialogContext) => CupertinoAlertDialog(
@@ -32,9 +34,9 @@ void showNativeDialog(
3234
actions
3335
.map(
3436
(action) => CupertinoDialogAction(
35-
onPressed: () {
36-
action.onTap();
37-
Navigator.of(dialogContext).pop();
37+
onPressed: () async {
38+
final result = await action.onTap();
39+
if (dialogContext.mounted) Navigator.of(dialogContext).pop(result);
3840
},
3941
isDestructiveAction: action.isDestructiveAction,
4042
child: Text(action.text),
@@ -44,7 +46,7 @@ void showNativeDialog(
4446
),
4547
);
4648
} else {
47-
showDialog<void>(
49+
return showDialog<T?>(
4850
context: context,
4951
builder:
5052
(dialogContext) => AlertDialog(
@@ -54,9 +56,9 @@ void showNativeDialog(
5456
actions
5557
.map(
5658
(action) => TextButton(
57-
onPressed: () {
58-
action.onTap();
59-
Navigator.of(dialogContext).pop();
59+
onPressed: () async {
60+
final result = await action.onTap();
61+
if (dialogContext.mounted) Navigator.of(dialogContext).pop(result);
6062
},
6163
child: Text(action.text),
6264
),
@@ -68,15 +70,15 @@ void showNativeDialog(
6870
}
6971

7072
/// A dialog action which is used to show the actions of a native dialog. Tapping a action will also close the dialog.
71-
class DialogAction {
73+
class DialogAction<T> {
7274
/// Creates a [DialogAction].
7375
const DialogAction({required this.text, required this.onTap, this.isDestructiveAction = false});
7476

7577
/// The text of the action.
7678
final String text;
7779

7880
/// The callback that is called when the action is tapped.
79-
final VoidCallback onTap;
81+
final FutureOr<T?> Function() onTap;
8082

8183
/// Whether the action is a destructive action. This is only used on iOS.
8284
final bool isDestructiveAction;

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: dcc_toolkit
22
description: "Internal toolkit package used by the DCC team."
3-
version: 0.0.14
3+
version: 0.0.15
44
homepage: https://dutchcodingcompany.com
55
repository: https://github.com/DutchCodingCompany/dcc_toolkit
66

test/pagination/pagination_mixin_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void main() {
3333
});
3434

3535
test('loadNextPage emits new state with next page items when current page is less than last page', () async {
36-
final paginationState = PaginationState<int>(items: [1, 2, 3], currentPage: 1, lastPage: 3);
36+
const paginationState = PaginationState<int>(items: [1, 2, 3], currentPage: 1, lastPage: 3);
3737
cubit.emit(_TestState(paginationState));
3838

3939
final nextPaginationStates = <PaginationState<int>?>[];
@@ -47,7 +47,7 @@ void main() {
4747
});
4848

4949
test('loadNextPage does not emit new state when current page is equal to last page', () async {
50-
final paginationState = PaginationState<int>(items: [1, 2, 3], currentPage: 3, lastPage: 3);
50+
const paginationState = PaginationState<int>(items: [1, 2, 3], currentPage: 3, lastPage: 3);
5151
cubit.emit(_TestState(paginationState));
5252

5353
final nextPaginationStates = <PaginationState<int>?>[];
@@ -57,7 +57,7 @@ void main() {
5757
});
5858

5959
test('loadNextPage does not emit new state when next page items are empty', () async {
60-
final paginationState = PaginationState<int>(items: [1, 2, 3], currentPage: 1, lastPage: 3);
60+
const paginationState = PaginationState<int>(items: [1, 2, 3], currentPage: 1, lastPage: 3);
6161
cubit
6262
..emit(_TestState(paginationState))
6363
..returnPages = [];
@@ -88,7 +88,7 @@ class _TestState implements PaginationInterface<int> {
8888
}
8989

9090
class _TestCubit extends Cubit<_TestState> with PaginationMixin<int, _TestState> {
91-
_TestCubit() : super(_TestState(PaginationState()));
91+
_TestCubit() : super(_TestState(const PaginationState()));
9292

9393
List<int> returnPages = [1, 2, 3];
9494

0 commit comments

Comments
 (0)