Function 如何从另一页调用颤振中的函数

Function 如何从另一页调用颤振中的函数,function,flutter,dart,flutter-dependencies,Function,Flutter,Dart,Flutter Dependencies,我是新来的,我在做一个小的poc项目。我只想调用一个函数,该函数位于我的第一页的第二页,使用abutton click。这就是我所做的 第一页 class Mainpage extends StatefulWidget { @override _MainpageState createState() => _MainpageState(); } class _MainpageState extends State<Mainpage> { @override

我是新来的,我在做一个小的poc项目。我只想调用一个函数,该函数位于我的第一页的第二页,使用abutton click。这就是我所做的

第一页

class Mainpage extends StatefulWidget {

  @override
  _MainpageState createState() => _MainpageState();
}

class _MainpageState extends State<Mainpage> {


 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
         backgroundColor: Colors.white,
        leading: Padding(
          padding: EdgeInsets.only(left: 12),
          child: IconButton(
            icon: Icon(Icons.menu,
              color: Colors.grey[500],
            size: 30,),
            onPressed: () {
              print('Click leading');
            },
          ),
        ),
        title: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children:<Widget>[
              Text('Basic AppBar'),
            ]
        ),
        actions: <Widget>[

          IconButton(
            icon: Icon(Icons.notifications,
            color: Colors.grey[500],
            size: 30,),
            onPressed: () {
              Navigator.pushNamed(context, '/notifications');
            },
          ),
        ],

      ),
      body:
          Container(
            padding: EdgeInsets.fromLTRB(10,10,10,0),

            child: Column(
                children:<Widget>[
       Row(
            children:<Widget>[
      
    ]),
                  SizedBox(height: 60),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                      children:<Widget>[
                        GestureDetector(
                           child: Image.asset('assets/cam.png',
                               height:90),
                          onTap: () {
                           
                            showDialog(
                              context: context,
                              builder: (context) {
                                return Dialog(
                                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
                                  elevation: 16,
                                  child: Container(
                                    height: 180.0,
                                    width: 330.0,
                                    child: ListView(
                                      children: <Widget>[
                                        SizedBox(height: 20),
                                        //Center(
                                           Padding(
                                             padding: const EdgeInsets.only(left:15.0),
                                             child: Text(
                                              "Add a Receipt",

                                              textAlign: TextAlign.left,
                                              style: TextStyle(fontSize: 24, color: Colors.black, fontWeight: FontWeight.bold),
                                          ),
                                           ),
                                       // ),
                                        SizedBox(height: 20),
                                        FlatButton(
                                          child: Text(
                                            'Take a photo..',
                                            textAlign: TextAlign.left,
                                            style: TextStyle(fontSize: 20),
                                          ),

                                          onPressed: () {
                                            
                                            });
                                           
class主页扩展StatefulWidget{
@凌驾
_MainpageState createState()=>\u MainpageState();
}
类_MainpageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
背景颜色:Colors.white,
前导:填充(
填充:仅限边缘设置(左:12),
孩子:我的钮扣(
图标:图标(Icons.menu,
颜色:颜色。灰色[500],
尺码:30,),,
已按下:(){
打印('点击前导');
},
),
),
标题:世界其他地区(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
文本(“基本应用程序栏”),
]
),
行动:[
图标按钮(
图标:图标(Icons.notifications,
颜色:颜色。灰色[500],
尺码:30,),,
已按下:(){
pushNamed(上下文“/notifications”);
},
),
],
),
正文:
容器(
填充:来自LTRB(10,10,10,0)的边缘设置,
子:列(
儿童:[
划船(
儿童:[
]),
尺寸箱(高度:60),
划船(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
手势检测器(
子级:Image.asset('assets/cam.png',
身高:90),,
onTap:(){
显示对话框(
上下文:上下文,
生成器:(上下文){
返回对话框(
形状:RoundedRectangleBorder(borderRadius:borderRadius.circular(10)),
海拔:16,
子:容器(
高度:180.0,
宽度:330.0,
子:ListView(
儿童:[
尺寸箱(高度:20),
//居中(
填充物(
填充:仅限常量边集(左:15.0),
子:文本(
“添加收据”,
textAlign:textAlign.left,
样式:TextStyle(fontSize:24,颜色:Colors.black,fontWeight:fontWeight.bold),
),
),
// ),
尺寸箱(高度:20),
扁平按钮(
子:文本(
“拍照……”,
textAlign:textAlign.left,
样式:TextStyle(字体大小:20),
),
已按下:(){
});
我不知道您是否想在上述代码的末尾给出onpressed函数

第二页如下


class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();

}

class _MyHomePageState extends State<MyHomePage> {


  File _selectedFile;
  bool _inProcess = false;
  Map data = {};




  Widget getImageWidget() {
    if (_selectedFile != null) {
      return Image.file(
        _selectedFile,
        width: 350,
        height: 650,
        fit: BoxFit.cover,
      );
    } else {
      return Image.asset(
        "assets/splashlogo.png",
        width: 350,
        height: 650,
        fit: BoxFit.cover,
      );
    }
  }

  getImage(ImageSource source) async {
    this.setState((){
      _inProcess = true;
    });
    File image = await ImagePicker.pickImage(source: source);
    if(image != null){
      File cropped = await ImageCropper.cropImage(
          sourcePath: image.path,
          maxWidth: 1080,
          maxHeight: 1080,

          compressFormat: ImageCompressFormat.jpg,
          androidUiSettings: AndroidUiSettings(
              toolbarColor: Colors.black,
              toolbarWidgetColor: Colors.white,
              //toolbarTitle: "RPS Cropper",
              statusBarColor: Colors.deepOrange.shade900,
              backgroundColor: Colors.black,
              initAspectRatio: CropAspectRatioPreset.original,
              lockAspectRatio: false
          ),
          iosUiSettings: IOSUiSettings(
            minimumAspectRatio: 1.0,
          )
      );

      this.setState((){
        _selectedFile = cropped;
        _inProcess = false;
      });
    } else {
      this.setState((){
        _inProcess = false;
      });
    }
  
  }


类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
文件\u选择的文件;
bool\u inProcess=false;
地图数据={};
Widget getImageWidget(){
如果(_selectedFile!=null){
返回Image.file(
_选择的文件,
宽度:350,
身高:650,
适合:BoxFit.cover,
);
}否则{
返回Image.asset(
“assets/splashlogo.png”,
宽度:350,
身高:650,
适合:BoxFit.cover,
);
}
}
getImage(ImageSource源)异步{
此.setState(){
_inProcess=true;
});
File image=wait ImagePicker.pickImage(源:source);
如果(图像!=null){
文件裁剪=等待ImageCropper.cropImage(
sourcePath:image.path,
最大宽度:1080,
最大高度:1080,
压缩格式:ImageCompressFormat.jpg,
androidUiSettings:androidUiSettings(
工具栏颜色:Colors.black,
工具栏WidgetColor:Colors.white,
//工具栏标题:“RPS裁剪器”,
statusBarColor:Colors.deepOrange.shade900,
背景颜色:Colors.black,
initAspectRatio:cropasspectratiopreset.original,
lockAspectRatio:错误
),
iosUiSettings:iosUiSettings(
最小频谱:1.0,
)
);
此.setState(){
_selectedFile=已裁剪;
_inProcess=false;
});
}否则{
此.setState(){
_inProcess=false;
});
}
}
我需要调用getImage(ImageSource.camera);在第一页的my onpress函数中,该函数指向第二页的getImage函数

有人能帮我吗?

用一把万能钥匙

GlobalKey<_MyHomePageState> globalImageKey = GlobalKey();
使用
MyHomePage
时:

MyHomePage(key: globalImageKey)
电话:


在这里添加这个在你的第一页在按下

                                            Navigator.pushReplacementNamed(context,'/2ndpage',arguments: {
                                              'pickerCode': "0",
                                            });
在第二页,你是这样做的

@override
  void initState() {
    super.initState();

    Future.delayed(Duration.zero, () {
      data = ModalRoute.of(context).settings.arguments;
      print(data);
      if(data['pickerCode']=="0"){
        getImage(ImageSource.camera);
      }
      
    });
  }
这是一件棘手的事情,但我想它会对你有所帮助。

你能告诉我它在哪里吗
                                            Navigator.pushReplacementNamed(context,'/2ndpage',arguments: {
                                              'pickerCode': "0",
                                            });
@override
  void initState() {
    super.initState();

    Future.delayed(Duration.zero, () {
      data = ModalRoute.of(context).settings.arguments;
      print(data);
      if(data['pickerCode']=="0"){
        getImage(ImageSource.camera);
      }
      
    });
  }