解决flutter作为模块接入现有APP后白屏软模式问题处理

flutter项目运行到模拟器碰到应用白屏问题:
ERROR:flutter/shell/gpu/gpu_surface_gl.cc(85)] Failed to setup Skia Gr

通常方案为:flutter run --enable-software-rendering

flutter作为模块接入现有APP后,似乎Android studio没办法设置了。

修改办法,主activity 的intent中加入

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Flutter.startInitialization(getApplicationContext());
        // use software rendering (ideally only when you need to)
        getIntent().putExtra("enable-software-rendering", true);

        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
        tx.replace(R.id.content_flutter, Flutter.createFragment("route1"));
        tx.commit();
    }
}

Flutter类创建view时修改:

public static FlutterView createView(@NonNull final Activity activity, @NonNull final Lifecycle lifecycle, final String initialRoute) {
    FlutterMain.startInitialization(activity.getApplicationContext());
//    FlutterMain.ensureInitializationComplete(activity.getApplicationContext(), null);

    /**
     * 1添加参数软模式参数解决白屏问题
     */
    String[] args = getArgsFromIntent(activity.getIntent());
    FlutterMain.ensureInitializationComplete(activity.getApplicationContext(), args);
    ////////////////





private static String[] getArgsFromIntent(Intent intent) {
    ArrayList args = new ArrayList();
    if (intent.getBooleanExtra("trace-startup", false)) {
      args.add("--trace-startup");
    }

    if (intent.getBooleanExtra("start-paused", false)) {
      args.add("--start-paused");
    }

    if (intent.getBooleanExtra("use-test-fonts", false)) {
      args.add("--use-test-fonts");
    }

    if (intent.getBooleanExtra("enable-dart-profiling", false)) {
      args.add("--enable-dart-profiling");
    }

    if (intent.getBooleanExtra("enable-software-rendering", false)) {
      args.add("--enable-software-rendering");
    }

    if (intent.getBooleanExtra("skia-deterministic-rendering", false)) {
      args.add("--skia-deterministic-rendering");
    }

    if (intent.getBooleanExtra("trace-skia", false)) {
      args.add("--trace-skia");
    }

    if (intent.getBooleanExtra("trace-systrace", false)) {
      args.add("--trace-systrace");
    }

    if (intent.getBooleanExtra("verbose-logging", false)) {
      args.add("--verbose-logging");
    }

    if (!args.isEmpty()) {
      String[] argsArray = new String[args.size()];
      return (String[])args.toArray(argsArray);
    } else {
      return null;
    }
  }

主要是这个方法添加的参数:

FlutterMain.ensureInitializationComplete(activity.getApplicationContext(), args);

 

这些参考类:

FlutterActivityDelegate.java

对股票走势分析感兴趣可以添加公.众.号(欢喜缠): huanxichan2020

你可能感兴趣的:(flutter)