This commit is contained in:
MohammadTaha Basiri 2022-01-11 18:47:38 +03:30
parent b3605b008c
commit 026c3a9f59
6 changed files with 104 additions and 66 deletions

View File

@ -8,19 +8,34 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class Authentication extends StatefulWidget {
const Authentication({Key? key}) : super(key: key);
final bool isResetPassword;
const Authentication({Key? key, required this.isResetPassword})
: super(key: key);
@override
State<Authentication> createState() => _AuthenticationState();
}
class _AuthenticationState extends State<Authentication> {
final List<Widget> _pages = const [
late final List<Widget> _pages;
@override
void initState() {
if (widget.isResetPassword) {
_pages = const [
Verification(),
ResetPassword(),
];
} else {
_pages = const [
UsernameInput(),
PasswordInput(),
Verification(),
ResetPassword(),
];
}
super.initState();
}
@override
Widget build(BuildContext context) {

View File

@ -1,6 +1,7 @@
import 'package:didvan/models/enums.dart';
import 'package:didvan/models/view/alert_data.dart';
import 'package:didvan/providers/core_provider.dart';
import 'package:didvan/providers/user_provider.dart';
import 'package:didvan/services/network/request.dart';
import 'package:didvan/services/network/request_helper.dart';
import 'package:didvan/utils/action_sheet.dart';
@ -35,7 +36,7 @@ class AuthenticationState extends CoreProvier {
}
}
Future<String?> login() async {
Future<String?> login(UserProvider userProvider) async {
appState = AppState.isolatedBusy;
final RequestService service = RequestService(
RequestHelper.login,
@ -44,8 +45,12 @@ class AuthenticationState extends CoreProvier {
);
await service.post();
if (service.isSuccess && service.result['loggedIn']) {
final token = service.result['token'];
appState = AppState.idle;
return service.result['token'];
await userProvider.setAndGetToken(token: token);
RequestService.token = token;
await userProvider.getUserInfo();
return token;
} else {
appState = AppState.failed;
ActionSheetUtils.showAlert(AlertData(message: service.errorMessage));

View File

@ -2,7 +2,6 @@ import 'package:didvan/pages/authentication/authentication_state.dart';
import 'package:didvan/pages/authentication/widgets/authentication_layout.dart';
import 'package:didvan/providers/user_provider.dart';
import 'package:didvan/routes/routes.dart';
import 'package:didvan/services/network/request.dart';
import 'package:didvan/widgets/didvan/button.dart';
import 'package:didvan/widgets/didvan/text.dart';
import 'package:didvan/widgets/didvan/text_field.dart';
@ -34,7 +33,7 @@ class _PasswordInputState extends State<PasswordInput> {
title: 'کلمه عبور',
hintText: 'کلمه عبور',
obsecureText: true,
validator: (value) => value!.length < 8
validator: (value) => value.length < 8
? 'کلمه عبور نمی‌تواند از 8 کاراکتر کمتر باشد'
: null,
),
@ -67,11 +66,9 @@ class _PasswordInputState extends State<PasswordInput> {
return;
}
final state = context.read<AuthenticationState>();
final token = await state.login();
if (token != null) {
final userProvider = context.read<UserProvider>();
await userProvider.setAndGetToken(token: token);
RequestService.token = token;
final token = await state.login(userProvider);
if (token != null) {
Navigator.of(context).pushReplacementNamed(Routes.home);
}
}

View File

@ -29,9 +29,14 @@ class _UsernameInputState extends State<UsernameInput> {
title: 'نام کاربری یا شماره موبایل',
hintText: 'نام کاربری یا شماره موبایل',
textAlign: TextAlign.center,
validator: (value) => value!.length < 4
? 'نام کاربری نمی‌تواند از 4 کاراکتر کوچکتر باشد'
: null,
validator: (value) {
if (value.contains(' ')) {
return 'نام کاربری باید بدون فاصله باشد';
}
if (value.length < 4) {
return 'نام کاربری نمی‌تواند از 4 کاراکتر کوچکتر باشد';
}
},
onChanged: (value) {
state.username = value;
},
@ -56,7 +61,7 @@ class _UsernameInputState extends State<UsernameInput> {
text: TextSpan(
style: Theme.of(context).textTheme.caption,
children: [
const TextSpan(text: 'با ثبت نام و ورود به دیدوان،'),
const TextSpan(text: 'با و ورود به دیدوان،'),
TextSpan(
text: ' شرایط ',
style: Theme.of(context)

View File

@ -1,5 +1,6 @@
import 'package:didvan/config/theme_data.dart';
import 'package:didvan/pages/authentication/authentication_state.dart';
import 'package:didvan/widgets/didvan/icon_button.dart';
import 'package:didvan/widgets/didvan/text.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
@ -10,19 +11,20 @@ class AuthenticationAppBar extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top + 12),
child: Row(
final state = context.read<AuthenticationState>();
return Row(
children: [
GestureDetector(
onTap: () => context.read<AuthenticationState>().currentPageIndex--,
child: Icon(
Icons.arrow_back,
color: Theme.of(context).colorScheme.title,
),
),
DidvanIconButton(
icon: Icons.arrow_back,
onPressed: () {
if (state.currentPageIndex == 0) {
Navigator.of(context).pop();
return;
}
state.currentPageIndex--;
}),
const SizedBox(
width: 20,
width: 4,
),
if (title != null)
DidvanText(
@ -31,7 +33,6 @@ class AuthenticationAppBar extends StatelessWidget {
color: Theme.of(context).colorScheme.title,
)
],
),
);
}
}

View File

@ -12,35 +12,50 @@ class AuthenticationLayout extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: SizedBox(
height: MediaQuery.of(context).size.height,
child: Column(
children: [
return CustomScrollView(
slivers: [
if (appBarTitle != null)
AuthenticationAppBar(
SliverAppBar(
automaticallyImplyLeading: false,
pinned: true,
toolbarHeight: 56,
backgroundColor: Theme.of(context).colorScheme.background,
flexibleSpace: Padding(
padding: EdgeInsets.only(
left: 16,
right: 16,
top: MediaQuery.of(context).padding.top,
),
child: AuthenticationAppBar(
title: appBarTitle,
),
if (appBarTitle == null)
const SizedBox(
height: 66,
),
const Padding(
),
SliverPadding(
padding: EdgeInsets.only(
top: 44,
top: appBarTitle == null ? 100 : 44,
left: 100,
right: 100,
bottom: 40,
),
sliver: const SliverToBoxAdapter(
child: DidvanVerticalLogo(
height: 200,
),
),
...children,
),
SliverPadding(
padding: const EdgeInsets.symmetric(horizontal: 20),
sliver: SliverFillRemaining(
hasScrollBody: false,
child: Column(
children: [
for (var i = 0; i < children.length; i++) children[i],
],
),
),
),
],
);
}
}