Forked from henriquepinheiro84/gist:9069c6fee47842370d51345edf27e55b
Last active
June 29, 2019 01:35
-
-
Save gladson/c8f508331567d8578bdf4055a74376da to your computer and use it in GitHub Desktop.
Estou fazendo um modulo flutter em um app android no entanto não estou conseguindo enviar dados para o módulo. Alguém já passou por isso e conseguiu resolver?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:app_pratico_flutter_module/pages/nova_pagina.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
import 'package:toast/toast.dart'; | |
void main() { | |
// runApp( | |
// choseWidget(window.defaultRouteName), | |
// ); | |
runApp(MaterialApp( | |
title: "Teste", | |
home: HomePage(), | |
)); | |
} | |
Widget choseWidget(String route) { | |
switch (route) { | |
case "r1": | |
return MyfluterView(); | |
default: | |
return Center( | |
child: Text("Unown route"), | |
); | |
} | |
} | |
class MyfluterView extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
static const plataform = const MethodChannel('demo.app.flutter.mensagem'); | |
String _message = "Sem mensagem ainda"; | |
@override | |
void initState() { | |
_getMessage().then((String message){ | |
Toast.show(message ?? "mensagem nula", context, duration: Toast.LENGTH_LONG); | |
setState(() { | |
_message = message; | |
Toast.show(message ??"teste", context); | |
}); | |
}); | |
} | |
Future<String> _getMessage() async { | |
String value; | |
try { | |
value = await plataform.invokeMethod('getMessage'); | |
Toast.show(value, context); | |
} catch(e) { | |
print(e); | |
Toast.show(e.toString(), context); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Card( | |
color: Colors.green, | |
shape: RoundedRectangleBorder(), | |
child: Center( | |
child: FlatButton( | |
onPressed: () { | |
Navigator.push(context, | |
MaterialPageRoute(builder: (context) => MyHomePage())); | |
}, | |
child: Text(_message ?? "Não recebeu mensagem")), | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
backgroundColor: Colors.white, | |
onPressed: () { | |
_getMessage().then((String message){ | |
Toast.show(message ?? "mensagem nula", context, duration: Toast.LENGTH_LONG); | |
setState(() { | |
_message = message; | |
Toast.show(message ??"teste", context); | |
}); | |
}); | |
// Navigator.push( | |
// context, MaterialPageRoute(builder: (context) => MyHomePage())); | |
}, | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} | |
import android.content.Context; | |
import android.content.ContextWrapper; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.os.BatteryManager; | |
import android.os.Build.VERSION; | |
import android.os.Build.VERSION_CODES; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.view.View; | |
import android.widget.FrameLayout; | |
import android.widget.Toast; | |
import io.flutter.app.FlutterActivity; | |
import io.flutter.plugin.common.MethodCall; | |
import io.flutter.plugin.common.MethodChannel; | |
import io.flutter.view.FlutterView; | |
import quadritech.qclass.R; | |
public class FlutterTestActivity extends FlutterActivity { | |
private static final String CHANNEL = "demo.app.flutter.mensagem"; | |
private View flutterView; | |
private FrameLayout.LayoutParams frameLayout; | |
private String sharedText; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_flutter_test); | |
findViewById(R.id.btn_abre_flutter).setOnClickListener(view -> abreFlutter()); | |
// this.flutterView = Flutter.createView(FlutterTestActivity, this,"r1"); | |
// | |
// this.frameLayout = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); | |
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() { | |
@Override | |
public void onMethodCall(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) { | |
if (methodCall.method.equals("getMessage")) { | |
String mensagem = "Olá vindo do android"; | |
result.success(mensagem); | |
Toast.makeText(FlutterTestActivity.this, mensagem, Toast.LENGTH_LONG).show(); | |
} | |
} | |
}); | |
} | |
private void abreFlutter() { | |
startActivity(new Intent(FlutterTestActivity.this, NovaActivity.class)); | |
Toast.makeText(this, "Testando botão", Toast.LENGTH_LONG).show(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
class FlutterToast { | |
static const MethodChannel _channel = const MethodChannel("my/toast"); | |
static Future<bool> toast(String content) async { | |
var args = {"content": content}; | |
return await _channel.invokeMethod("toast", args); | |
} | |
} | |
void main() { | |
runApp(MaterialApp( | |
title: "toast", | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text("ShowToast"), | |
), | |
body: Center( | |
child: RaisedButton( | |
onPressed: () async { | |
FlutterToast.toast("clicked"); | |
}, | |
child: Text("test show toast"), | |
), | |
), | |
), | |
)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.widget.Toast; | |
import io.flutter.plugin.common.MethodCall; | |
import io.flutter.plugin.common.MethodChannel; | |
import io.flutter.view.FlutterView; | |
public class FlutterToastPlugin implements MethodChannel.MethodCallHandler { | |
private final Context context; | |
public FlutterToastPlugin(Context context){ | |
this.context = context; | |
} | |
public static void regiester(FlutterView registrar,Context context){ | |
final MethodChannel channel = new MethodChannel(registrar,"my/toast"); | |
channel.setMethodCallHandler(new FlutterToastPlugin(context)); | |
} | |
@Override | |
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) { | |
if (methodCall.method.equals("toast")){ | |
String content = methodCall.argument("content"); | |
Toast.makeText(context,content,Toast.LENGTH_SHORT).show(); | |
result.success(true); | |
}else{ | |
result.notImplemented(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment