diff --git a/lib/pages/authentication/authentication.dart b/lib/pages/authentication/authentication.dart index 6c98ca8..cd06164 100644 --- a/lib/pages/authentication/authentication.dart +++ b/lib/pages/authentication/authentication.dart @@ -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 createState() => _AuthenticationState(); } class _AuthenticationState extends State { - final List _pages = const [ - UsernameInput(), - PasswordInput(), - Verification(), - ResetPassword(), - ]; + late final List _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) { diff --git a/lib/pages/authentication/authentication_state.dart b/lib/pages/authentication/authentication_state.dart index b6074ee..11e6eb2 100644 --- a/lib/pages/authentication/authentication_state.dart +++ b/lib/pages/authentication/authentication_state.dart @@ -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 login() async { + Future 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)); diff --git a/lib/pages/authentication/screens/password.dart b/lib/pages/authentication/screens/password.dart index bdd0953..ca8b5c0 100644 --- a/lib/pages/authentication/screens/password.dart +++ b/lib/pages/authentication/screens/password.dart @@ -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 { title: 'کلمه عبور', hintText: 'کلمه عبور', obsecureText: true, - validator: (value) => value!.length < 8 + validator: (value) => value.length < 8 ? 'کلمه عبور نمی‌تواند از 8 کاراکتر کمتر باشد' : null, ), @@ -67,11 +66,9 @@ class _PasswordInputState extends State { return; } final state = context.read(); - final token = await state.login(); + final userProvider = context.read(); + final token = await state.login(userProvider); if (token != null) { - final userProvider = context.read(); - await userProvider.setAndGetToken(token: token); - RequestService.token = token; Navigator.of(context).pushReplacementNamed(Routes.home); } } diff --git a/lib/pages/authentication/screens/username.dart b/lib/pages/authentication/screens/username.dart index be3dd43..0e7b1f6 100644 --- a/lib/pages/authentication/screens/username.dart +++ b/lib/pages/authentication/screens/username.dart @@ -29,9 +29,14 @@ class _UsernameInputState extends State { 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 { text: TextSpan( style: Theme.of(context).textTheme.caption, children: [ - const TextSpan(text: 'با ثبت نام و ورود به دیدوان،'), + const TextSpan(text: 'با و ورود به دیدوان،'), TextSpan( text: ' شرایط ', style: Theme.of(context) diff --git a/lib/pages/authentication/widgets/authentication_app_bar.dart b/lib/pages/authentication/widgets/authentication_app_bar.dart index 7061c00..03489c2 100644 --- a/lib/pages/authentication/widgets/authentication_app_bar.dart +++ b/lib/pages/authentication/widgets/authentication_app_bar.dart @@ -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,28 +11,28 @@ class AuthenticationAppBar extends StatelessWidget { @override Widget build(BuildContext context) { - return Padding( - padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top + 12), - child: Row( - children: [ - GestureDetector( - onTap: () => context.read().currentPageIndex--, - child: Icon( - Icons.arrow_back, - color: Theme.of(context).colorScheme.title, - ), - ), - const SizedBox( - width: 20, - ), - if (title != null) - DidvanText( - title!, - style: Theme.of(context).textTheme.subtitle2, - color: Theme.of(context).colorScheme.title, - ) - ], - ), + final state = context.read(); + return Row( + children: [ + DidvanIconButton( + icon: Icons.arrow_back, + onPressed: () { + if (state.currentPageIndex == 0) { + Navigator.of(context).pop(); + return; + } + state.currentPageIndex--; + }), + const SizedBox( + width: 4, + ), + if (title != null) + DidvanText( + title!, + style: Theme.of(context).textTheme.subtitle2, + color: Theme.of(context).colorScheme.title, + ) + ], ); } } diff --git a/lib/pages/authentication/widgets/authentication_layout.dart b/lib/pages/authentication/widgets/authentication_layout.dart index f15e7fe..4d063d4 100644 --- a/lib/pages/authentication/widgets/authentication_layout.dart +++ b/lib/pages/authentication/widgets/authentication_layout.dart @@ -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: [ - if (appBarTitle != null) - AuthenticationAppBar( + return CustomScrollView( + slivers: [ + if (appBarTitle != null) + 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( - padding: EdgeInsets.only( - top: 44, - left: 100, - right: 100, - bottom: 40, - ), - child: DidvanVerticalLogo( - height: 200, - ), ), - ...children, - ], + ), + SliverPadding( + padding: EdgeInsets.only( + top: appBarTitle == null ? 100 : 44, + left: 100, + right: 100, + bottom: 40, + ), + sliver: const SliverToBoxAdapter( + child: DidvanVerticalLogo( + height: 200, + ), + ), ), - ), + SliverPadding( + padding: const EdgeInsets.symmetric(horizontal: 20), + sliver: SliverFillRemaining( + hasScrollBody: false, + child: Column( + children: [ + for (var i = 0; i < children.length; i++) children[i], + ], + ), + ), + ), + ], ); } }