当前位置:   article > 正文

Flutter知识点_flutter lock

flutter lock

flutter组件库


Flutter 命令行创建项目

可以指定语言: -i -> iOS, -a -> android

  1. flutter create -i objc -a java #项目名#
  2. flutter create -i swift -a kotlin #项目名#

创建完项目之后, cd到项目文件夹下

  1. // 查看支持的设备
  2. flutter devices
  3. // 打开模拟器
  4. flutter emulators --launch apple_ios_simulator
  5. //运行项目
  6. flutter run
  7. //flutter 清理项目
  8. flutter clean

Column中嵌套Column或者Row中嵌套Row
https://blog.csdn.net/senkai123/article/details/102667479


Flutter 三种方式实现页面切换后保持原页面状态
https://zhuanlan.zhihu.com/p/58582876

flutter中,保存页面的状态issue

使用上面文章中的方法仅仅可以保存 “没有导航栏跳转的情况”
如果在页面中放置一个按钮然后跳转到子页面,此时外部部分页面如果不做额外处理,则会销毁。

处理方法:

  1. 除了要在需要保存状态的widget中混入 AutomaticKeepAliveClientMixin,然后重写 方法:
  2. @override
  3. bool get wantKeepAlive => true;
  4. 还要在页面的build方法中,调用 super.build(context); 方法

flutter中使用InkWell给任意Widget添加点击事件


flutter截长图
Flutter之屏幕截图/组件截图 - 简书


json -> dart
https://javiercbk.github.io/json_to_dart/


父控件根据子控件高度自适应
① column中放不可滚动的组件,设置mainAxisSize: MainAxisSize.min即可
② column中放ListView等可以滚动的组件,则除了要设置主轴Size为MainAxisSize.min之外,还要设置ListView的shrinkWrap: true,

  1. 这个属性是大部门可以滚动的组件(ListView,GridView)的父类ScrollView的属性,用来确定scrollview的内容高度是否由子组件决定
  2. /// Whether the extent of the scroll view in the [scrollDirection] should be
  3. /// determined by the contents being viewed.
  4. /// Defaults to false.
  5. final bool shrinkWrap;

③ 在自定义iOS样式的AlertController的时候,我使用了AlertDialog -> Column -> ListView这样的布局方式,结果发现会报错
RenderShrinkWrappingViewport does not support returning intrinsic dimensions

Flutter之Dialog使用和踩坑
flutter Dialog里ListView的问题
flutter 自定义弹框
Flutter: ListView in a SimpleDialog


Column、Row对齐方式中spaceBetween, spaceEvenly, spaceAround的对比


flutter调用iOS中api修改:

  1. import UIKit
  2. import Flutter
  3. @UIApplicationMain
  4. @objc class AppDelegate: FlutterAppDelegate {
  5. override func application(
  6. _ application: UIApplication,
  7. didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  8. ) -> Bool {
  9. GeneratedPluginRegistrant.register(with: self)
  10. let controller : FlutterViewController = window?.rootViewController as! FlutterViewController;
  11. let batteryChannel = FlutterMethodChannel.init(name: "com.flutter.io/wififx",
  12. binaryMessenger: controller.binaryMessenger);
  13. batteryChannel.setMethodCallHandler({
  14. (call: FlutterMethodCall, result: FlutterResult) -> Void in
  15. // Handle battery messages.
  16. if ("getBatteryLevel" == call.method) {
  17. self.receiveBatteryLevel(result: result);
  18. } else {
  19. result(FlutterMethodNotImplemented);
  20. }
  21. });
  22. return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  23. }
  24. private func receiveBatteryLevel(result: FlutterResult) {
  25. let device = UIDevice.current;
  26. device.isBatteryMonitoringEnabled = true;
  27. if (device.batteryState == .unknown) {
  28. result(FlutterError.init(code: "UNAVAILABLE",
  29. message: "电池信息不可用",
  30. details: nil));
  31. } else {
  32. result(Int(device.batteryLevel * 100));
  33. }
  34. }
  35. }

https://github.com/flutter/flutter/issues/35945


Android Studio打开flutter项目的时候,设备列表那里一直是loading。

解决方法: 全凭运气。。。

  1. 查找带dart的进程,杀掉,然后重启as

  2. 此时命令行执行flutter doctor, 偶然间好了。。

  3. 删除 flutter/bin/cache/路径下的lockfile、flutter.bat.lock文件

  4. 重启电脑

  5. 等着吧。。

https://blog.csdn.net/yueyang6019bo123/article/details/103753239

https://stackoverflow.com/questions/51101178/android-studio-device-list-stuck-on-loading

https://github.com/flutter/flutter-intellij/issues/3892


