Reputation: 399
I need to pass whatever user type in TextField and then use it in another stateful widget. How can I pass data in myController of TextField. I am using this to search API based on user input and need to pass this data at time of navigating to another screen
TextField(
controller: myController,
decoration: InputDecoration.collapsed(
hintText: ' Search by Product Name',),
onSubmitted: (value) { Navigator.push(context, MaterialPageRoute(builder: (context)=> ProductBySearch()));},),
class ProductBySearch extends StatefulWidget {
@override
_ProductBySearchState createState() => _ProductBySearchState();
}
class _ProductBySearchState extends State<ProductBySearch> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder<List<Product>>(
future: getProductByBanner1(http.Client(), mycontroller),
builder: (context, snapshot) {
if (snapshot.hasError) print(snapshot.error);
if (snapshot.hasData) {
return ProductByBanner1Grid(
product: snapshot.data,
);
} else {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
CircularProgressIndicator(),
Container(
height: 15,
width: double.infinity,
),
Text('Factory2Homes'),
],
));
}
},
),
);
}
}
Upvotes: 0
Views: 755
Reputation: 7052
You may pass the data to the constructor of the Widget
built in the MaterialPageRoute
builder. This data being the myController.text
itself.
class ProductBySearch extends StatefulWidget {
const ProductBySearch({Key key, @required this.productName}) : super(key: key);
final String productName;
// (...)
}
And then
MaterialPageRoute(builder: (context) => ProductBySearch(
productName: myController.text,
))
I invite you to read Send data to a new screen Flutter Cookbook tutorial. It should explain you the concept fairly well.
Upvotes: 1