我是新手,我正在从事一个小型poc项目。我想要的是使用一个按钮单击来调用第一页第二页中的函数。这是我所做的,
第一页
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: () {
});
我不希望在上述代码的结尾处给出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;
});
}
}
我需要在第一页的onpress函数中调用 getImage(ImageSource.camera); ,该函数指向第二页的getimage函数。
有人可以帮我吗?。
答案 0 :(得分:0)
使用GlobalKey。
GlobalKey<_MyHomePageState> globalImageKey = GlobalKey();
更改此:
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}): super(key:key)
@override
_MyHomePageState createState() => _MyHomePageState();
}
使用MyHomePage
时:
MyHomePage(key: globalImageKey)
致电:
globalImageKey.currentState.getImage(ImageSource.camera);
答案 1 :(得分:0)
在此处将其添加到您的第一页中
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);
}
});
}
这是一件棘手的事情,但我认为它将为您提供帮助。