2017-07-09 10 views
2

私はTextFormFieldでユーザー入力を収集しており、ユーザーが完了したことを示すFloatingActionButtonを押すと、オンスクリーンキーボードを消したいと思います。画面上のキーボードを消すにはどうしたらいいですか?

キーボードを自動的に消すにはどうすればよいですか?

import 'package:flutter/material.dart'; 

class MyHomePage extends StatefulWidget { 
    MyHomePageState createState() => new MyHomePageState(); 
} 

class MyHomePageState extends State<MyHomePage> { 
    TextEditingController _controller = new TextEditingController(); 

    @override 
    Widget build(BuildContext context) { 
    return new Scaffold(
     appBar: new AppBar(), 
     floatingActionButton: new FloatingActionButton(
     child: new Icon(Icons.send), 
     onPressed:() { 
      setState(() { 
      // send message 
      // dismiss on screen keyboard here 
      _controller.clear(); 
      }); 
     }, 
    ), 
     body: new Container(
     alignment: FractionalOffset.center, 
     padding: new EdgeInsets.all(20.0), 
     child: new TextFormField(
      controller: _controller, 
      decoration: new InputDecoration(labelText: 'Example Text'), 
     ), 
    ), 
    ); 
    } 
} 

class MyApp extends StatelessWidget { 
    @override 
    Widget build(BuildContext context) { 
    return new MaterialApp(
     home: new MyHomePage(), 
    ); 
    } 
} 

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

答えて

6

あなたはTextFormFieldのフォーカスを奪うと、未使用のFocusNodeにそれを与えることによって、キーボードを消すことができます。

FocusScope.of(context).requestFocus(new FocusNode()); 
+0

どこでこのコードを実装するのでしょうか? TextFormFieldでは、たぶん 'onChanged:'の後や、カスタムボタンの動作の中で? –

関連する問題