To return a widget (or rather a widget's information, such as its data) from a route using Navigator.pop, you typically return an object that can be used to build or create the desired widget on the previous screen. Flutter does not support directly passing a widget object itself, but you can pass back data (like a model, string, or other types) from your second widget back to the first.
Returning Data to Build a Widget After Navigation
Here's a more detailed example to illustrate how you can return data from a second screen and then use that data to create a widget on the first screen.
- Define your routes:
Here’s how you can achieve this:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: FirstScreen(),
);
}
}
class FirstScreen extends StatefulWidget {
@override
_FirstScreenState createState() => _FirstScreenState();
}
class _FirstScreenState extends State<FirstScreen> {
Widget? returnedWidget;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First Screen'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () async {
// Navigate to SecondScreen and wait for the result
final result = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
// If a widget was returned, update the state
if (result != null) {
setState(() {
returnedWidget = result; // Store the returned widget
});
}
},
child: Text('Go to Second Screen'),
),
SizedBox(height: 20),
// Display the returned widget if it exists
if (returnedWidget != null) ...[
Text('Returned Widget:'),
returnedWidget!, // Use the returned widget here
]
],
),
);
}
}
Second Screen
The second screen can return a widget (or any data type that can be used to build a widget):
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Screen'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Select a widget to return:'),
ElevatedButton(
onPressed: () {
// Create a simple widget to return
final widgetToReturn = Text(
'Returned from Second Screen!',
style: TextStyle(fontSize: 24, color: Colors.blue),
);
// Return the widget to the first screen
Navigator.pop(context, widgetToReturn);
},
child: Text('Return Text Widget'),
),
ElevatedButton(
onPressed: () {
// Create another widget to return
final widgetToReturn = Container(
color: Colors.green,
padding: EdgeInsets.all(16.0),
child: Text(
'Returned a Container!',
style: TextStyle(fontSize: 20),
),
);
// Return the widget to the first screen
Navigator.pop(context, widgetToReturn);
},
child: Text('Return Container Widget'),
),
],
),
),
);
}
}
Explanation
-
Navigation and Returning the Widget:
- From
FirstScreen, you push to SecondScreen and use await to wait for a result.
- The
result can be any widget you wish to return, such as a Text widget or a Container widget.
-
Displaying the Returned Widget:
- After coming back (after
Navigator.pop), the widget is stored in returnedWidget.
- You can then display this widget directly in your
Column on FirstScreen.
Key Points
- You cannot return a widget itself directly from a
Navigator.pop(), but you can return data required to create or configure a widget and then build that widget based on the returned data when you navigate back.
- This pattern can help maintain state and UI flexibility in your Flutter app.