Skip to content

Commit c7d6f60

Browse files
feat(dart): improve sign in experience (#15)
Co-authored-by: simeng-li <simeng@silverhand.io>
1 parent bbb0f6c commit c7d6f60

File tree

11 files changed

+232
-170
lines changed

11 files changed

+232
-170
lines changed

example/android/app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
2626
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
2727

2828
android {
29-
compileSdkVersion flutter.compileSdkVersion
29+
compileSdkVersion 33
3030
ndkVersion flutter.ndkVersion
3131

3232
compileOptions {
@@ -47,7 +47,7 @@ android {
4747
applicationId "com.example.example"
4848
// You can update the following values to match your application needs.
4949
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
50-
minSdkVersion flutter.minSdkVersion
50+
minSdkVersion 19
5151
targetSdkVersion flutter.targetSdkVersion
5252
versionCode flutterVersionCode.toInteger()
5353
versionName flutterVersionName
Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.example.example">
3-
<application
4-
android:label="example"
5-
android:name="${applicationName}"
6-
android:icon="@mipmap/ic_launcher">
2+
package="com.example.example">
3+
<application
4+
android:label="example"
5+
android:name="${applicationName}"
6+
android:icon="@mipmap/ic_launcher">
77
<activity
8-
android:name=".MainActivity"
9-
android:exported="true"
10-
android:launchMode="singleTop"
11-
android:theme="@style/LaunchTheme"
12-
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
13-
android:hardwareAccelerated="true"
14-
android:windowSoftInputMode="adjustResize">
8+
android:name=".MainActivity"
9+
android:exported="true"
10+
android:launchMode="singleTop"
11+
android:theme="@style/LaunchTheme"
12+
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
13+
android:hardwareAccelerated="true"
14+
android:windowSoftInputMode="adjustResize">
1515
<!-- Specifies an Android theme to apply to this Activity as soon as
1616
the Android process has started. This theme is visible to the user
1717
while the Flutter UI initializes. After that, this theme continues
1818
to determine the Window background behind the Flutter UI. -->
1919
<meta-data
20-
android:name="io.flutter.embedding.android.NormalTheme"
21-
android:resource="@style/NormalTheme"
22-
/>
20+
android:name="io.flutter.embedding.android.NormalTheme"
21+
android:resource="@style/NormalTheme"
22+
/>
2323
<intent-filter>
2424
<action android:name="android.intent.action.MAIN"/>
2525
<category android:name="android.intent.category.LAUNCHER"/>
2626
</intent-filter>
2727
</activity>
28+
29+
<activity android:name="com.linusu.flutter_web_auth.CallbackActivity" android:exported="false">
30+
<intent-filter android:label="flutter_web_auth">
31+
<action android:name="android.intent.action.VIEW"/>
32+
<category android:name="android.intent.category.DEFAULT"/>
33+
<category android:name="android.intent.category.BROWSABLE"/>
34+
<data android:scheme="io.logto"/>
35+
</intent-filter>
36+
</activity>
2837
<!-- Don't delete the meta-data below.
2938
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
3039
<meta-data
31-
android:name="flutterEmbedding"
32-
android:value="2" />
40+
android:name="flutterEmbedding"
41+
android:value="2"/>
3342
</application>
3443
</manifest>

example/lib/main.dart

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:flutter/material.dart';
2-
import 'package:http/http.dart' as http;
32
import 'package:logto_dart_sdk/logto_dart_sdk.dart';
3+
import 'package:http/http.dart' as http;
44

55
void main() async {
66
WidgetsFlutterBinding.ensureInitialized();
@@ -37,10 +37,10 @@ class MyHomePage extends StatefulWidget {
3737
}
3838

3939
class _MyHomePageState extends State<MyHomePage> {
40-
String content = 'Logto SDK Demo Home Page';
41-
bool isAuthenticated = false;
40+
static String welcome = 'Logto SDK Demo Home Page';
41+
String? content;
42+
bool? isAuthenticated;
4243

43-
final client = http.Client();
4444
final redirectUri = 'io.logto://callback';
4545
final config = const LogtoConfig(
4646
appId: 'xgSxW0MDpVqW2GDvCnlNb', endpoint: 'https://logto.dev');
@@ -60,32 +60,51 @@ class _MyHomePageState extends State<MyHomePage> {
6060
content = claims!.toJson().toString();
6161
isAuthenticated = true;
6262
});
63+
return;
6364
}
65+
setState(() {
66+
content = '';
67+
isAuthenticated = false;
68+
});
6469
}
6570

6671
void _init() async {
67-
logtoClient = LogtoClient(config, client);
68-
render();
69-
}
70-
71-
void signInCallback() {
72+
logtoClient = LogtoClient(
73+
config: config,
74+
httpClient: http.Client(),
75+
);
7276
render();
7377
}
7478

7579
@override
7680
Widget build(BuildContext context) {
7781
Widget signInButton = TextButton(
7882
style: TextButton.styleFrom(
83+
foregroundColor: Colors.white,
7984
backgroundColor: Colors.deepPurpleAccent,
8085
padding: const EdgeInsets.all(16.0),
8186
textStyle: const TextStyle(fontSize: 20),
8287
),
83-
onPressed: () {
84-
logtoClient.signIn(context, redirectUri, signInCallback);
88+
onPressed: () async {
89+
await logtoClient.signIn(redirectUri);
90+
render();
8591
},
8692
child: const Text('Sign In'),
8793
);
8894

95+
Widget signOutButton = TextButton(
96+
style: TextButton.styleFrom(
97+
foregroundColor: Colors.black,
98+
padding: const EdgeInsets.all(16.0),
99+
textStyle: const TextStyle(fontSize: 20),
100+
),
101+
onPressed: () async {
102+
await logtoClient.signOut(redirectUri: redirectUri);
103+
render();
104+
},
105+
child: const Text('Sign Out'),
106+
);
107+
89108
return Scaffold(
90109
appBar: AppBar(
91110
title: Text(widget.title),
@@ -94,23 +113,23 @@ class _MyHomePageState extends State<MyHomePage> {
94113
child: Column(
95114
mainAxisAlignment: MainAxisAlignment.center,
96115
children: <Widget>[
116+
SelectableText(welcome,
117+
style:
118+
const TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
97119
Container(
98120
padding: const EdgeInsets.all(64),
99121
child: SelectableText(
100-
content,
122+
content ?? '',
101123
),
102124
),
103-
// TODO: show signout button
104-
isAuthenticated ? signInButton : signInButton,
125+
isAuthenticated != null
126+
? isAuthenticated == true
127+
? signOutButton
128+
: signInButton
129+
: const SizedBox.shrink()
105130
],
106131
),
107132
),
108133
);
109134
}
110-
111-
@override
112-
void dispose() {
113-
client.close();
114-
super.dispose();
115-
}
116135
}

example/pubspec.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ packages:
172172
description: flutter
173173
source: sdk
174174
version: "0.0.0"
175+
flutter_web_auth:
176+
dependency: transitive
177+
description:
178+
name: flutter_web_auth
179+
url: "https://pub.dartlang.org"
180+
source: hosted
181+
version: "0.4.1"
175182
flutter_web_plugins:
176183
dependency: transitive
177184
description: flutter

0 commit comments

Comments
 (0)