框架/SDK版本:
Flutter: 3.10.4
Dart: 3.0.3
Run Code Online (Sandbox Code Playgroud)
这是我的main()代码:
Future<void> main() async {
//debugPaintSizeEnabled = true;
//BindingBase.debugZoneErrorsAreFatal = true;
WidgetsFlutterBinding.ensureInitialized();
EasyLocalization.ensureInitialized()
.then((value) => Fimber.plantTree(DebugTree()))
.then((value) => SentryFlutter.init(
(options) {
options.dsn = '***';
// Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
// We recommend adjusting this value in production.
options.tracesSampleRate = 1.0;
//options.attachScreenshot = true;
},
appRunner: () => runApp(
EasyLocalization(
supportedLocales: const [Locale('en', 'US'), Locale('de', 'DE')],
path: '../assets/translations/',
fallbackLocale: const Locale('en', 'US'),
assetLoader: const CodegenLoader(),
child: …Run Code Online (Sandbox Code Playgroud) 我无法使用com.google.android.material.bottomnavigation.BottomNavigationView设置导航组件,但它发出错误.
这是我尝试使用的代码
// Setup bottom navigation view
NavigationUI.setupWithNavController(
bottom_navigation_view,
findNavController(R.id.main_nav_host_fragment)
)
Run Code Online (Sandbox Code Playgroud)
当我打开NavigationUI类时,我注意到它只接受旧的android.support.design.widget.BottomNavigationView,但我想使用新的com.google.android.material.bottomnavigation.BottomNavigationView类.有什么想法吗?
我正在尝试颤振,目前正在关注本教程 https://www.youtube.com/watch?v=j6c_vHdbUfg
我注意到该应用程序在调试模式下运行良好,但是当我在手机上尝试 apk 时,该应用程序在加载屏幕后显示灰色屏幕。我不太熟悉在移动设备上构建应用程序,不确定是什么导致了这里的问题。我已经尝试使用教程中的 github 项目构建 apk,但它也有这个问题。https://github.com/iamshaunjp/flutter-beginners-tutorial/tree/lesson-35
我使用下面的代码将用户导航到设置屏幕以手动允许位置权限:
PermissionHandler().openAppSettings();
Run Code Online (Sandbox Code Playgroud)
一旦用户允许此权限,我就会检查是否授予该权限。如果获得批准,我将允许用户导航到下一个屏幕。
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
checkPermission(); //this will check the status of permission when the user returns back from the settings page.
}
checkPermission() async {
var location = Location();
bool _permission = false;
bool serviceStatus = await location.serviceEnabled();
if (serviceStatus) {
print("enable");
_permission = await location.requestPermission();
print("Permission result: $_permission");
if (_permission) {
// Navigate to next screen
}else{
print("permission not enable");
}
} else {
print("not enable");
}
}
Run Code Online (Sandbox Code Playgroud)
问题是didChangeAppLifecycleState屏幕上的任何操作总是会调用方法。当用户从后台导航到应用程序到前台或从设置屏幕导航到屏幕时,我应该如何检测状态。以下是状态,但这些都没有用。
resumedinActivate pauseddetached如果我们只是在 StatelessWidget 上使用 StatefullWidget,反之亦然,是否会对性能产生影响?
在我看来,我们只是StatefullWidget用于更新 UI 的一部分setState(),有一种方法可以在 中设置一些代码initState()并在dispose()函数中处理一些东西。因此,当我不需要这些东西时,我会继续使用StatelessWidget.
但是这两个最常用的小部件之间的真正性能影响是什么?
当文本表单字段的值发生变化时,如何自动验证它?
我试过
bool _autoValidate = false;
Run Code Online (Sandbox Code Playgroud)
TextFormField(
autovalidate: _autoValidate ,
onChanged: (value) {
setState(() {
_autoValidate = true;
});
},
validator: (value) {
if (value.length < 5) {
return 'False';
} else {
return 'True';
}
},
),
Run Code Online (Sandbox Code Playgroud)
但不起作用,TextFormField验证时仍然没有显示错误。
我需要一种方法来打开对更改的文本的验证。
我正在尝试创建一个拖放游戏。我想确保Draggable小部件在被拖动时不会离开屏幕。
我找不到这个特定问题的答案。有人问了一些关于约束可拖动区域的类似问题Constraining Draggable area但答案实际上并没有使用Draggable.
首先,我尝试在左侧实施限制。
我尝试将 Listener 与onPointerMove. 我已将此事件与 limitBoundaries 方法相关联,以检测何时Draggable从屏幕左侧退出。这部分正在工作,因为它确实在控制台中打印出来Offset时的值。我还将 setState 关联到此方法以设置可拖动的位置,但这不起作用。Draggable(position.dx < 0)Offset(0.0, position.dy)
有人可以帮我解决这个问题吗?
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Draggable Test',
home: GamePlay(),
);
}
}
class GamePlay extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
Row(
children: [
Container(
width: …Run Code Online (Sandbox Code Playgroud) 我正在编写一个扫描仪应用程序,该应用程序将安装在运行 Android 的扫描仪上。应用程序内部有一个TextFormField等待输入扫描或粘贴到里面的文本以进行其他 API 调用。
但是我没有找到任何TextFormField禁用软键盘的选项,但仍然可以接受输入文本
以下是我TextFormField尝试过的扫描仪小部件代码。
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class BuildScannerBar extends StatefulWidget {
final Function onFieldSubmitted;
final TextEditingController textFieldController;
final String labelText, hintText;
final bool disableKeyboard;
BuildScannerBar({
@required this.textFieldController,
@required this.onFieldSubmitted,
this.disableKeyboard = true,
this.labelText = 'Barcode Scan',
this.hintText = '',
});
@override
_BuildScannerBarState createState() => _BuildScannerBarState();
}
class _BuildScannerBarState extends State<BuildScannerBar> {
@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.topCenter,
child: Container(
height: 75,
margin: EdgeInsets.only(top: 50),
width: 300 …Run Code Online (Sandbox Code Playgroud) 我只是被困在这里。我已经尝试了一切,但对我没有任何帮助。我想要的只是将图标放在“行”的右侧。
注意:我尝试将行内的所有窗口小部件设置为Align Widget,并处理该Align小部件的位置,但是没有任何效果。下面的代码正在检索此代码:
此箭头应转到“行”小部件的末尾。这是我的代码:
Row(
mainAxisAlignment: MainAxisAlignment.start, //change here don't //worked
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
margin:
EdgeInsets.only(left: 8.0, top: 8.0, bottom: 8.0, right: 12.0),
width: 15.0,
height: 15.0,
decoration: BoxDecoration(
color: task.color, borderRadius: BorderRadius.circular(40.0)),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
task.title,
style: TextStyle(
color: Colors.black,
fontSize: 19.0,
fontWeight: FontWeight.bold),
),
Text(
'Duration: ${task.date}',
style: TextStyle(color: Colors.black, fontSize: 14.0),
)
],
),
Icon(Icons.navigate_next, color: Colors.black) // This Icon
],
),
Run Code Online (Sandbox Code Playgroud) 我有一串数字,我需要使用正则表达式替换它的前 N 位数字。
我尝试了以下代码,但它不起作用:
String hideLastFourCharacters(String s){
final result = s.replaceAll(r"\\d{2}", '-');
return result;
}
Run Code Online (Sandbox Code Playgroud) 请帮帮我。
我想使用LiveData.
OnChanged()在应用程序启动时触发一次,但是当我string1通过单击按钮更改值时,onChange()不会触发并且信息不会更新。TextView一直显示“哇”
我完全按照这里的描述做所有事情。
的ViewModel:
class CurrentViewModel : ViewModel() {
val currentName: MutableLiveData<String> by lazy {
MutableLiveData<String>()
}
}
Run Code Online (Sandbox Code Playgroud)
片段:
class CurrentFragment : Fragment(R.layout.current_fragment) {
private val viewModel: CurrentViewModel by viewModels()
var string1 = "Wow!"
override fun onActivityCreated(savedInstanceState: Bundle?)
val nameObserver = Observer<String> { newName ->
textview.text = newName }
viewModel.currentName.value = string1
viewModel.currentName.observe(activity!!, nameObserver)
button.setOnClickListener {
string1 = "some new string"
}
}
Run Code Online (Sandbox Code Playgroud)