android项目嵌入flutter且解决的问题

1:在android原生根目录下通过运行命令创建flutter module,这种创建与 flutter new project 内部目录结构不一样,所以直接用命令去创建,

2:new Binding 如果标识红色报错,那就看看flutter是否导入,或者目录结构是否正确, evaluate方法.groovy的文件路径可以直接拼接具体路径('XXX/my_flutter/.android/include_flutter.groovy'

)

3:minSdkVersion 最低版本为16

4:JDK 为1.8

5:如果Executing tasks: [:app:assembleDebug]错误,命令行 ./gradlew assembleDebug

6:如果android 调用flutter 页面事通过创建Fragment替换来时先得,会有黑屏效果打包为release就解决了,默认调用main.dart时AppBar 是没有返回键的,所以需要自己手动添加IconButton,但appBar返回也黑屏,是因为当前是栈内最后一个页面,或者主界面,所以通过Navigator.pop(context)退出为栈内退出,SystemNavigator.pop();这种是主界面退出,所以当android 嵌入调用flutter第一个界面main.dart时返回就需要用SystemNavigator.pop();

 

创建Flutter module

在当前project下 运行命令 

flutter create -t module my_flutter(my_flutter为生成的flutter module名称)

增加配置代码

(1) 在工程的settings.gradle增加以下配置 

(2)在app (假如你的Android应用名称为app)的build.gradle文件下dependencies增加如下依赖

其中flutter工程为创建Flutter module过程自动生成的,注意就是flutter,b

注意 其中 minSdkVersion 需要至少为16,否则会报错

生成的工程结构如下:

工程目录结构

在Android工程中创建Flutter的UI(调用Flutter中的dart编写的widget)

 Flutter提供两种方法引入,一种是View,一种是Fragment,View如下

其中,第二个参数是Lifecycle对象, 第三个参数为route,这个参数Flutter端可以通过window.defaultRouteName获取,利用它flutter可知道要创建哪个widget.

同理 Flutter.createFragment(String route)可生成FlutterFragment

顺便说明下在运行后会发现有白屏的情况,是因为在debug模式下运行的原因,无需理会,最终发布release模式下会很快的

Flutter module支持hot reload (热重载)

进入到flutter module目录下

执行命令 flutter attach 即可

如上就说明连接成功,只要有改动,键盘敲击R或者r 即可看到更改

如果你运行命令后一直在waiting,可尝试杀死该应用进程后再重启,进入相关Flutter页面就可连接上了

 

flutter main.dart 文件代码 导入ui 调用 window.defaultRouteName获得传入的值,可以通过传值判断跳转那个页面

 

android类:
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_browse_fragment, Flutter.createFragment("route1"));
fragmentTransaction.commit();
import 'dart:ui';
import 'package:flutter/material.dart';

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

Widget widgetForRoute(String route) {
  switch (route) {
    case 'route1':
      return new MyApp();
    case 'route2':
      return new MyApp();
    default:
      return Center(
        child: Text('Unknown    route1 : $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(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or press Run > Flutter Hot Reload in a Flutter IDE). Notice that the
        // counter didn't reset back to zero; the application is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            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),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值