Account page

This commit is contained in:
2024-05-19 00:56:32 +08:00
parent 2fad1067fa
commit 527db6b3bc
23 changed files with 737 additions and 37 deletions

View File

@@ -1,12 +1,166 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/router.dart';
import 'package:solian/widgets/account/account_avatar.dart';
class AccountScreen extends StatelessWidget {
class AccountScreen extends StatefulWidget {
const AccountScreen({super.key});
@override
State<AccountScreen> createState() => _AccountScreenState();
}
class _AccountScreenState extends State<AccountScreen> {
@override
Widget build(BuildContext context) {
return const Center(
child: Text("Woah account"),
final actionItems = [
// (const Icon(Icons.color_lens), 'personalize'.tr, 'account.personalize'),
// (const Icon(Icons.diversity_1), 'friend'.tr, 'account.friend'),
];
final AuthProvider provider = Get.find();
return Material(
color: Theme.of(context).colorScheme.background,
child: FutureBuilder(
future: provider.isAuthorized,
builder: (context, snapshot) {
if (!snapshot.hasData || snapshot.data == false) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ActionCard(
icon: const Icon(Icons.login, color: Colors.white),
title: 'signin'.tr,
caption: 'signinCaption'.tr,
onTap: () {
AppRouter.instance.pushNamed('signin').then((_) {
setState(() {});
});
},
),
ActionCard(
icon: const Icon(Icons.add, color: Colors.white),
title: 'signup'.tr,
caption: 'signupCaption'.tr,
onTap: () {
AppRouter.instance.pushNamed('signup');
},
),
],
),
);
}
return Column(
children: [
const AccountNameCard().paddingOnly(bottom: 8),
...(actionItems.map(
(x) => ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 34),
leading: x.$1,
title: Text(x.$2),
onTap: () {
AppRouter.instance.pushNamed(x.$3);
},
),
)),
ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 34),
leading: const Icon(Icons.logout),
title: Text('signout'.tr),
onTap: () {
provider.signout();
setState(() {});
},
),
],
);
},
),
);
}
}
class AccountNameCard extends StatelessWidget {
const AccountNameCard({super.key});
@override
Widget build(BuildContext context) {
final AuthProvider provider = Get.find();
return FutureBuilder(
future: provider.getProfile(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const Center(
child: CircularProgressIndicator(),
);
}
return Material(
elevation: 2,
child: ListTile(
contentPadding: const EdgeInsets.only(left: 22, right: 34, top: 4, bottom: 4),
leading: AccountAvatar(content: snapshot.data!.body?['avatar'], radius: 24),
title: Text(snapshot.data!.body?['nick']),
subtitle: Text(snapshot.data!.body?['name']),
),
);
},
);
}
}
class ActionCard extends StatelessWidget {
final Widget icon;
final String title;
final String caption;
final Function onTap;
const ActionCard({
super.key,
required this.onTap,
required this.title,
required this.caption,
required this.icon,
});
@override
Widget build(BuildContext context) {
return Card(
child: InkWell(
borderRadius: BorderRadius.circular(10),
onTap: () => onTap(),
child: Container(
width: 320,
padding: const EdgeInsets.all(20),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CircleAvatar(
backgroundColor: Colors.indigo,
child: icon,
).paddingOnly(bottom: 12),
Text(
title,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.w900,
overflow: TextOverflow.clip,
),
),
Text(
caption,
style: const TextStyle(overflow: TextOverflow.clip),
),
],
),
),
),
);
}
}

View File

@@ -0,0 +1,120 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/router.dart';
import 'package:solian/services.dart';
import 'package:url_launcher/url_launcher_string.dart';
class SignInScreen extends StatefulWidget {
const SignInScreen({super.key});
@override
State<SignInScreen> createState() => _SignInScreenState();
}
class _SignInScreenState extends State<SignInScreen> {
final _usernameController = TextEditingController();
final _passwordController = TextEditingController();
void performAction(BuildContext context) {
final AuthProvider provider = Get.find();
final username = _usernameController.value.text;
final password = _passwordController.value.text;
if (username.isEmpty || password.isEmpty) return;
provider.signin(context, username, password).then((_) {
AppRouter.instance.pop(true);
}).catchError((e) {
List<String> messages = e.toString().split('\n');
if (messages.last.contains('risk')) {
final ticketId = RegExp(r'ticketId=(\d+)').firstMatch(messages.last);
if (ticketId == null) {
Get.showSnackbar(GetSnackBar(
title: 'errorHappened'.tr,
message: 'requested to multi-factor authenticate, but the ticket id was not found',
));
}
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('riskDetection'.tr),
content: Text('signinRiskDetected'.tr),
actions: [
TextButton(
child: Text('next'.tr),
onPressed: () {
launchUrlString(
'${ServiceFinder.services['passport']}/mfa?ticket=${ticketId!.group(1)}',
);
if (Navigator.canPop(context)) {
Navigator.pop(context);
}
},
)
],
);
},
);
} else {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(messages.last),
));
}
});
}
@override
Widget build(BuildContext context) {
return Material(
color: Theme.of(context).colorScheme.background,
child: Center(
child: Container(
width: MediaQuery.of(context).size.width * 0.6,
constraints: const BoxConstraints(maxWidth: 360),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
padding: const EdgeInsets.symmetric(vertical: 16),
child: Image.asset('assets/logo.png', width: 72, height: 72),
),
TextField(
autocorrect: false,
enableSuggestions: false,
controller: _usernameController,
autofillHints: const [AutofillHints.username],
decoration: InputDecoration(
isDense: true,
border: const OutlineInputBorder(),
labelText: 'username'.tr,
),
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
),
const SizedBox(height: 12),
TextField(
obscureText: true,
autocorrect: false,
enableSuggestions: false,
autofillHints: const [AutofillHints.password],
controller: _passwordController,
decoration: InputDecoration(
isDense: true,
border: const OutlineInputBorder(),
labelText: 'password'.tr,
),
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
onSubmitted: (_) => performAction(context),
),
const SizedBox(height: 16),
ElevatedButton(
child: Text('signin'.tr),
onPressed: () => performAction(context),
)
],
),
),
),
);
}
}

