gpt4 book ai didi

flutter - 如何将searchBar放入appBar - Flutter?

转载 作者:行者123 更新时间:2023-12-04 10:21:57 27 4
gpt4 key购买 nike

我无法将搜索栏放在 AppBar 中,
现在我的搜索栏低于我的 AppBar ,我尝试使用另一个 Container进入我的 AppBar但没有成功。

我的代码:

class _HomePageState extends State<HomePage> {

@override
Widget build(BuildContext context) {
return MaterialApp(
home:Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(100.0),
child: AppBar(
iconTheme: IconThemeData(color: Color.fromRGBO(9, 133, 46, 100)),
backgroundColor: Colors.white,
actions: <Widget>[
IconButton(
icon: Icon(
Icons.shopping_cart,
color: Color.fromRGBO(9, 133, 46, 100),
),
onPressed: (){
print('klikniete');
},
),
],
),
),
body: Builder(
builder: (context) => Container(
child: FutureBuilder(
future: fetchOrders(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (_ordersForDisplay.length == null) {
return Container(
child: Center(child: Text("Ładowanie...")),
);
} else {
return ListView.builder(
itemCount: _ordersForDisplay.length + 1,
itemBuilder: (BuildContext context, int index) {
return index == 0 ? _searchBar() : _listItem(index - 1);
},
);
}
},
),
),
),
)
);
}

_searchBar() {
return Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
decoration: InputDecoration(
hintText: 'Wyszukaj po mieście...'
),
onChanged: (text) {
text = text.toLowerCase();
setState(() {
_ordersForDisplay = _orders.where((note) {
var noteTitle = note.city.toLowerCase();
return noteTitle.contains(text);
}).toList();
});
},
),
);
}



_listItem(index) {
return GestureDetector(
onTap: () => Navigator.of(context).push(
MaterialPageRoute(builder: (context) => DetailPage(item: _ordersForDisplay[index])),
),
child: Card(
child: Padding(
padding: const EdgeInsets.only(
top: 32.0, bottom: 32.0, left: 16.0, right: 16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
_ordersForDisplay[index].firstName,
style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
),
Text(
_ordersForDisplay[index].lastName,
style: TextStyle(
color: Colors.grey.shade600
),
),
],
),
),
),
);
}
}

我通过使用 title: _searchBar 将 searchBar 放入我的 appBar ,接下来我删除 return index == 0 ? _searchBar() : _listItem(index - 1);仅粘贴 return _listItem(index, context) ,但现在我有错误: RangeError (index): Invalid value: Only valid value is 0: 1

最佳答案

你期待这个吗

enter image description here

或这个?

enter image description here

代码:

class CustomSearchBarDemo extends StatefulWidget {
@override
_CustomSearchBarDemoState createState() => _CustomSearchBarDemoState();
}

class _CustomSearchBarDemoState extends State<CustomSearchBarDemo> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0.0,
backgroundColor: Colors.white,
title: Text("Search",style: TextStyle(color: Colors.black),),
centerTitle: true,
bottom: PreferredSize(
preferredSize: Size.fromHeight(kToolbarHeight),
child: Container(
// padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.grey[300],
),
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Material(
color: Colors.grey[300],
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(Icons.search,color: Colors.grey),
Expanded(
child: TextField(
// textAlign: TextAlign.center,
decoration: InputDecoration.collapsed(
hintText: ' Search by name or address',
),
onChanged: (value) {

},
),
),
InkWell(
child: Icon(Icons.mic,color: Colors.grey,),
onTap: () {

},
)
],
),
),
)
) ,
),

),

);
}
}

或者
class CustomSearchBarDemo extends StatefulWidget {
@override
_CustomSearchBarDemoState createState() => _CustomSearchBarDemoState();
}

class _CustomSearchBarDemoState extends State<CustomSearchBarDemo> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar:
PreferredSize(
preferredSize: Size.fromHeight(kToolbarHeight),
child: Container(
padding: const EdgeInsets.only(top:20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.grey[300],
),
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Material(
color: Colors.grey[300],
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(Icons.search,color: Colors.grey),
Expanded(
child: TextField(
// textAlign: TextAlign.center,
decoration: InputDecoration.collapsed(
hintText: ' Search by name or address',
),
onChanged: (value) {

},
),
),
InkWell(
child: Icon(Icons.mic,color: Colors.grey,),
onTap: () {

},
)
],
),
),
)
) ,
),

);
}
}

关于flutter - 如何将searchBar放入appBar - Flutter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60813379/

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