Flutter学习笔记

Dart

https://book.flutterchina.club/chapter1/dart.html

Flutter页面切换动画设为iOS风格,且支边缘持滑动返回

MaterialApp(theme: ThemeData(platform: TargetPlatform.iOS)

发现了其他解决方案:

不改变theme,在跳转页面时,调用:

                Navigator.push( context,
                    CupertinoPageRoute(builder: (context) {
                      return NewRoute();
                    }));

而不是:

                Navigator.push( context,
                    MaterialPageRoute(builder: (context) {
                      return NewRoute();
                    }));

这样在Android设备上,页面跳转动画也和iOS一样,且可以滑动返回。

如果设置跳转为MaterialPageRoute,在Android设备上是Material风格,但是在iOS上依然是iOS风格。

关于类似Android中style.xml的写法问题

Android中可以再style.xml中定义某种常用控件的样式,简化代码,也便于修改。

但是查找了很多资料,好像Flutter中暂时没有此类机制。

Text可以使用ThemeData中的textTheme类似地实现,其他控件暂时没发现解决办法。

textTheme规定了(好像是)13种text的样式。

https://flutter.dev/docs/cookbook/design/themes

https://api.flutter.dev/flutter/material/TextTheme-class.html

https://medium.com/flutter-community/flutter-apply-style-as-a-theme-in-a-text-widget-90268328bd23

merge也挺有用,使用方法:

Theme.of(context)
.textTheme
.title
.merge(TextStyle(color: Colors.red)

P.S.不必修改textTheme。可以创建一个类,单独存储TextStyle,Text直接在此引用。

Android原生交互相关问题

在Android目录下打开新的Android Studio编辑(例如打开build.gradle,点击Open for Editing in Android Studio)

https://stackoverflow.com/questions/51544535/how-to-import-third-party-android-library-to-java-file-in-flutter

国际化

感觉非常麻烦,参考:

https://book.flutterchina.club/chapter13/intl.html

https://www.jianshu.com/p/6ca24dd50c57

http://w4mxl.github.io/2019/01/26/flutter-Intl-Localizations/

添加依赖:

dependencies:
  flutter_localizations:
    sdk: flutter

生成完成后,使用:

    return MaterialApp(
      localizationsDelegates: [
        LocalizationStringDelegate() // 设置Delegate
      ],
      supportedLocales: [
        const Locale('en', ''), // 英语
        const Locale('zh', 'CN'), // 中文简体
        //其它Locales
      ],
// ...


LocalizationString.of(context).home

P.S.国际化可以使用Flutter i18n插件,非常方便

https://www.jianshu.com/p/b9f830efe1f8

依赖不变,依然是flutter_localizations。插件安装后重启Android Studio,工程会自动增加了lib/generated/,res/values/。直接编辑arb文件即可,自动生成其他dart文件。创建arb时选择语言、地区。

      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        S.delegate,
      ],
      supportedLocales: S.delegate.supportedLocales,

S.of(context).home

关于和Android异步对比

https://www.jianshu.com/p/8c422130e750

还需细细研读。

Dart AES CBC PKCS7Padding

这两天一直研究加密的事了,我觉得应该单开一篇文章。说开就开。

TextEditingController销毁问题

页面退出时,TextEditingController应该销毁。

Remember to dispose of the TextEditingController when it is no longer needed. This will ensure we discard any resources used by the object.

https://api.flutter.dev/flutter/widgets/TextEditingController-class.html

https://flutter.dev/docs/cookbook/forms/text-field-changes

  @override
  void dispose() {
    // Clean up the controller when the widget is removed from the
    // widget tree.
    myController.dispose();
    super.dispose();
  }

Tab切换不重新加载页面

https://www.jianshu.com/p/3773a882ab0c

ClipRRect

可以使用ClipRRect做出圆角Image

Hero

Hero可以在切换页面时,将某控件飞到下个页面

FittedBox

FittedBox可以调整子控件拉抻、平铺、裁剪等等

2 Comments on “Flutter学习笔记

发表评论

电子邮件地址不会被公开。 必填项已用*标注