Files
flutterApp/lib/features/auth/presentation/pages/login_page_v2.dart

740 lines
25 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
import 'package:maibu_satabot_v2/core/localization/locale_cubit.dart';
import 'package:maibu_satabot_v2/components/language_selection_dialog.dart';
import 'package:maibu_satabot_v2/components/capsule_toast.dart';
import '../../../../core/router/route_paths.dart';
import '../bloc/login_cubit.dart';
import '../bloc/login_state.dart';
/// 登录页 V2 —— 按最新设计稿实现(账号 + 密码登录)。
///
/// 说明:
/// - 视觉:背景纵向浅蓝渐变、主按钮横向蓝色渐变胶囊、顶部 logo + 语言胶囊、
/// 中部机器人 Hero 卡片、白色表单卡片内含「登录 / 注册」分段 Tab。
/// - 逻辑:完全复用现有 [LoginCubit](账号密码登录,sourceType=2),未改动数据层。
/// - 「注册」暂不支持:点击注册 Tab 仅弹出胶囊提示,不跳转。
/// - 旧登录页 login_page.dart 原样保留,路由默认指向本页。
class LoginPageV2 extends StatefulWidget {
const LoginPageV2({super.key});
@override
State<LoginPageV2> createState() => _LoginPageV2State();
}
class _LoginPageV2State extends State<LoginPageV2>
with SingleTickerProviderStateMixin {
final _userCtrl = TextEditingController();
final _pwdCtrl = TextEditingController();
bool _isAgreed = false; // 协议勾选状态
bool _obscurePwd = true; // 密码可见性
int _selectedTab = 0; // 0=登录, 1=注册(注册仅提示,不切换内容)
// 进场动画控制器
late final AnimationController _entranceController;
// ===== 设计稿配色 =====
static const _bgTop = Color(0xFFFAFCFF);
static const _bgMid = Color(0xFFF0F6FE);
// 底部蓝比设计稿基准色 #EDF6FE 加深:原稿中/底两色几乎无差,渐变看不出层次
static const _bgBottom = Color(0xFFD6E9FB);
static const _navy = Color(0xFF1A2B4A); // 主标题/标签深色
static const _grayText = Color(0xFF86909C); // 次要文字
static const _primaryBlue = Color(0xFF1C96F6);
static const _inputBorder = Color(0xFFE5EDF7);
static const _tabBg = Color(0xFFEEF3FA);
static const _tabUnselectedText = Color(0xFF4E5969);
@override
void initState() {
super.initState();
_entranceController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 900),
);
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
_maybeShowLanguageDialog();
Future.delayed(const Duration(milliseconds: 120), () {
if (mounted) _entranceController.forward();
});
});
}
/// 进入登录页:弹出语言选择框,由用户当场决定(与旧页保持一致的强制选择体验)。
void _maybeShowLanguageDialog() {
showDialog(
context: context,
barrierDismissible: false,
builder: (_) => const LanguageSelectionDialog(mandatory: true),
);
}
@override
void dispose() {
_entranceController.dispose();
_userCtrl.dispose();
_pwdCtrl.dispose();
super.dispose();
}
Animation<double> _interval(double begin, double end) {
return CurvedAnimation(
parent: _entranceController,
curve: Interval(begin, end, curve: Curves.easeOut),
);
}
// 执行登录逻辑(复用现有 LoginCubit:账号 + 密码,sourceType=2)
void _handleLogin() {
context.read<LoginCubit>().login(_userCtrl.text, _pwdCtrl.text, 2);
}
void _onTabTap(int index) {
if (index == 1) {
// 注册暂不支持:仅提示,不切换内容
CapsuleToast.show('注册功能暂未开放', showCheck: false);
return;
}
setState(() => _selectedTab = 0);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: _bgTop,
body: DecoratedBox(
// 底色:纵向浅蓝渐变
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [_bgTop, _bgMid, _bgBottom],
stops: [0.0, 0.4, 1.0],
),
),
child: DecoratedBox(
// 左上角蓝色晕(对应设计稿 logo 一侧的淡蓝)
decoration: const BoxDecoration(
gradient: RadialGradient(
center: Alignment(-1.1, -1.0),
radius: 1.1,
colors: [Color(0xFFD6E8FC), Color(0x00D6E8FC)],
),
),
child: DecoratedBox(
// 右上角蓝色晕(对应设计稿顶部右侧的淡蓝)
decoration: const BoxDecoration(
gradient: RadialGradient(
center: Alignment(1.1, -0.9),
radius: 1.25,
colors: [Color(0xFFC7DFF8), Color(0x00C7DFF8)],
),
),
child: SafeArea(
bottom: false,
child: BlocListener<LoginCubit, LoginState>(
listener: (context, state) {
if (state is LoginSuccess) {
context.go(RoutePaths.home);
} else if (state is LoginFailure) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'${AppLocalizations.of(context).translate('login.login_failed')},${state.message}',
),
),
);
}
},
child: LayoutBuilder(
builder: (context, constraints) {
return SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: ConstrainedBox(
constraints:
BoxConstraints(minHeight: constraints.maxHeight),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 16),
// 顶部:logo + 语言胶囊
_Entrance(
animation: _interval(0.00, 0.35),
child: _buildTopBar(),
),
const SizedBox(height: 12),
// 欢迎标题 + 副标题
_Entrance(
animation: _interval(0.08, 0.43),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'欢迎使用飒沓机器人',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold,
color: _navy,
height: 1.2,
),
),
const SizedBox(height: 4),
const Text(
'统一管理您的机器人设备',
style: TextStyle(
fontSize: 13,
color: _grayText,
),
),
],
),
),
const SizedBox(height: 12),
// 机器人 Hero 卡片
_Entrance(
animation: _interval(0.16, 0.51),
child: _buildHeroCard(),
),
const SizedBox(height: 12),
// 白色表单卡片
_Entrance(
animation: _interval(0.26, 0.63),
child: _buildFormCard(),
),
const SizedBox(height: 12),
// 帮助与客服
_Entrance(
animation: _interval(0.40, 0.75),
child: Center(child: _buildHelpRow()),
),
const SizedBox(height: 16),
],
),
),
);
},
),
),
),
),
),
),
);
}
// ===== 顶部栏:logo(左)+ 语言胶囊(右)=====
// 注意:本页 LayoutBuilder 的 builder 在布局回调阶段执行,
// 不能在其子树里直接调用 context.select/watch(Provider 会断言失败)。
// 因此用 BlocBuilder 包裹 LocaleCubit 的读取,让它在正常 build 阶段取值。
Widget _buildTopBar() {
return BlocBuilder<LocaleCubit, Locale>(
builder: (context, locale) {
final langName = AppLanguages.instance.nameOf(locale);
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Image.asset(
// 中文用国内版 logo,英文用英文专版 logo(随语言切换实时更新)
locale.languageCode == 'zh'
? 'assets/images/logo_sata.png'
: 'assets/images/logo_stata-en.png',
height: 40,
fit: BoxFit.contain,
),
GestureDetector(
onTap: () {
showDialog(
context: context,
builder: (_) =>
const LanguageSelectionDialog(mandatory: false),
);
},
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 9),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(100),
border: Border.all(color: _inputBorder),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.language, size: 16, color: _primaryBlue),
const SizedBox(width: 6),
Text(
langName,
style: const TextStyle(fontSize: 13, color: _navy),
),
const SizedBox(width: 4),
const Icon(Icons.expand_more, size: 16, color: _grayText),
],
),
),
),
],
);
},
);
}
// ===== 机器人 Hero 卡片 =====
Widget _buildHeroCard() {
return ClipRRect(
borderRadius: BorderRadius.circular(16),
child: SizedBox(
height: 130,
width: double.infinity,
child: Stack(
fit: StackFit.expand,
children: [
Image.asset(
'assets/images/robot_logo_login.png',
fit: BoxFit.cover,
),
// 底部渐变遮罩,衬托文字
DecoratedBox(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.bottomCenter,
end: Alignment.center,
colors: [
Colors.black.withValues(alpha: 0.35),
Colors.transparent,
],
),
),
),
const Positioned(
left: 12,
bottom: 8,
child: Text(
'SMART ROBOTICS · CONNECTED',
style: TextStyle(
color: Colors.white,
fontSize: 11,
letterSpacing: 1.5,
fontWeight: FontWeight.w500,
),
),
),
],
),
),
);
}
// ===== 白色表单卡片 =====
Widget _buildFormCard() {
return Container(
width: double.infinity,
padding: const EdgeInsets.fromLTRB(16, 16, 16, 18),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: const Color(0xFF1C96F6).withValues(alpha: 0.06),
blurRadius: 20,
offset: const Offset(0, 6),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildSegmentTab(),
const SizedBox(height: 14),
// 账号
_buildFieldLabel('账号'),
const SizedBox(height: 6),
_buildTextField(
controller: _userCtrl,
icon: Icons.person_outline,
hint: AppLocalizations.of(context).translate('login.username_hint'),
),
const SizedBox(height: 12),
// 密码
_buildFieldLabel('密码'),
const SizedBox(height: 6),
_buildTextField(
controller: _pwdCtrl,
icon: Icons.lock_outline,
hint: AppLocalizations.of(context).translate('login.password_hint'),
obscureText: _obscurePwd,
suffix: IconButton(
icon: Icon(
_obscurePwd
? Icons.visibility_off_outlined
: Icons.visibility_outlined,
size: 20,
color: _grayText,
),
onPressed: () => setState(() => _obscurePwd = !_obscurePwd),
),
),
const SizedBox(height: 12),
_buildProtocolSection(),
const SizedBox(height: 4),
const Padding(
padding: EdgeInsets.only(left: 32),
child: Text(
'新账户注册前须主动勾选确认',
style: TextStyle(fontSize: 12, color: _grayText),
),
),
const SizedBox(height: 14),
// 登录按钮
BlocBuilder<LoginCubit, LoginState>(
builder: (context, state) {
final isLoading = state is LoginLoading;
return _GradientButton(
isLoading: isLoading,
onPressed: () {
if (!_isAgreed) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)
.translate('login.agree_first')),
duration: const Duration(seconds: 1),
),
);
return;
}
_handleLogin();
},
);
},
),
],
),
);
}
// ===== 登录 / 注册 分段 Tab =====
Widget _buildSegmentTab() {
return Container(
height: 42,
padding: const EdgeInsets.all(4),
decoration: BoxDecoration(
color: _tabBg,
borderRadius: BorderRadius.circular(12),
),
child: Row(
children: [
Expanded(child: _buildTabItem(0, '登录')),
Expanded(child: _buildTabItem(1, '注册')),
],
),
);
}
Widget _buildTabItem(int index, String label) {
final selected = _selectedTab == index;
return GestureDetector(
onTap: () => _onTabTap(index),
child: AnimatedContainer(
duration: const Duration(milliseconds: 200),
alignment: Alignment.center,
decoration: BoxDecoration(
gradient: selected
? const LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [Color(0xFF2BAFFE), Color(0xFF0B7DEE)],
)
: null,
borderRadius: BorderRadius.circular(9),
),
child: Text(
label,
style: TextStyle(
fontSize: 15,
fontWeight: selected ? FontWeight.w600 : FontWeight.w400,
color: selected ? Colors.white : _tabUnselectedText,
),
),
),
);
}
Widget _buildFieldLabel(String text) {
return Text(
text,
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: _navy,
),
);
}
Widget _buildTextField({
required TextEditingController controller,
required IconData icon,
required String hint,
bool obscureText = false,
Widget? suffix,
}) {
return TextField(
controller: controller,
obscureText: obscureText,
style: const TextStyle(fontSize: 14, color: _navy),
cursorColor: _primaryBlue,
decoration: InputDecoration(
hintText: hint,
hintStyle: const TextStyle(color: _grayText, fontSize: 14),
filled: true,
fillColor: Colors.white,
prefixIcon: Icon(icon, size: 20, color: _primaryBlue),
prefixIconConstraints:
const BoxConstraints(minWidth: 44, minHeight: 48),
suffixIcon: suffix,
contentPadding: const EdgeInsets.symmetric(vertical: 12),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: const BorderSide(color: _inputBorder),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: const BorderSide(color: _primaryBlue, width: 1.2),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: const BorderSide(color: _inputBorder),
),
),
);
}
// ===== 协议勾选 =====
Widget _buildProtocolSection() {
final loc = AppLocalizations.of(context);
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 24,
width: 24,
child: Checkbox(
value: _isAgreed,
activeColor: _primaryBlue,
checkColor: Colors.white,
side: const BorderSide(color: _inputBorder),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
onChanged: (val) => setState(() => _isAgreed = val!),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4),
),
),
),
const SizedBox(width: 8),
Expanded(
child: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
Text(
loc.translate('login.agree_prefix'),
style: const TextStyle(fontSize: 13, color: _grayText),
),
_protocolText(loc.translate('login.user_agreement_link'),
() => _showProtocolDetail(loc.translate('login.user_agreement_link'))),
Text(
loc.translate('login.agree_and'),
style: const TextStyle(fontSize: 13, color: _grayText),
),
_protocolText(loc.translate('login.privacy_policy_link'),
() => _showProtocolDetail(loc.translate('login.privacy_policy_link'))),
],
),
),
],
);
}
Widget _protocolText(String text, VoidCallback onTap) {
return GestureDetector(
onTap: onTap,
child: Text(
text,
style: const TextStyle(
fontSize: 13,
color: _primaryBlue,
fontWeight: FontWeight.w600,
),
),
);
}
Widget _buildHelpRow() {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.help_outline, size: 16, color: _grayText),
const SizedBox(width: 4),
GestureDetector(
onTap: () => CapsuleToast.show('帮助与客服功能开发中', showCheck: false),
child: const Text(
'帮助与客服',
style: TextStyle(fontSize: 13, color: _grayText),
),
),
],
);
}
void _showProtocolDetail(String title) {
showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.white,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
),
builder: (context) => Container(
padding: const EdgeInsets.all(24),
height: MediaQuery.of(context).size.height * 0.7,
child: Column(
children: [
Text(
title,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: _navy,
),
),
const Divider(height: 32, color: _inputBorder),
Expanded(
child: SingleChildScrollView(
child: Text(
(AppLocalizations.of(context)
.translate('login.protocol_detail_text')
.replaceAll('%s', title) +
'\n') *
20,
style: const TextStyle(
color: _tabUnselectedText,
height: 1.5,
),
),
),
),
const SizedBox(height: 16),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () => Navigator.pop(context),
style: ElevatedButton.styleFrom(
backgroundColor: _primaryBlue,
foregroundColor: Colors.white,
elevation: 0,
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: Text(AppLocalizations.of(context)
.translate('login.understood')),
),
),
],
),
),
);
}
}
/// 主按钮:横向蓝色渐变胶囊(#2BAFFE → #1C96F6 → #0B7DEE),带前置箭头。
class _GradientButton extends StatelessWidget {
const _GradientButton({required this.onPressed, required this.isLoading});
final VoidCallback onPressed;
final bool isLoading;
@override
Widget build(BuildContext context) {
final enabled = !isLoading;
return Opacity(
opacity: enabled ? 1 : 0.7,
child: GestureDetector(
onTap: enabled ? onPressed : null,
child: Container(
width: double.infinity,
height: 46,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(14),
gradient: const LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [Color(0xFF2BAFFE), Color(0xFF1C96F6), Color(0xFF0B7DEE)],
stops: [0.0, 0.5, 1.0],
),
boxShadow: [
BoxShadow(
color: const Color(0xFF1C96F6).withValues(alpha: 0.30),
blurRadius: 14,
offset: const Offset(0, 6),
),
],
),
child: isLoading
? const SizedBox(
width: 22,
height: 22,
child: CircularProgressIndicator(
strokeWidth: 2.4,
color: Colors.white,
),
)
: const Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.arrow_forward_rounded,
size: 20, color: Colors.white),
SizedBox(width: 8),
Text(
'登录',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: Colors.white,
letterSpacing: 1,
),
),
],
),
),
),
);
}
}
/// 进场动画包装:淡入 + 上滑。
class _Entrance extends StatelessWidget {
const _Entrance({required this.animation, required this.child});
final Animation<double> animation;
final Widget child;
@override
Widget build(BuildContext context) {
final curved =
CurvedAnimation(parent: animation, curve: Curves.easeOutCubic);
final slideAnim = Tween<Offset>(
begin: const Offset(0, 0.12),
end: Offset.zero,
).animate(curved);
return FadeTransition(
opacity: animation,
child: SlideTransition(position: slideAnim, child: child),
);
}
}