简体   繁体   English

使用另一个窗口小部件中的按钮插入TextField-Flutter

[英]Insert into TextField using buttons from another widget - Flutter

I have a TextField that serves as a search bar where the user can use the built in Android/iOS keyboard to type but also has the possibility to insert a special characters in the search bar from a button. 我有一个TextField用作搜索栏,用户可以在其中使用内置的Android / iOS键盘键入内容,也可以通过按钮在搜索栏中插入特殊字符。 In a way the typing and the other insertion is combined into one string 以某种方式将键入和另一插入组合到一个字符串中

use case: The user types hell in the search bar then presses the button widget the search bar value becomes : hellö 用例:用户在搜索栏中键入hell ,然后按按钮小部件,搜索栏的值变为:hellö

I set up everything but when I click the button nothing happens (the typing from the keyboard works fine) 我进行了所有设置,但是当我单击按钮时,什么也没有发生(通过键盘键入可以正常工作)

Here's my code: 这是我的代码:

//I have this as a global variable
TextEditingController _searchInputControllor = TextEditingController();

//This is the TextField
class _SearchBarState extends State<SearchBar> { 
  @override
  Widget build(BuildContext context) {
    return TextField( 
    enableInteractiveSelection: false,                     
    controller: _searchInputControllor,
    cursorColor: primaryDark,
    decoration: InputDecoration(
      contentPadding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 15.0),
      border: InputBorder.none,
      hintText: "Search...",
      suffixIcon: Material(                     
        color: Colors.white,
        elevation: 6.0,
        borderRadius: BorderRadius.all(Radius.circular(6.0),),
        child: InkWell(
          splashColor: Colors.greenAccent,
          onTap: () {},
          child: Icon(Icons.search, color: primaryDark,),
         ),
       ),
     ),
   );
  }
} 

//This is the button widget
//It is supposed to add to the search bar but nothing happens
class _SpecialCharState extends State<SpecialChar> {
  @override
  Widget build(BuildContext context) {
    return ButtonTheme(
      minWidth: 40.0,
      child: FlatButton(
        color: Colors.transparent,
        textColor: Colors.black,
        padding: EdgeInsets.all(8.0),
        splashColor: Colors.blue,
        onPressed: () {
          setState(() {
            _searchInputControllor.text = _searchInputControllor.text + widget.btnVal.toLowerCase();
          });
        },
        child: Text(
          widget.btnVal
        ),
      )
    );
   }
  }

A. No problem at all A.没问题

I think your code is working well as I tried on my Android Phone Demo . 我认为您的代码运行良好,就像我在Android Phone 演示中尝试的那样。

The text field is changed as I tap the buttons. 当我点击按钮时,文本字段将更改。

B. Change cursor position B.更改光标位置

Nonetheless, I add this code to make the cursor automatically placed on last character. 尽管如此,我还是添加了此代码以使光标自动置于最后一个字符上。

Rather than directly changed the text, we copy its value which contains selection. 我们将复制包含选择内容的值,而不是直接更改文本。 Later we offset its selection by length of newText 稍后我们将其选择偏移 newText的长度

void appendCharacters() {
  String oldText = _searchInputControllor.text;
  String newText = oldText + widget.btnVal.toLowerCase();

  var newValue = _searchInputControllor.value.copyWith(
    text: newText,
    selection: TextSelection.collapsed(
      offset: newText.length, //offset to Last Character
    ),
    composing: TextRange.empty,
  );

  _searchInputControllor.value = newValue;
}

so we can trigger the method with code below : 因此我们可以使用以下代码触发该方法:

Widget build(BuildContext context) {
  return ButtonTheme(
    minWidth: 40.0,
    child: FlatButton(
      onPressed: appendCharacters, // call a function
    ),
  );
}

Working App Repository 工作应用程序存储库

You may look into this repo and build yourself. 您可以查看此存储库并自己构建。 Github Github上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM