gpt4 book ai didi

flutter - 类似于Whatsapp的搜索图标,在单击时可在体内显示标题-使用Flutter

转载 作者:行者123 更新时间:2023-12-03 04:18:35 25 4
gpt4 key购买 nike

如何创建类似于Whatsapp在其appBar中但在正文中使用的搜索。
左边是标题,右边是放大镜图标。当您单击图标时,搜索输入将在标题上方打开。

最佳答案

更新了答案
enter image description here
enter image description here

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
bool _showSearch = false;
FocusNode _focusNode;

Widget _searchBar() {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: _showSearch
? Row(
children: [
IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
setState(() {
_showSearch = false;
FocusScope.of(context).unfocus();
});
},
),
Expanded(
child: TextField(
focusNode: _focusNode,
autofocus: true,
style: TextStyle(
color: Colors.white,
),
decoration: InputDecoration(
hintText: 'Filtro',
hintStyle: TextStyle(
color: Colors.white,
),
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
),
),
),
],
)
: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'WhatsApp',
),
IconButton(
icon: Icon(
Icons.search,
),
onPressed: () {
setState(() {
_showSearch = true;
FocusScope.of(context).requestFocus(_focusNode);
});
},
),
],
),
);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Search'),
),
body: Container(
color: Colors.grey[300],
padding: const EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
height: 100,
color: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
height: 50,
alignment: Alignment.center,
child: Text(
'Tab view here',
),
),
_searchBar(),
],
),
),
Expanded(
child: ListView(
padding: const EdgeInsets.all(5),
children: [
ListTile(
title: Text('Tile 1'),
subtitle: Text('Content'),
),
ListTile(
title: Text('Tile 2'),
subtitle: Text('Content'),
),
],
),
),
],
),
),
);
}
}
旧答案
你是这个意思吗
enter image description here
enter image description here
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
bool _showSearch = false;
FocusNode _focusNode;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: _showSearch
? IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
setState(() {
_showSearch = false;
FocusScope.of(context).unfocus();
});
},
)
: null,
title: _showSearch
? TextField(
focusNode: _focusNode,
autofocus: true,
style: TextStyle(
color: Colors.white,
),
decoration: InputDecoration(
hintText: 'Search...',
hintStyle: TextStyle(
color: Colors.white,
),
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
),
)
: Text('WhatsApp'),
actions: _showSearch
? []
: [
IconButton(
icon: Icon(
Icons.search,
color: Colors.white,
),
onPressed: () {
setState(() {
_showSearch = true;
FocusScope.of(context).requestFocus(_focusNode);
});
},
),
IconButton(
icon: Icon(
Icons.more_vert,
color: Colors.white,
),
),
],
),
body: Center(
child: Text('Content'),
),
);
}
}

关于flutter - 类似于Whatsapp的搜索图标,在单击时可在体内显示标题-使用Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63926050/

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