Reputation: 3
I'm new to flutter and I want to passe some data between screens.
I know a simular question was ask here but I try that solution but for my code did not work.
I try: "Passing data between screens in Flutter"
Search bar code:
import 'package:flutter/material.dart';
import 'Screen_4.dart';
class SearchList extends StatefulWidget {
SearchList({Key key}) : super(key: key);
@override
_SearchListState createState() => _SearchListState();
}
class _SearchListState extends State<SearchList> {
Widget appBarTitle = Text(
"Search ",
style: TextStyle(color: Colors.white),
);
Icon actionIcon = Icon(
Icons.search,
color: Colors.white,
);
final key = GlobalKey<ScaffoldState>();
final TextEditingController _searchQuery = TextEditingController();
List<String> _list;
bool _IsSearching;
String _searchText = "";
_SearchListState() {
_searchQuery.addListener(() {
if (_searchQuery.text.isEmpty) {
setState(() {
_IsSearching = false;
_searchText = "";
});
} else {
setState(() {
_IsSearching = true;
_searchText = _searchQuery.text;
});
}
});
}
@override
void initState() {
super.initState();
_IsSearching = false;
init();
}
void init() {
_list = List();
_list.add("Google");
_list.add("IOS");
_list.add("Andorid");
_list.add("Dart");
_list.add("Flutter");
_list.add("Python");
_list.add("React");
_list.add("Xamarin");
_list.add("Kotlin");
_list.add("Java");
_list.add("RxAndroid");
_list.add('Lenovo');
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: key,
appBar: buildBar(context),
body: ListView(
padding: EdgeInsets.symmetric(vertical: 8.0),
children: _IsSearching ? _buildSearchList() : _buildList(),
),
);
}
List<ChildItem> _buildList() {
return _list.map((contact) => ChildItem(contact)).toList();
}
List<ChildItem> _buildSearchList() {
if (_searchText.isEmpty) {
return _list.map((contact) => ChildItem(contact)).toList();
} else {
List<String> _searchList = List();
for (int i = 0; i < _list.length; i++) {
String name = _list.elementAt(i);
if (name.toLowerCase().contains(_searchText.toLowerCase())) {
_searchList.add(name);
}
}
return _searchList.map((contact) => ChildItem(contact)).toList();
}
}
Widget buildBar(BuildContext context) {
return AppBar(centerTitle: true, title: appBarTitle, actions: <Widget>[
IconButton(
icon: actionIcon,
onPressed: () {
setState(() {
if (this.actionIcon.icon == Icons.search) {
this.actionIcon = Icon(
Icons.close,
color: Colors.white,
);
this.appBarTitle = TextField(
controller: _searchQuery,
style: TextStyle(
color: Colors.white,
),
decoration: InputDecoration(
prefixIcon: Icon(Icons.search, color: Colors.black),
hintText: "Search...",
hintStyle: TextStyle(color: Colors.white)),
);
_handleSearchStart();
} else {
_handleSearchEnd();
}
});
},
),
]);
}
void _handleSearchStart() {
setState(() {
_IsSearching = true;
});
}
void _handleSearchEnd() {
setState(() {
this.actionIcon = Icon(
Icons.search,
color: Colors.white,
);
this.appBarTitle = Text(
"Search Sample",
style: TextStyle(color: Colors.white),
);
_IsSearching = false;
_searchQuery.clear();
});
}
}
class ChildItem extends StatelessWidget {
final String name;
ChildItem(this.name);
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(this.name), //onTap: () => print(name));
onTap: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => ShowData()));
},
);
}
}
The screen I want to to show the result:
import 'search_bar_no_API.dart';
class ShowData extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text('The Name Here'),
),
What I want is the person to : eg: search: google press: the name google then go to other page with the name Google on the appbar.
Upvotes: 0
Views: 168
Reputation: 1003
In your ChildItem
class do this:
class ChildItem extends StatelessWidget {
final String name;
ChildItem(this.name);
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(this.name), //onTap: () => print(name));
onTap: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => ShowData(title: this.name)));
},
);
}
}
you will notice, I passed in this.name
to the property title
of the ShowData
class in the Navigator.push(...)
and so in your ShowData
class, create a final
variable of type String
eg. final String title
and create a Constructor
of the class ShowData
like this ShowData({this.title})
. Below is a complete code of how the ShowData
class should look like:
class ShowData extends StatelessWidget {
final String title;
ShowData({this.title});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text(title),
)
);
}
}
Upvotes: 1