flutter-android
flutter
官网:Flutter文档 - Flutter中文网 (flutterchina.club)
flutter打包成安卓app
创建项目
flutter create 项目名
添加包命令
flutter pub add wifi_iot
运行命令
flutter run --no-sound-null-safety
打包命令
flutter build apk --no-sound-null-safety
导包错误:(Target of URI doesn’t exist)
flutter packages get
flutter创建安卓项目,webinpappview嵌入网址,并且需要申请相机和麦克风权限
依赖项:
dependencies:
flutter:
sdk: flutter
webview_flutter: ^0.3.10+4
flutter_inappwebview: ^5.3.2
permission_handler: 5.1.0+2
permission: 0.1.7
oktoast: 2.3.2
flutter_easyloading: ^3.0.5
#^6.1.6
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.5
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.VIDEO_CAPTURE" /> <uses-permission android:name="android.permission.AUDIO_CAPTURE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 相机权限 --> <uses-permission android:name="android.permission.CAMERA" /> <!-- 麦克风权限 --> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <!--.接收启动完成的广播权限--> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:permission_handler/permission_handler.dart'; Future main() async { WidgetsFlutterBinding.ensureInitialized(); // await Permission.microphone.request(); // await Permission.camera.request(); // 同时申请多个权限: await [ Permission.microphone, Permission.camera, ].request(); runApp(const MyApp()); } class MyApp extends StatefulWidget { const MyApp({super.key}); @override // ignore: library_private_types_in_public_api, unnecessary_new _MyAppState createState() => new _MyAppState(); } class _MyAppState extends State<MyApp> { @override Widget build(BuildContext context) { return const MaterialApp(home: InAppWebViewPage()); } } class InAppWebViewPage extends StatefulWidget { const InAppWebViewPage({super.key}); @override // ignore: library_private_types_in_public_api, unnecessary_new _InAppWebViewPageState createState() => new _InAppWebViewPageState(); } class _InAppWebViewPageState extends State<InAppWebViewPage> { // ignore: unused_field late InAppWebViewController _webViewController; @override Widget build(BuildContext context) { return Scaffold( // ignore: avoid_unnecessary_containers body: Container( child: Column(children: <Widget>[ Expanded( // ignore: avoid_unnecessary_containers child: Container( child: InAppWebView( initialUrlRequest: URLRequest( url: Uri.parse("https://kh.tyzfchina.com.cn:4433")), initialOptions: InAppWebViewGroupOptions( crossPlatform: InAppWebViewOptions( mediaPlaybackRequiresUserGesture: false, ), ), onWebViewCreated: (InAppWebViewController controller) { _webViewController = controller; }, androidOnPermissionRequest: (InAppWebViewController controller, String origin, List<String> resources) async { return PermissionRequestResponse( resources: resources, action: PermissionRequestResponseAction.GRANT); }), //该事件在WebView请求访问指定资源(即安卓原生WebChromeClient.onPermissionRequest event )的权限时触发。 ), ), ]))); } }
报错解决方案
flutter打包报错 Build failed due to use of deprecated Android v1 embedding.
原因:由于使用了已弃用的 Android v1 嵌入,构建失败。
解决:android/app/src/main下的找到AndroidManifest.xml修改application标签内容如下
<application android:name="${applicationName}" //修改之前的android:name="io.flutter.app.FlutterApplication"为这样 android:label="flutter_nest_page_view" android:icon="@mipmap/ic_launcher"> <meta-data //添加这个meta-data标签内容 android:name="flutterEmbedding" android:value="2" /> ... </application>
Flutter 3.3.6 中FlatButton, RaisedButton, & OutlineButton找不到
原因:
-
FlatButton
,RaisedButton
, andOutlineButton
这三个widget 已经有了新的控件替代,并且有新的主题做对应的替换即可
- 参考:(59条消息) Flutter 3.3.6 中FlatButton, RaisedButton, & OutlineButton找不到_闪亮的大早的博客-CSDN博客
FlatButton(
onPressed: onPressed,
child: Text('Button'),
// ...
);
RaisedButton(
onPressed: onPressed,
child: Text('Button'),
// ...
);
OutlineButton(
onPressed: onPressed,
child: Text('Button'),
// ...
);
TextButton(
onPressed: onPressed,
child: Text('Button'),
// ...
);
ElevatedButton(
onPressed: onPressed,
child: Text('Button'),
// ...
);
OutlinedButton(
onPressed: onPressed,
child: Text('Button'),
// ...
);
flutter Execution failed for task ‘:audioplayers:compileReleaseKotlin‘.
android->build.gradle文件,修改ext.kotlin_version = ‘1.5.10’
Error: Member not found: ’packageRoot’ in Flutter
flutter pub upgrade