防止键盘出现在Flutter上

时间:2020-01-03 17:13:08

标签: flutter

我做了这个非常简单的应用,但是我有一个问题。 在我执行的每个操作(如删除某些内容)上,键盘都会自动打开。 我想避免这种情况,只在我单击文本表单字段(代码上的178行)时才打开键盘。 我怎样才能做到这一点? 我知道这很简单,我已经使用focusnode尝试了一些事情,试图禁用自动对焦,但是我没能按照我需要的方式使它工作。

import 'dart:convert';
import 'dart:ffi';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:http/http.dart' as http;

void main() {
  runApp(MaterialApp(
    home: Pedidos(),
  ));
}

class Pedidos extends StatefulWidget {
  @override
  _PedidosState createState() => _PedidosState();
}

class _PedidosState extends State<Pedidos> with TickerProviderStateMixin {
  TabController _tabController;

  final _pecasController = TextEditingController();

  List _pecasList = [];
  var _firstPress = true;

  @override
  void initState() {
    super.initState();
    _tabController = new TabController(length: 3, vsync: this);
  }

  void _addPecas() {
    if ((_pecasController.text.isEmpty) ||
        ((_pecasController.text.trimLeft() == ("")))) {
      print("Campo Vazio");
    } else {
      setState(() {
        Map<String, dynamic> newPeca = Map();
        newPeca["title"] = _pecasController.text.trimLeft();
        //newPeca["ok"] = false;
        _pecasController.text = "";
        _pecasList.add(newPeca);
        // _saveData();
        print(_pecasList);
      });
    }
  }

  void _enviar() {
    if (_pecasList.length < 1) {
      showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
                title: new Text("Lista vazia"),
                actions: <Widget>[
                  new FlatButton(
                      child: new Text("Fechar"),
                      onPressed: () {
                        Navigator.of(context).pop();
                      }),
                ]);
          });
    } else {
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: new Text("Deseja enviar os itens?"),
            actions: <Widget>[
              new FlatButton(
                  child: new Text("Fechar"),
                  onPressed: () {
                    Navigator.of(context).pop();
                  }),
              new FlatButton(
                  child: new Text("Enviar"),
                                    onPressed: () async {
                    Map<String, String> headers = new Map<String, String>();
                    headers["Content-type"] = "application/json";
                    headers["Accept"] = "application/json";
                    //String str = '{"take":55, "skip":"0"}';
                    final resp = await http.post('http://172.16.14.109:5000/',
                        //body: str,
                        body: jsonEncode(_pecasList),
                        headers: headers);

                    if (resp.statusCode == 200) {
                      if (resp.body == "ok") {
                        setState(() {
                          print(_pecasList);
                          _pecasList.clear();
                          Navigator.of(context).pop();
                        });
                      } else {
                        showDialog(
                            context: context,
                            builder: (BuildContext context) {
                              return AlertDialog(
                                  title: new Text(
                                      "Erro: entre em contato com o suporte."),
                                  actions: <Widget>[
                                    new FlatButton(
                                        child: new Text("Fechar"),
                                        onPressed: () {
                                          Navigator.of(context).pop();
                                          Navigator.of(context).pop();
                                        }),
                                  ]);
                            });
                      }
                    }
                  })
            ],
          );
        },
      );
    }
  }

  void _apagarTudo() {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
              title: new Text("Deseja limpar a lista?"),
              actions: <Widget>[
                new FlatButton(
                    child: new Text("Fechar"),
                    onPressed: () {
                      Navigator.of(context).pop();
                    }),
                new FlatButton(
                    child: new Text("Limpar"),
                    onPressed: () {
                      setState(() {
                        _pecasList.clear();
                        Navigator.of(context).pop();
                      });
                    }),
              ]);
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "Solicitação de Peças",
          style: TextStyle(fontWeight: FontWeight.bold),
        ),
        centerTitle: true,
        backgroundColor: Colors.green,
      ),
      body: Column(
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Row(
              children: <Widget>[
                Expanded(
                  flex: 8,
                  child: TextFormField(
                    controller: _pecasController,
                    keyboardType: TextInputType.text,                  
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 18,
                    ),
                    decoration: InputDecoration(
                      hintText: ("Adicionar item"),
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(right: 15),
                ),
                Expanded(
                  flex: 2,
                  child: RaisedButton(
                    child: Icon(Icons.add, color: Colors.amber),
                    color: Colors.green,
                    onPressed: _addPecas,
                  ),
                )
              ],
            ),
          ),
          Divider(
            height: 02.0,
          ),
          Expanded(
            child: ListView.builder(
                itemCount: _pecasList.length,
                itemBuilder: (context, index) {
                  return ListTile(
                      dense: true,
                      key:
                          Key(DateTime.now().millisecondsSinceEpoch.toString()),
                      title: Text(
                        _pecasList[index]["title"],
                        style: TextStyle(fontSize: 15.0),
                      ),
                      trailing: Icon(
                        Icons.delete_forever,
                        color: Colors.redAccent,
                      ),
                      onLongPress: () {
                        showDialog(
                          context: context,
                          builder: (BuildContext context) {
                            return AlertDialog(
                              title:
                                  new Text("Deseja remover o item da lista?"),
                              actions: <Widget>[
                                new FlatButton(
                                  child: new Text("Fechar"),
                                  onPressed: () {
                                    Navigator.of(context).pop();
                                  },
                                ),
                                new FlatButton(
                                  child: new Text("Excluir"),
                                  onPressed: () {
                                    _pecasList.removeAt(index);
                                    setState(() {
                                      Navigator.of(context).pop();
                                    });
                                  },
                                ),
                              ],
                            );
                          },
                        );
                      });
                }),
          ),
          Row(
            mainAxisAlignment: (MainAxisAlignment.center),
            children: <Widget>[
              Padding(
                  padding: const EdgeInsets.all(10.0),
                  child: RaisedButton(
                    color: Colors.green,
                    padding: EdgeInsets.all(5.0),
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Padding(
                          padding: const EdgeInsets.all(4.0),
                          child: Icon(
                            Icons.send,
                            color: Colors.white,
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.all(2.0),
                          child: Text(
                            "Enviar",
                            style: TextStyle(
                              color: Colors.white,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                      ],
                    ),
                    onPressed: _enviar,
                  )),
              Padding(
                  padding: const EdgeInsets.all(10.0),
                  child: RaisedButton(
                      color: Colors.redAccent,
                      padding: EdgeInsets.all(5.0),
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: <Widget>[
                          Padding(
                            padding: const EdgeInsets.all(4.0),
                            child: Icon(
                              Icons.delete,
                              color: Colors.white,
                            ),
                          ),
                          Padding(
                            padding: const EdgeInsets.all(2.0),
                            child: Text(
                              "Limpar",
                              style: TextStyle(
                                color: Colors.white,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ),
                        ],
                      ),
                      onPressed: () {
                        _apagarTudo();
                      }))
            ],
          )
        ],
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

尝试这些更改。

我已控制${bot.guilds.size}的{​​{1}}字段。当您单击RaisedButton将项目添加到列表时,enabled被禁用。当您点击TextFormField时就会启用它。

TextFormField