gpt4 book ai didi

flutter - 在屏幕之间传递数据

转载 作者:IT王子 更新时间:2023-10-29 07:18:10 26 4
gpt4 key购买 nike

我是 Flutter 的新手,我想在屏幕之间传递一些数据。

我知道这里有人问了一个类似的问题,但我尝试了那个解决方案,但我的代码不起作用。

我尝试:“在 Flutter 中的屏幕之间传递数据”

搜索条码:

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()));
},
);
}
}

我想要显示结果的屏幕:

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'),
),

我想要的是:例如:搜索:谷歌按:名称谷歌然后转到应用栏上名称为 Google 的其他页面。

最佳答案

在您的 ChildItem 类中执行此操作:

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)));
},
);
}
}

你会注意到,我将 this.name 传递给了 Navigator.push 中 ShowData 类的属性 title (...)因此在您的 ShowData 类中,创建一个类型为 Stringfinal 变量,例如。 final String title 并创建类 ShowDataConstructor,就像这样 ShowData({this.title})。以下是 ShowData 类的完整代码:

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),
)
);
}
}

enter image description here

关于flutter - 在屏幕之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56648974/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com