Flutter Android Toast Message(flutter访问Android Toast Message)

Android Toast通知可用于向用户发送快速消息,并在几秒钟后消失。

但是当涉及Flutter时,没有直接的方式来显示这些Toast消息。因此,我们需要找到一种替代方法来实现它。在这种情况下,platform将是您的朋友。

lib/main.dart

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  /// 创建一个MethodChannel类的对象
  static const platform = const MethodChannel("toast.flutter.io/toast");
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'This is Toast Tutorial',
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _showToast,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }

  void _showToast() {
    /// 将唯一的通道名称传递给构造函数。此通道名称用于唯一标识每个通道。
    platform.invokeMethod("showToast", {"message": "xxxxx"});
  }
}

android/app/src/main/java/com/example/flutter_toast/MainActivity.java

package com.example.flutter_toast;

import android.os.Bundle;
import android.widget.Toast;

import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

import java.util.Map;

public class MainActivity extends FlutterActivity {
  // 创建一个变量来分配通道名称, 在这里,我们需要使用我们在flutter代码中指定的相同通道名称。
  private static final String CHANNEL = "toast.flutter.io/toast";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
    
    // 需要创建一个新的MethodChannel对象
    // 作为构造函数的第二个参数,我们需要传递通道名称
    // 当我们从flutter中调用invokeMethod时,方法调用处理程序将调用
    new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
    
      // 在onMethodCall里面,我们需要检查哪个方法。因为我们可以通过单一平台渠道调用多种方法
      @Override
      public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
        if (methodCall.method.equals("showToast")) {
          // 检查参数
          if (!(methodCall.arguments instanceof Map)) {
            throw new IllegalArgumentException("Map argument expected");
          }
          System.out.println(methodCall.arguments);
          Toast.makeText(getApplicationContext(), (String) methodCall.argument("message"), Toast.LENGTH_SHORT).show();
        } else {
          result.notImplemented();
        }
      }
    });
  }
}

Flutter 1.14.2 创建插件包

注意:平台消息是异步的,还有可能会出错

  1. 创建插件包
$ mkdir test_lib && cd test_lib
$ flutter create -t plugin --org com.ajanuw ./
$ code .
  1. 编写lib\test_lib.dart,也就是别人使用你的api
import 'dart:async';

import 'package:flutter/services.dart';

class TestLib {
  static const MethodChannel _channel =
      const MethodChannel('github.com/januwA/test_lib');

  static Future<String> get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  static Future<String> hello(String value) {
    return _channel.invokeMethod('hello', {"message": value});
  }
}
  1. 编写android\src\main\kotlin\com\example\test_lib\TestLibPlugin.kt,在这里处理flutter发来的消息
package com.example.test_lib

import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar

// 这里需要和test_lib.dart里面注册的一样
const val CHANNEL: String = "github.com/januwA/test_lib"

/** TestLibPlugin */
public class TestLibPlugin: FlutterPlugin, MethodCallHandler {

  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), CHANNEL).setMethodCallHandler(TestLibPlugin());
  }

  companion object {
    @JvmStatic
    fun registerWith(registrar: Registrar) {
      MethodChannel(registrar.messenger(), CHANNEL).setMethodCallHandler(TestLibPlugin())
    }
  }

  override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
    when(call.method) {
      "getPlatformVersion" -> result.success("Android ${android.os.Build.VERSION.RELEASE}")
      "hello" -> result.success("hello " + call.argument("message"))
      else -> result.notImplemented()
    }
  }

  override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
  }
}

example

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: FutureBuilder(
            future: TestLib.platformVersion,
            builder: (context, AsyncSnapshot<String> snap) {
              if (snap.connectionState == ConnectionState.done) {
                return Text(snap.data);
              }
              return SizedBox();
            },
          ),
        ),
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.ac_unit),
          onPressed: () {
            TestLib.hello("Ajanuw").then(print); // hello Ajanuw
          },
        ),
      ),
    );
  }
}
posted @ 2019-08-19 13:15  Ajanuw  阅读(...)  评论(...编辑  收藏