Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AlertDialogのUI実装 #19

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.7.10'
ext.kotlin_version = '1.9.0'
repositories {
google()
mavenCentral()
Expand Down
35 changes: 35 additions & 0 deletions lib/debug/debug_alert_dialog_page.dart
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このコードってどこかで使用される予定とか将来的にあったりする?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Figmaにはまだ書いてないけどログアウトとか権限周りで使うよ

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:flutter/material.dart';
import 'package:pg_mobile/util/navigator_util.dart';

class DebugAlertDialogPage extends StatelessWidget {
const DebugAlertDialogPage({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Alert Dialog'),
),
body: SafeArea(
child: Center(
child: ElevatedButton(
onPressed: () {
NavigatorUtil.showCommonAlertDialog(
context,
titleText: 'titleText',
contentText: 'contentText',
onPressedCancel: () {
Navigator.pop(context);
},
onPressedOK: () {
Navigator.pop(context);
},
);
},
child: const Text('AlertDialogを表示'),
),
),
),
);
}
}
11 changes: 11 additions & 0 deletions lib/debug/debug_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pg_mobile/debug/debug_alert_dialog_page.dart';
import 'package:pg_mobile/debug/debug_text_theme_page.dart';
import 'package:pg_mobile/debug/login_sample/login_sample.dart';

Expand Down Expand Up @@ -50,6 +51,16 @@ class DebugPage extends StatelessWidget {
);
},
),
_button(
'Alert Dialog',
onPressed: () {
Navigator.of(context).push(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pushだけUtilのメソッドに変更して欲しい🙏

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正しやす🙆‍♂️

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d29fed3
修正した

MaterialPageRoute(
builder: (context) => const DebugAlertDialogPage(),
),
);
},
),
],
),
);
Expand Down
6 changes: 6 additions & 0 deletions lib/extensions/target_platform_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:flutter/material.dart';

extension TargetPlatformExtension on TargetPlatform {
bool get isIOS => this == TargetPlatform.iOS;
bool get isAndroid => this == TargetPlatform.android;
}
49 changes: 49 additions & 0 deletions lib/util/navigator_util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pg_mobile/extensions/target_platform_extension.dart';
import 'package:pg_mobile/widgets/common_cupertino_alert_dialog.dart';
import 'package:pg_mobile/widgets/common_material_alert_dialog.dart';

class NavigatorUtil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この名前だとrooting系の処理まとまってるクラスなのかなーって思ってしまうから
OSでモーダルの処理を分けてる旨が伝わる命名がベターだと思った
PlatformModalHandlerとかOSModalDispatcherとか

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かにそうやね…
クラス名をPlatformModalHandlerに変更するね

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c991d8e
修正した

/// OSに応じたAlertDialogを表示するメソッド
static void showCommonAlertDialog(
BuildContext context, {
required String titleText,
required String contentText,
String cancelText = 'キャンセル',
String okText = 'OK',
required void Function()? onPressedCancel,
required void Function()? onPressedOK,
bool hideCancel = false,
}) {
if (Theme.of(context).platform.isIOS) {
showCupertinoModalPopup<void>(
context: context,
builder: (BuildContext context) => CommonCupertinoAlertDialog(
titleText: titleText,
contentText: contentText,
cancelText: cancelText,
okText: okText,
onPressedCancel: onPressedCancel,
onPressedOK: onPressedOK,
hideCancel: hideCancel,
),
);
} else {
showDialog<void>(
context: context,
builder: (BuildContext context) {
return CommonMaterialAlertDialog(
titleText: titleText,
contentText: contentText,
cancelText: cancelText,
okText: okText,
onPressedCancel: onPressedCancel,
onPressedOK: onPressedOK,
hideCancel: hideCancel,
);
},
);
}
}
}
50 changes: 50 additions & 0 deletions lib/widgets/common_cupertino_alert_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:flutter/cupertino.dart';

class CommonCupertinoAlertDialog extends StatelessWidget {
const CommonCupertinoAlertDialog({
Key? key,
required this.titleText,
required this.contentText,
required this.cancelText,
required this.okText,
required this.onPressedCancel,
required this.onPressedOK,
required this.hideCancel,
}) : super(key: key);

final String titleText;
final String contentText;
final String cancelText;
final String okText;
final void Function()? onPressedCancel;
final void Function()? onPressedOK;
final bool hideCancel;

static const TextStyle _actionsTextStyle = TextStyle(
color: CupertinoColors.systemBlue,
);

bool get _showCancel => !hideCancel;

@override
Widget build(BuildContext context) {
return CupertinoAlertDialog(
title: Text(titleText),
content: Text(contentText),
actions: <CupertinoDialogAction>[
if (_showCancel)
CupertinoDialogAction(
onPressed: onPressedCancel,
textStyle: _actionsTextStyle,
child: Text(cancelText),
),
CupertinoDialogAction(
isDestructiveAction: true,
onPressed: onPressedOK,
textStyle: _actionsTextStyle,
child: Text(okText),
),
],
);
}
}
50 changes: 50 additions & 0 deletions lib/widgets/common_material_alert_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:flutter/material.dart';
import 'package:pg_mobile/constants/app_colors.dart';

class CommonMaterialAlertDialog extends StatelessWidget {
const CommonMaterialAlertDialog({
Key? key,
required this.titleText,
required this.contentText,
required this.cancelText,
required this.okText,
required this.onPressedCancel,
required this.onPressedOK,
required this.hideCancel,
}) : super(key: key);

final String titleText;
final String contentText;
final String cancelText;
final String okText;
final void Function()? onPressedCancel;
final void Function()? onPressedOK;
final bool hideCancel;

bool get _showCancel => !hideCancel;

@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(
titleText,
style: const TextStyle(color: AppColors.black),
),
content: Text(
contentText,
style: const TextStyle(color: AppColors.black),
),
actions: <Widget>[
if (_showCancel)
TextButton(
onPressed: onPressedCancel,
child: Text(cancelText),
),
TextButton(
onPressed: onPressedOK,
child: Text(okText),
),
],
);
}
}
Loading