| // Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | |
| // for details. All rights reserved. Use of this source code is governed by a | |
| // BSD-style license that can be found in the LICENSE file. | |
| import 'package:flutter/foundation.dart'; | |
| import 'package:flutter/material.dart'; | |
| import 'package:jni/jni.dart'; | |
| import 'package:jni_flutter/jni_flutter.dart'; | |
| // The hierarchy created in generated code will mirror the java package | |
| // structure. | |
| import 'android_utils.g.dart'; | |
| JObject context = androidApplicationContext; | |
| final hashmap = JHashMap<JString, JString>(); | |
| final emojiCompat = EmojiCompat.get(); | |
| extension IntX on int { | |
| JString toJString() { | |
| return toString().toJString(); | |
| } | |
| } | |
| const sunglassEmoji = "😎"; | |
| // snippet-start#show_toast | |
| void showToast() { | |
| final activity = | |
| androidActivity(PlatformDispatcher.instance.engineId!)?.as(Activity.type); | |
| final message = 'This is a native toast shown from a Flutter app via JNI.'; | |
| AndroidUtils.showToast( | |
| activity, | |
| message.toJString().as(CharSequence.type), | |
| 0, | |
| ); | |
| } | |
| // snippet-end#show_toast | |
| /// Display device model number and the number of times this was called | |
| /// as Toast. | |
| void showToastFancy() { | |
| final toastCount = hashmap.getOrDefault( | |
| "toastCount".toJString(), | |
| 0.toJString(), | |
| ); | |
| final newToastCount = (int.parse(toastCount!.toDartString()) + 1).toJString(); | |
| hashmap.put("toastCount".toJString(), newToastCount); | |
| final emoji = | |
| emojiCompat.hasEmojiGlyph(sunglassEmoji.toJString().as(CharSequence.type)) | |
| ? sunglassEmoji | |
| : ':cool:'; | |
| final message = | |
| '${newToastCount.toDartString()} - ${Build.MODEL!.toDartString()} $emoji'; | |
| AndroidUtils.showToast( | |
| androidActivity(PlatformDispatcher.instance.engineId!)?.as(Activity.type), | |
| message.toJString().as(CharSequence.type), | |
| 0, | |
| ); | |
| } | |
| void main() { | |
| EmojiCompat.init(context.as(Context.type)); | |
| runApp(const MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| const MyApp({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'Flutter Demo', | |
| theme: ThemeData(primarySwatch: Colors.teal), | |
| home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
| ); | |
| } | |
| } | |
| class MyHomePage extends StatelessWidget { | |
| const MyHomePage({super.key, required this.title}); | |
| final String title; | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar(title: Text(title)), | |
| body: Center( | |
| child: Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: [ | |
| ElevatedButton( | |
| child: const Text('Show Device Model'), | |
| onPressed: () => showToastFancy(), | |
| ), | |
| ], | |
| ), | |
| ), | |
| ); | |
| } | |
| } |