Flutter Android Toast Message(flutterアクセスAndroid Toast Message)
6351 ワード
しかし、これらのToastメッセージは、Flutterに関連する場合に直接表示されない.そのため、それを実現するための代替方法を見つける必要があります.この場合、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 {
/// 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: [
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プラグインパッケージの作成
注:プラットフォーム・メッセージは非同期であり、エラーが発生する可能性があります.
$ mkdir test_lib && cd test_lib
$ flutter create -t plugin --org com.ajanuw ./
$ code .
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 get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
static Future hello(String value) {
return _channel.invokeMethod('hello', {"message": value});
}
}
android\src\main\kotlin\com\example\test_lib\TestLibPlugin.kt
を記述する.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 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
},
),
),
);
}
}