我在页面中有一个按钮。以及名为MySdClass的类。在我的主类中,我有一个构造函数,它从MySdClass接收一些数据。我想让wen用户点击按钮,该页面的构造函数再次从MySdClass接收数据。我脑海中想象的代码不起作用:
import 'package:flutter/material.dart';
import 'package:./SdClass.dart';
void main(){
runApp(MainClass(inputData: MySdClass.data,));
}
class MainClass extends StatelessWidget {
final String data;
const MainClass({Key? key, required this.data}) : super(key: key);
@override
Widget build(BuildContext context) {
return Row(
children: [
Container(
const Text(this.widget.data),
),
ElevatedButton(onPressed: (){//what should be here?}, child: const
Text('next')),
],
);
}
}
void main(){
runApp(MainClass(inputData: MySdClass.data, onPressed:(){
"Perform some action and you can send data again through this contructor"
});
}
class MainClass extends StatelessWidget {
final String data;
void Function() onPressed;
MainClass({Key? key, required this.data,required this.onPressed}) : super(key: key);
@override
Widget build(BuildContext context) {
return Row(
children: [
Container(
const Text(this.widget.data),
),
ElevatedButton(onPressed: onPressed, child: const
Text('next')),
],
);
}
}