gpt4 book ai didi

flutter - 如何在 Flutter 中的 Appbar 内添加文本字段?

转载 作者:行者123 更新时间:2023-12-03 02:45:52 24 4
gpt4 key购买 nike

我在使用 TextField 时遇到问题在 AppBar因此用户可以输入类似于使用搜索栏的输入。我需要接受用户输入并用它做一些事情,所以它不是我的应用程序中的搜索。这就是使用 TextField 有意义的原因。 .

现在,我已经成功地拥有了 TextField在我的左侧 AppBar .问题在于TextField是一个正方形,不占用足够的空间,所以你可以看到你在写什么。

链接到它的外观:

The search bar visually

在代码中,这是如何制作的:

 @override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Myseum',
theme: new ThemeData(
primarySwatch: Colors.blue,
fontFamily: 'Raleway',
),
home: Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(
"Myseum",
style: TextStyle(
fontFamily: 'Raleway',
fontStyle: FontStyle.italic,
fontSize: 25,
),
),
leading: prefix0.TextBox(), // TextBox is the widget I made.
backgroundColor: Colors.black,
),



现在小部件 TextBox()
class TextBox extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
alignment: Alignment.centerLeft,
color: Colors.white,
child: TextField(
decoration:
InputDecoration(border: InputBorder.none, hintText: 'Search'),
),
);
}
}

最佳答案

就像评论中提到的 - 将您的文本字段放在标题小部件中...我将您的代码转换为一个简单的有状态小部件,以给您一个想法。

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
bool typing = false;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: typing ? TextBox() : Text("Title"),
leading: IconButton(
icon: Icon(typing ? Icons.done : Icons.search),
onPressed: () {
setState(() {
typing = !typing;
});
},
),
),
body: Center(
child: Text("Your app content"),
),
);
}
}

class TextBox extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
alignment: Alignment.centerLeft,
color: Colors.white,
child: TextField(
decoration:
InputDecoration(border: InputBorder.none, hintText: 'Search'),
),
);
}
}

关于flutter - 如何在 Flutter 中的 Appbar 内添加文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56346660/

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