Flutter-TextField的动态后缀文字吗?

时间:2020-02-09 15:30:22

标签: flutter textfield

我正在Flutter中构建一个简单的音板应用程序,该应用程序顶部包含一个搜索栏。

在激活搜索时,小部件树的有趣部分如下:

home: Scaffold(
          appBar: AppBar(
            title: new TextField(
              controller: _filter,
              decoration: new InputDecoration(
                prefixIcon: new Icon(Icons.search, color: Colors.white),
                hintText: 'Search...'
          ),

在搜索栏中键入文本后,_filter上的侦听器将更新引号列表,该引号用于构建应用程序主体。一切正常。

但是,我现在希望该应用显示返回结果的计数,并且由于它位于我的标题栏中,因此我希望它与该栏本身位于同一行,例如:

enter image description here

InputDecoration中,我尝试过的所有事情:

  • 设置suffixText-样式正确并在正确的位置,但是随着_filter的更改它不会更新,因为我不是每次都重构TextField我无法执行此操作,因为它弄乱了输入的内容。
  • 使用自己的控制器将suffix设置为完整的TextField小部件。这样可以自动更新,但是由于某种原因使我的实际搜索文字模糊。我尝试使背景透明,但这无济于事-new TextField(controller: _searchCountController, readOnly: true, textAlign: TextAlign.end, style: TextStyle(backgroundColor: Colors.transparent),)。在下面的屏幕截图中,我输入了“ w”,该计数已更新为84(但看不到我输入的“ w” ...)

enter image description here

  • 设置counter而不是suffix。我可以使它自动更新,并且不会模糊搜索,但是它出现在搜索栏的下方,这使整个事情看起来很麻烦。似乎不太适合标题栏中的内容。

enter image description here

关于如何实现自我追求的任何建议?对Flutter来说是非常新的事物,所以我很可能错过了一些显而易见的事情。希望对此有一个简单的解决方案:)

2 个答案:

答案 0 :(得分:0)

您可以在下面复制粘贴运行完整代码
您可以将RowExpanded(TextField)Text()一起使用
代码段

AppBar(
          title: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Expanded(
            child: TextField(
                controller: _filter,
                decoration: InputDecoration(
                  prefixIcon: Icon(Icons.search, color: Colors.white),
                  hintText: 'Search...',
                )),
          ),
          Text('$_counter'),
        ],
      ))

工作演示

enter image description here

完整代码

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  TextEditingController _filter = TextEditingController();
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Expanded(
            child: TextField(
                controller: _filter,
                decoration: InputDecoration(
                  prefixIcon: Icon(Icons.search, color: Colors.white),
                  hintText: 'Search...',
                )),
          ),
          Text('$_counter'),
        ],
      )),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

答案 1 :(得分:0)

好吧,正如预期的那样,事实证明我缺少明显的东西,并且有一个简单的解决方案。我在获取简单的suffixText时遇到了麻烦,因为我最终要缓存组件,因此没有给Flutter重新渲染它的机会。

万一它对任何人都有帮助,我已按照this post来实现搜索栏。他们使用的模式是存储_appBarTitle小部件,该小部件仅在启动和取消搜索后才会更改。我放弃了这种方法,取而代之的是使用一个简单的_searching布尔值,然后让flutter来完成其余工作:

Widget _buildAppBar() {
    if (_searching) {
      return new TextField(
          controller: _filter,
          decoration: new InputDecoration(
            prefixIcon: new Icon(Icons.search, color: Colors.white),
            hintText: 'Search...',
            suffixText: '${_filteredQuotes.length}',
          ),
          autofocus: true,
          style: TextStyle(color: Colors.white));
    } else {
      return new Text('Pocket Scat');
    }
  }

使用这种方法,我根本不需要为后缀指定组件。它还具有自动从我的应用样式中获取hintColor的优势。