Flutter i18n 国际化
Flutter_i18n国际化全解 - 简书


Sliver效果的列表添加下拉刷新
https://juejin.im/post/5beb91275188251d9e0c1d73


Use Flutter in China
https://flutter.dev/community/china


属性传值、InheritedWidget、Notification、EventBus区别


原生项目集成flutter之后,页面跳转管理
https://pub.flutter-io.cn/packages/flutter_boost


flutter切换字体
https://blog.csdn.net/emdd2016/article/details/93312143


Android Studio Flutter Shortcut


Flex/Row/Column、 Flexible/Expanded
①. Row、Column 都是继承于 Flex的组件, 可以设置子widget的对齐方式
②. Expanded是继承于Flexible的widget,可以看做 flex为1,fit为FlexFit.tight 的Flexible组件
Expanded可以放到①中的组件中:

  1. 如果①中子widget有非Flexible的组件,则剩余部分根据Flexible的flex属性,来分配。
  2. 如果①中的子widget全都是Flexible的组件,则按照flex的值来分配。

flutter 分享
https://pub.flutter-io.cn/packages/share
Sharesdk


Flutter项目编译报错
Code Sign Error in macOS High Sierra Xcode - resource fork, Finder information, or similar detritus not allowed

Code signing fails with error 'resource fork, Finder information, or similar detritus not allowed' - 简书
https://developer.apple.com/library/archive/qa/qa1940/_index.html

resource fork, Finder information, or similar detritus not allowed - 简书

  1. cd到你的项目文件夹下;
  2. 在终端执行下面的代码:
  3. xattr -rc .
  4. // 别忘了后面是有一个点.的
  5. 然后,执行 flutter run 就OK了。

相关命令
查看造成错误的文件

xattr -lr <fxeye.app文件的路径>

删除造成错误的文件

xattr -cr <fxeye.app文件路径>

Future、 async、 await

结论:
async是标志方法需要一段时间才能得到返回值。
await字面上意思是 等待异步的方法执行完成之后再执行后续代码(会阻塞当前方法)

https://dart.dev/codelabs/async-await#completed
https://blog.csdn.net/yuzhiqiang_1993/article/details/89155870
https://medium.com/@jelenaaa.lecic/when-to-use-async-await-then-and-future-in-dart-5e00e64ab9b1
https://zhuanlan.zhihu.com/p/39999205
https://dart.cn/tutorials/language/futures


Stack的宽高,根据子组件来变化
https://segmentfault.com/q/1010000022210409?utm_source=tag-newest
https://stackoverflow.com/questions/51123939/flutter-stack-size-to-sibling
https://stackoverflow.com/questions/45745448/how-do-i-stretch-an-image-to-fit-the-whole-background-100-height-x-100-width/45745479#45745479

Stack/ IndexedStack
一篇带你看懂Flutter叠加组件Stack


自定义bottomNavigationBar 使用Lottie动画组件

参考插件
FF-Navigation bar
Bottom personalized bot bar
Titled Bottom Navigation Bar
Bubbled Navigation Bar
Bottom NavyBar


Flutter 类中创建私有变量

  1. class FXTabbarItem extends StatefulWidget {
  2. /// tabbarItem 标题
  3. String _title;
  4. /// json文件路径
  5. String _jsonFile;
  6. Key _itemKey;
  7. /// 点击回调
  8. VoidCallback _onTapCallBack;
  9. /// 文字常规样式
  10. final TextStyle titleNormalStyle;
  11. /// 文字选中样式
  12. final TextStyle titleSelectStyle;
  13. /// 是否是选中状态
  14. final bool isSelected;
  15. FXTabbarItem({
  16. @required String title,
  17. @required String jsonFile,
  18. @required Key itemKey,
  19. @required VoidCallback onTap,
  20. this.isSelected,
  21. this.titleNormalStyle,
  22. this.titleSelectStyle,
  23. }) : _title = title,
  24. _jsonFile = jsonFile,
  25. _itemKey = itemKey,
  26. _onTapCallBack = onTap;
  27. @override
  28. _FXTabbarItemState createState() => _FXTabbarItemState();
  29. }

如果还有super继承的

  1. class FXDealerDetailHeader extends StatefulWidget {
  2. final FXTraderModel _traderModel;
  3. FXDealerDetailHeader({Key key, FXTraderModel traderModel})
  4. : _traderModel = traderModel,
  5. super(key: key);
  6. @override
  7. _FXDealerDetailHeaderState createState() => _FXDealerDetailHeaderState();
  8. }

Flutter 获取组件的frame信息

  1. 使用GlobalKey,在帧绘制回调的时候获取组件的frame

WidgetsBinding 提供了单次 Frame 绘制回调及实时 Frame 绘制回调两种机制

  • 单次 Frame 绘制回调:通过 addPostFrameCallback 实现。在当前 Frame 绘制完后进行回调,且只会回调一次,如果需要多次回调则需设置多次

  1. WidgetsBinding.instance.addPostFrameCallback((_){
  2. print("addPostFrameCallback 绘制回调"); // 只回调一次
  3. });
  • 实时 Frame 绘制回调:通过 addPersistentFrameCallback 实现。在每次绘制 Frame 结束后进行回调

  1. WidgetsBinding.instance.addPersistentFrameCallback((_){
  2. print("addPersistentFrameCallback 绘制回调"); // 每帧都回调
  3. });
  1. 使用Builder组件

  1. Builder(
  2. builder: (context) {
  3. return IconButton(
  4. icon: Icon(Icons.flight),
  5. onPressed: () {
  6. /// 通过 Builder 组件获取 context即element,
  7. /// 再通过element获取renderObject, 即可拿到组件信息
  8. RenderBox box = context.findRenderObject() as RenderBox;
  9. var offset = box.localToGlobal(Offset.zero);
  10. var size = box.currentContext.size
  11. },
  12. );
  13. },
  14. )

Flutter 文字下面显示双黄线

原因: Text组件,默认使用的是Material节点中的TextStyle,所以我们可以在Text组件外面包裹Material组件

  1. Scaffold

  1. Widget build(BuildContext context) {
  2. return Scaffold(
  3. appBar: AppBar(
  4. title: const Text('Sample Code'),
  5. ),
  6. body: Center(
  7. child: Text('You have pressed the button $_count times.')
  8. ),
  9. backgroundColor: Colors.blueGrey.shade200,
  10. floatingActionButton: FloatingActionButton(
  11. onPressed: () => setState(() => _count++),
  12. tooltip: 'Increment Counter',
  13. child: const Icon(Icons.add),
  14. ),
  15. );
  16. }
  1. Material

  1. Widget build(BuildContext context) {
  2. return Material(
  3. type: MaterialType.transparency,
  4. child: Container()
  5. );
  6. }
  1. 修改Text的style

  1. Text("Text Content",
  2. style: TextStyle(
  3. decoration: TextDecoration.none,
  4. )
  5. );

Flutter Text底部黄色双划线 - 简书


Flutter Text TextOverflow.ellipsis 默认 按单词 切割

问题描述 https://github.com/flutter/flutter/issues/18761

考虑替代方案: https://pub.dev/packages/assorted_layout_widgets


Flutter VS Code 编译器 有选中的代码块的时候无代码提示问题

在设置中搜索 prevent, 然后在 Editor > Suggest: Snippets Prevent Quick Suggestions前面,把√去掉即可

https://blog.csdn.net/qq972618478/article/details/97665271


30 Best Flutter App Templates in 2020
https://flutterawesome.com/30-best-flutter-app-templates-in-2020/


Flutter 绘制虚线

两种方法:

  1. 使用canvas绘图的时候绘制,此时要依赖第三方库来实现:
    https://pub.flutter-io.cn/packages/path_drawing#-readme-tab-

  2. 使用widget层的listView来构建Widget
    https://stackoverflow.com/questions/54019785/how-to-add-line-dash-in-flutter


一个类似iOS JXCategoryView LineView效果的tabbar

https://github.com/flutter-studio/animated_interpolation
路径: /lib/smart_tabbar.dart


Flutter BezierPath 实现类似iOS strokeEnd 动画效果

  1. Path path = Path();
  2. path.moveTo(0, size.height / 2);
  3. //直线测试
  4. // path.lineTo(size.width / 2, size.height / 2);
  5. // path.lineTo(size.width, 0);
  6. //曲线测试, ⚠️⚠️ Flutter使用和 iOS一样的绘图坐标系 ⚠️⚠️
  7. path.addArc(
  8. Rect.fromCenter(
  9. center: Offset(size.width / 2, size.height / 2),
  10. width: size.width,
  11. height: size.height),
  12. 0,
  13. -pi/2);
  14. var pathMetrics = path.computeMetrics(forceClosed: false);
  15. var list = pathMetrics.toList();
  16. var length = progress * list.length.toInt();
  17. Path newPath = new Path();
  18. for (int i = 0; i < length; i++) {
  19. var extractPath = list[i]
  20. .extractPath(0, list[i].length * progress, startWithMoveTo: true);
  21. newPath.addPath(extractPath, Offset(0, 0));
  22. }
  23. canvas.drawPath(newPath, paint);

参考: Flutter篇 (二)绘制贝塞尔曲线 、折线 、柱状图,支持触摸 - 简书

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/787443
推荐阅读
相关标签
  

闽ICP备14008679号