我有这个登录页面,允许用户输入用户名、密码和登录名。基本上,当用户在两个文本字段中输入信息时,文本控制器将更新。当用户单击提交按钮时,它将运行一些登录过程(与此问题无关)。
我正在 Android Studio 上开发它,当我在 Chrome 网络上运行它时,它工作得很好,允许我输入文本。然而,当我在 Android 模拟器上执行此操作时,当我进入登录页面时,我会遇到大量错误。一开始会出现一些错误,但是当我打开 Android 触摸键盘时,在键入或删除字符时会出现错误。我的代码有问题吗?
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:wolfpackapp/models_services/teachassist_model.dart';
/*
#########################
#=-=-= LoginScreen =-=-=#
#########################
*/
class LoginScreen extends StatefulWidget {
const LoginScreen({super.key});
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final TextEditingController _usernameController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).colorScheme.primary,
extendBody: true,
body: SingleChildScrollView(
child: Column(
children: [
/*
##################
#=-=-= Form =-=-=#
##################
*/
SizedBox (
height: 350,
child: Padding(
padding: const EdgeInsets.all(50),
child: Column(
children: [
// Username
UsernameField(controller: _usernameController),
// Password
PasswordField(controller: _passwordController),
],
),
),
),
/*
####################
#=-=-= Button =-=-=#
####################
*/
Padding(
padding: const EdgeInsets.symmetric(horizontal: 50),
child: SizedBox(
width: double.infinity,
child: ElevatedButton(
style: Theme.of(context).elevatedButtonTheme.style,
child: Padding(
padding: const EdgeInsets.all(15),
child: Text('Sign In',
style: GoogleFonts.lato(
color: Theme.of(context).colorScheme.inversePrimary,
fontSize: 20, fontWeight: FontWeight.w600)),
),
onPressed: (){
String username = _usernameController.text.trim();
String password = _passwordController.text.trim();
authorizeUser(username,password);
},
),
),
),
],
),
),
);
}
}
/*
######################
#=-=-= Username =-=-=#
######################
*/
class UsernameField extends StatefulWidget {
final TextEditingController controller;
const UsernameField({
required this.controller,
super.key,
});
@override
State<UsernameField> createState() => _UsernameFieldState();
}
class _UsernameFieldState extends State<UsernameField> {
Color _colorText = Colors.grey;
@override
Widget build(BuildContext context) {
const defaultColor = Colors.grey;
final focusColor = Theme.of(context).colorScheme.secondary;
return Focus(
onFocusChange: (hasFocus) {
setState(() => _colorText = hasFocus ? focusColor : defaultColor);
},
child: TextField(
controller: widget.controller,
decoration: InputDecoration(
prefixIcon: Icon(Icons.person, color: _colorText),
labelText: "Student Number",
labelStyle: TextStyle(color: _colorText),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: _colorText)
),
),
),
);
}
}
/*
######################
#=-=-= Password =-=-=#
######################
*/
class PasswordField extends StatefulWidget {
final TextEditingController controller;
const PasswordField({
required this.controller,
super.key,
});
@override
State<PasswordField> createState() => _PasswordFieldState();
}
class _PasswordFieldState extends State<PasswordField> {
Color _colorText = Colors.grey;
@override
Widget build(BuildContext context) {
const defaultColor = Colors.grey;
final focusColor = Theme.of(context).colorScheme.secondary;
return Focus(
onFocusChange: (hasFocus) {
setState(() => _colorText = hasFocus ? focusColor : defaultColor);
},
child: TextField(
controller: widget.controller,
obscureText: true,
decoration: InputDecoration(
prefixIcon: Icon(Icons.lock, color: _colorText),
labelText: "Password",
labelStyle: TextStyle(color: _colorText),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: _colorText)
),
),
),
);
}
}
Performing hot restart...
Syncing files to device sdk gphone64 x86 64...
Restarted application in 1,222ms.
W/WindowOnBackDispatcher( 4191): OnBackInvokedCallback is not enabled for the application.
W/WindowOnBackDispatcher( 4191): Set 'android:enableOnBackInvokedCallback="true"' in the application manifest.
D/InsetsController( 4191): hide(ime(), fromIme=true)
D/InputConnectionAdaptor( 4191): The input method toggled cursor monitoring off
D/EGL_emulation( 4191): app_time_stats: avg=9214.08ms min=1.45ms max=128936.82ms count=14
I/ImeTracker( 4191): com.edisoncai.unionville_student_tools:f5b57e07: onRequestHide at ORIGIN_CLIENT reason HIDE_SOFT_INPUT_ON_ANIMATION_STATE_CHANGED fromUser false
I/ImeTracker( 4191): com.google.android.inputmethod.latin:9f05f268: onHidden
I/ImeTracker( 4191): com.edisoncai.unionville_student_tools:7153a72f: onRequestShow at ORIGIN_CLIENT reason SHOW_SOFT_INPUT fromUser false
D/InputMethodManager( 4191): showSoftInput() view=io.flutter.embedding.android.FlutterView{66c40f9 VFE...... .F...... 0,0-1344,2920 #1 aid=1073741824} flags=0 reason=SHOW_SOFT_INPUT
I/AssistStructure( 4191): Flattened final assist data: 456 bytes, containing 1 windows, 3 views
D/InputConnectionAdaptor( 4191): The input method toggled cursor monitoring on
D/InsetsController( 4191): show(ime(), fromIme=true)
D/EGL_emulation( 4191): app_time_stats: avg=2978.41ms min=13.51ms max=59255.83ms count=20
I/ImeTracker( 4191): com.edisoncai.unionville_student_tools:7153a72f: onShown
I/ImeTracker( 4191): com.edisoncai.unionville_student_tools:df6a6d1c: onRequestShow at ORIGIN_CLIENT reason SHOW_SOFT_INPUT fromUser false
D/InputMethodManager( 4191): showSoftInput() view=io.flutter.embedding.android.FlutterView{66c40f9 VFE...... .F...... 0,0-1344,2920 #1 aid=1073741824} flags=0 reason=SHOW_SOFT_INPUT
D/InputConnectionAdaptor( 4191): The input method toggled cursor monitoring on
I/AssistStructure( 4191): Flattened final assist data: 456 bytes, containing 1 windows, 3 views
D/InputConnectionAdaptor( 4191): The input method toggled cursor monitoring off
D/InputConnectionAdaptor( 4191): The input method toggled cursor monitoring on
D/InsetsController( 4191): show(ime(), fromIme=true)
I/ImeTracker( 4191): com.edisoncai.unionville_student_tools:df6a6d1c: onCancelled at PHASE_CLIENT_APPLY_ANIMATION
I/ImeTracker( 4191): com.edisoncai.unionville_student_tools:7424377f: onRequestShow at ORIGIN_CLIENT reason SHOW_SOFT_INPUT fromUser false
D/InputMethodManager( 4191): showSoftInput() view=io.flutter.embedding.android.FlutterView{66c40f9 VFE...... .F...... 0,0-1344,2920 #1 aid=1073741824} flags=0 reason=SHOW_SOFT_INPUT
I/AssistStructure( 4191): Flattened final assist data: 456 bytes, containing 1 windows, 3 views
D/InputConnectionAdaptor( 4191): The input method toggled cursor monitoring on
D/EGL_emulation( 4191): app_time_stats: avg=289.96ms min=65.88ms max=499.70ms count=4
D/InputConnectionAdaptor( 4191): The input method toggled cursor monitoring off
D/InputConnectionAdaptor( 4191): The input method toggled cursor monitoring on
D/InsetsController( 4191): show(ime(), fromIme=true)
I/ImeTracker( 4191): com.edisoncai.unionville_student_tools:7424377f: onCancelled at PHASE_CLIENT_APPLY_ANIMATION
I/ImeTracker( 4191): com.edisoncai.unionville_student_tools:dbae3d86: onRequestShow at ORIGIN_CLIENT reason SHOW_SOFT_INPUT fromUser false
D/InputMethodManager( 4191): showSoftInput() view=io.flutter.embedding.android.FlutterView{66c40f9 VFE...... .F...... 0,0-1344,2920 #1 aid=1073741824} flags=0 reason=SHOW_SOFT_INPUT
D/InsetsController( 4191): show(ime(), fromIme=true)
I/ImeTracker( 4191): com.edisoncai.unionville_student_tools:dbae3d86: onCancelled at PHASE_CLIENT_APPLY_ANIMATION
Application finished.
打印的第一个错误是:
W/WindowOnBackDispatcher( 4191): OnBackInvokedCallback is not enabled for the application.
W/WindowOnBackDispatcher( 4191): Set 'android:enableOnBackInvokedCallback="true"' in the application manifest.
您需要添加对 Android 14 的预测后退手势功能的支持来解决此问题(如果此步骤没有帮助,请查看链接的资源,可能有一些我错过的东西):
TargetPlatform.android: PredictiveBackPageTransitionsBuilder(),
的 MaterialApp
中添加 ThemeData
:return MaterialApp(
theme: ThemeData(
brightness: Brightness.light,
pageTransitionsTheme: const PageTransitionsTheme(
builders: {
// Use PredictiveBackPageTransitionsBuilder to get the predictive back route transition!
TargetPlatform.android: PredictiveBackPageTransitionsBuilder(),
},
),
),
home: const MyApp(),
);
android:enableOnBackInvokedCallback="true"
标签中的 android/app/src/main/AndroidManifest.xml
文件中设置 <application>
:<application
...
android:enableOnBackInvokedCallback="true"
... >
...
</application>
如果此页面上的用户体验或 UI 没有问题,则调试输出中的其他行可能只是模拟器日志。要隐藏它们,请查看这个 Stack Overflow 线程。希望这有帮助:)