Flutter混合开发入门

前言

老项目直接迁移到Flutter成本比较高,且风险也比较高,最好的方法就是混合开发,慢慢迁移过去

实现目标

从原生的页面跳转到Flutter页面

实现代码

通过命令行,进入原生项目的根目录,然后创建一个Flutter项目

$ cd some/path/
$ flutter create -t module --org com.example my_flutter

 在原生项目的app module的build.gradle中添加如下配置,支持java8

android {
  //...
  compileOptions {
    sourceCompatibility 1.8
    targetCompatibility 1.8
  }
}

修改原生项目的settings.gradle文件,配置对flutter项目的依赖

要注意官方教程中flutter路径写成'/my_flutter/.android/include_flutter.groovy' ,是无法正常引用的,会报找不到文件错误

include ':app'
setBinding(new Binding([gradle: this]))                           
evaluate(new File(                                                
  settingsDir.parentFile,                                              
  '/NativeProjectName/my_flutter/.android/include_flutter.groovy'                     
))                                                                  

在原生项目app module的build.gradle文件中增加对flutter的依赖

dependencies {
  implementation project(':flutter')
}

新建一个FlutterActivity,该页面显示的是一个FlutterView

通过Flutter.createView来创建FlutterView,其中第三个参数为路由名用于区分获取不同的FlutterView,在Flutter项目中会通过不同的路由名来创建不同的FlutterView

public class FlutterActivity extends AppCompatActivity {

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

        FlutterView flutterView = Flutter.createView(FlutterActivity.this, getLifecycle(), "route1");
        setContentView(flutterView);
    }
}


    

修改flutter项目lib文件夹下的main.dart文件,通过RouteName返回不同的FlutterView

核心代码如下

void main() => runApp(_widgetForRoute(window.defaultRouteName));

Widget _widgetForRoute(String route) {
  switch (route) {
    case 'route1':
      return MyApp();
    default:
      return Center(
        child: Text('Unknown route: $route', textDirection: TextDirection.ltr),
      );
  }
}

main.dart完整代码如下

import 'dart:ui';
import 'package:flutter/material.dart';

void main() => runApp(_widgetForRoute(window.defaultRouteName));

Widget _widgetForRoute(String route) {
  switch (route) {
    case 'route1':
      return MyApp();
    default:
      return Center(
        child: Text('Unknown route: $route', textDirection: TextDirection.ltr),
      );
  }
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), //
    );
  }
}

参考

Add Flutter to existing apps

 

你可能感兴趣的:(Flutter)