在将参数传递给screen类之后,我无法使用navigator push将数据从当前屏幕获取到新屏幕



我已经将参数传递给ontap事件中的按钮,现在我想用当前按下的按钮数据导航到新屏幕

onTap: ()  {
//toast message showing he selected song title
// print("${context} You Selected:   " + item.data![index].title);
Navigator.push(context,
MaterialPageRoute(builder: (context) =>  SongScreen(title: item.data![index].title)),);
},

现在我想把数据传给有状态的小部件,但我遇到了错误,我想把这些数据带到文本字段

class SongScreen extends StatefulWidget {
const SongScreen({super.key, required String title});
@override
State<SongScreen> createState() => _SongScreenState();
}
class _SongScreenState extends State<SongScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
// Below is the code for Linear Gradient.
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [Colors.purple, Colors.blue],
begin: Alignment.bottomLeft,
end: Alignment.topRight,
),
),
child: 
Center(
child: Text('data'),
),
),
);
}
}
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => abc(title: "Hello"),
),
);
},
child: Text("Push me"),
),
class abc extends StatefulWidget {
abc({Key? key, required this.title}) : super(key: key);
String title;
@override
State<abc> createState() => _abcState();
}
class _abcState extends State<abc> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(widget.title),
),
);
}
}

最新更新