View File

@@ -0,0 +1,147 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:solian/router.dart';
import 'package:solian/services.dart';
class SignUpScreen extends StatefulWidget {
const SignUpScreen({super.key});
@override
State<SignUpScreen> createState() => _SignUpScreenState();
}
class _SignUpScreenState extends State<SignUpScreen> {
final _emailController = TextEditingController();
final _usernameController = TextEditingController();
final _nicknameController = TextEditingController();
final _passwordController = TextEditingController();
void performAction(BuildContext context) async {
final email = _emailController.value.text;
final username = _usernameController.value.text;
final nickname = _passwordController.value.text;
final password = _passwordController.value.text;
if (email.isEmpty ||
username.isEmpty ||
nickname.isEmpty ||
password.isEmpty) return;
final client = GetConnect();
client.httpClient.baseUrl = ServiceFinder.services['passport'];
final res = await client.post('/api/users', {
'name': username,
'nick': nickname,
'email': email,
'password': password,
});
if (res.statusCode == 200) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('signupDone'.tr),
content: Text('signupDoneCaption'.tr),
actions: [
TextButton(
child: Text('confirmOkay'.tr),
onPressed: () => Navigator.pop(context),
),
],
);
},
).then((_) {
AppRouter.instance.replaceNamed('auth.sign-in');
});
} else {
Get.showSnackbar(GetSnackBar(
title: 'errorHappened'.tr,
message: res.bodyString,
));
}
}
@override
Widget build(BuildContext context) {
return Material(
color: Theme.of(context).colorScheme.background,
child: Center(
child: Container(
width: MediaQuery.of(context).size.width * 0.6,
constraints: const BoxConstraints(maxWidth: 360),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
padding: const EdgeInsets.symmetric(vertical: 16),
child: Image.asset('assets/logo.png', width: 72, height: 72),
),
TextField(
autocorrect: false,
enableSuggestions: false,
controller: _usernameController,
autofillHints: const [AutofillHints.username],
decoration: InputDecoration(
isDense: true,
border: const OutlineInputBorder(),
labelText: 'username'.tr,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
),
const SizedBox(height: 12),
TextField(
autocorrect: false,
enableSuggestions: false,
controller: _nicknameController,
autofillHints: const [AutofillHints.nickname],
decoration: InputDecoration(
isDense: true,
border: const OutlineInputBorder(),
labelText: 'nickname'.tr,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
),
const SizedBox(height: 12),
TextField(
autocorrect: false,
enableSuggestions: false,
controller: _emailController,
autofillHints: const [AutofillHints.email],
decoration: InputDecoration(
isDense: true,
border: const OutlineInputBorder(),
labelText: 'email'.tr,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
),
const SizedBox(height: 12),
TextField(
obscureText: true,
autocorrect: false,
enableSuggestions: false,
autofillHints: const [AutofillHints.password],
controller: _passwordController,
decoration: InputDecoration(
isDense: true,
border: const OutlineInputBorder(),
labelText: 'password'.tr,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
onSubmitted: (_) => performAction(context),
),
const SizedBox(height: 16),
ElevatedButton(
child: Text('signup'.tr),
onPressed: () => performAction(context),
)
],
),
),
),
);
}
}

View File

@@ -52,23 +52,26 @@ class _HomeScreenState extends State<HomeScreen> {
);
}
return RefreshIndicator(
onRefresh: () {
_data.clear();
_pageKey = 0;
_dataTotal = null;
return getPosts();
},
child: ListView.separated(
itemCount: _data.length,
itemBuilder: (BuildContext context, int index) {
final item = _data[index];
return GestureDetector(
child: PostItem(key: Key('p${item.alias}'), item: item),
onTap: () {},
);
return Material(
color: Theme.of(context).colorScheme.background,
child: RefreshIndicator(
onRefresh: () {
_data.clear();
_pageKey = 0;
_dataTotal = null;
return getPosts();
},
separatorBuilder: (_, __) => const Divider(thickness: 0.3, height: 0.3),
child: ListView.separated(
itemCount: _data.length,
itemBuilder: (BuildContext context, int index) {
final item = _data[index];
return GestureDetector(
child: PostItem(key: Key('p${item.alias}'), item: item),
onTap: () {},
);
},
separatorBuilder: (_, __) => const Divider(thickness: 0.3, height: 0.3),
),
),
);
}