Flutter-学习记录

333 阅读1分钟

flutter启动页显示修改-android

  1. 修改style文件
  • 把AndroidManifest MainActivity中配置的style文件打开,在LaunchTheme的parent中增加.Fullscreen具体操作如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Theme applied to the Android Window while the process is starting -->
    <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar.Fullscreen">
        <!-- Show a splash screen on the activity. Automatically removed when
             Flutter draws its first frame -->
        <item name="android:windowBackground">@drawable/launch_background</item>
    </style>
    <!-- Theme applied to the Android Window as soon as the process has started.
         This theme determines the color of the Android Window while your
         Flutter UI initializes, as well as behind your Flutter UI while its
         running.
         
         This Theme is only used starting with V2 of Flutter's Android embedding. -->
    <style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar.Fullscreen">
        <item name="android:windowBackground">@android:color/white</item>
    </style>
</resources>
  1. 修改launch_background
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">

    <item android:drawable="@drawable/launch_gray" />

    <!--中部-->
    <item
        android:bottom="298dp"
        android:left="63dp"
        android:right="63dp"
        android:top="194dp">
        <bitmap
            android:gravity="center"
            android:src="@mipmap/launch_pager_icon" />
    </item>

    <!--底部-->
    <item android:bottom="35.5dp">
        <bitmap
            android:gravity="center|bottom"
            android:src="@mipmap/launch_pager_btm"
            android:tileMode="disabled" />
    </item>

</layer-list>

  1. 在colors文件中增加
<drawable name="launch_gray">#AABBCC</drawable>
  1. 增加图片资源

image.png

  1. 效果

Screenshot_20210324_154018.jpg

flutter常用组件

TextField 输入文本垂直居中显示

TextField 默认下方带有padding,所以在InputDecoration中加入参数设置padding 为0 即可

contentPadding: EdgeInsets.all(0)

AppBarLeading leading组件中子元素过宽时 overflow溢出问题解决

leading元素默认宽度时56,假如你的childwidget宽度超过56时,则会报溢出的问题,但是官方提供给我们手动改leading宽度的方式。 默认及固定leading的源码:

1.leadingWidth为空则走默认
if (leading != null) {
      leading = ConstrainedBox(
        constraints: BoxConstraints.tightFor(width: widget.leadingWidth ?? _kLeadingWidth),
        child: leading,
      );
}

2.默认等于kToolbarHeight
const double _kLeadingWidth = kToolbarHeight; // So the leading button is square.

3.kToolbarHeight源码中固定设置为56.0
/// The height of the toolbar component of the [AppBar].
const double kToolbarHeight = 56.0;

自定义设置leading宽度,在AppBar属性中增加:

leadingWidth: xxx,//xxx填写您的实际宽度

禁止PageView滚动

在PageView属性中增加如下代码:

physics: NeverScrollableScrollPhysics(),

list调用map时同时访问index,和value

list.asMap().entries.map((entry) {
    int index = entry.key;
    String value = entry.value;
    return something;
}

Flutter忽略空安全生成Pigeon

在run 命令后面添加 --no-sound-safety即可

flutter pub run --no-sound-null-safety pigeon \
  --input pigeons/message.dart \
  --dart_out lib/pigeons/message.dart \
  --objc_header_out ios/Classes/messages.h \
  --objc_source_out ios/Classes/messages.m \
  --objc_prefix FLT \
  --java_out android/app/src/main/kotlin/com/zhou/mineplayer/Messages.java \
  --java_package "com.zhou.mineplayer"