import'package:flutter/cupertino.dart';import'package:flutter/material.dart';import'dart:async';import'package:flutter/services.dart';import'package:flutter/widgets.dart';import'package:flutter_plugin_demo2/flutter_plugin_demo2.dart';import'package:flutter_plugin_demo2_example/more_Params_page.dart';import'package:flutter_plugin_demo2_example/show_native_page.dart';voidmain(){runApp(constMyApp());}classMyAppextendsStatefulWidget{constMyApp({Key? key}):super(key: key);@overrideState<MyApp>createState()=>_MyAppState();}class _MyAppState extendsState<MyApp>{@overrideWidgetbuild(BuildContext context){returnMaterialApp(
routes:{"/moreParams":(context)=>MoreParamsPage(),},
home:HomePage(),);}}classHomePageextendsStatefulWidget{HomePage({Key? key}):super(key: key);@overrideState<HomePage>createState()=>_HomePageState();}class _HomePageState extendsState<HomePage>{@overrideWidgetbuild(BuildContext context){returnScaffold(
appBar:AppBar(
title:constText('Plugin example app'),),
body:Center(
child:Column(
mainAxisAlignment:MainAxisAlignment.center,
children:[Text('Running on: $_platformVersion\n'),constDivider(),Text(_originalStr +"\n"),InkWell(
onTap:(){print("开始flutter调用原生方法");updateAndroidInfoData();},
child:Container(
padding:constEdgeInsets.all(8),
color:Colors.red,
child:Text("flutter调用原生~~"),),),constDivider(),InkWell(
onTap:()async{print("flutter传值");Navigator.pushNamed(context,"/moreParams");},
child:Container(
padding:constEdgeInsets.all(8),
color:Colors.red,
child:Text("flutter传值~~"),)),constDivider(),InkWell(
onTap:()async{print("flutter显示原生View");Navigator.push(
context,MaterialPageRoute(builder:(context)=>ShowNativePage()),);},
child:Container(
padding:constEdgeInsets.all(8),
color:Colors.red,
child:Text("flutter显示原生View~~"),)),constDivider(),InkWell(
onTap:()async{String str =awaitFlutterPluginDemo2.registerCallBack??"no method";print("success :$str");},
child:Container(
padding:constEdgeInsets.all(8),
color:Colors.red,
child:Text("原生异步定时回调"),)),Text('异步回调:'+fromMobileStr)],),),);}String _platformVersion ='Unknown';String _originalStr ="init data";String fromMobileStr ="fromMobileStr";FlutterPluginDemo2 flutterPluginDemo2 =FlutterPluginDemo2();@overridevoidinitState(){super.initState();initPlatformState();
flutterPluginDemo2.setMethodCallback(flutterMethod);}// Platform messages are asynchronous, so we initialize in an async method.Future<void>initPlatformState()async{String platformVersion;// Platform messages may fail, so we use a try/catch PlatformException.// We also handle the message potentially returning null.try{
platformVersion =awaitFlutterPluginDemo2.platformVersion ??'Unknown platform version';}onPlatformException{
platformVersion ='Failed to get platform version.';}// If the widget was removed from the tree while the asynchronous platform// message was in flight, we want to discard the reply rather than calling// setState to update our non-existent appearance.if(!mounted)return;setState((){
_platformVersion = platformVersion;});}Future<dynamic>flutterMethod(MethodCall methodCall)async{print("methodCall test: $methodCall.method");switch(methodCall.method){case'test':String msg = methodCall.arguments["msg"];print("param:"+msg);setState((){
fromMobileStr = msg;});break;}}Future<void>updateAndroidInfoData()async{String originalStr;try{
originalStr =awaitFlutterPluginDemo2.originalAllData ??"Unknow Not Back";}onPlatformException{
originalStr ="Failed Not Back";}if(!mounted)return;setState((){
_originalStr = originalStr;});}}