【问题标题】:Problem with textfield in my first Flutter app build我的第一个 Flutter 应用程序构建中的文本字段问题
【发布时间】:2020-09-28 01:10:38
【问题描述】:

我正在学习 Flutter,并开发了我的第一个待办事项应用程序。我遇到的问题是该应用程序在我的设备和模拟器上的调试模式下运行良好,但是当我运行 flutter run build 命令并安装发布 apk 时,您输入 Todo 项的文本字段不起作用,而是我得到一个灰色框. 我会附上一些图片来澄清。我是菜鸟,所以很可能我错过了一些东西。我只是想将我的应用程序作为发布 apk 进行测试,看看它是否流畅。

感谢您的帮助!

这是 vscode 安装在我的摩托罗拉机上的调试 apk

这是该对话框在发布 apk 上的样子

我已上传项目供您查看,但这里是表单代码和列表

TodoItemForm.dart:

import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:todo/models/TodoItemModel.dart';

class TodoItemForm extends StatefulWidget {
  TodoItemForm({
    Key key,
    @required this.context,
    this.item,
    this.onSubmit,
    this.onClose,
  }) : super(key: key) {
    if (this.item == null)
      this.item = new TodoItemModel("", false, DateTime.now(), DateTime.now());
  }

  final BuildContext context;
  TodoItemModel item;
  final ValueChanged<TodoItemModel> onSubmit;
  final VoidCallback onClose;
  @override
  _TodoItemFormState createState() => _TodoItemFormState();
}

class _TodoItemFormState extends State<TodoItemForm> {
  TextEditingController _todoItemTextController = new TextEditingController();
  @override
  void initState() {
    super.initState();
    if (widget.item != null) {
      _todoItemTextController.value = TextEditingValue(text: widget.item.text);
    } else {
      widget.item = new TodoItemModel(
          _todoItemTextController.text, false, DateTime.now(), DateTime.now());
    }
  }

  void onSubmit() {
    widget.item.text = _todoItemTextController.text;
    widget.onSubmit(widget.item);
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Row(
        children: <Widget>[
          Container(
            margin: EdgeInsets.fromLTRB(0, 0, 10, 0),
            child: Icon(
              Icons.playlist_add,
              color: Theme.of(context).primaryColor,
            ),
          ),
          Text(
            "New To Do Item",
          ),
        ],
      ),
      insetPadding: EdgeInsets.symmetric(horizontal: 2),
      content: Expanded(
        child: TextField(
          controller: _todoItemTextController,
          autofocus: true,
          decoration: InputDecoration(
            labelText: "Task to do:",
            hintText: "Buy Groseries!",
          ),
        ),
      ),
      actions: <Widget>[
        FlatButton(
          onPressed: this.onSubmit,
          child: Text(
            "SAVE",
            style: new TextStyle(color: Theme.of(context).accentColor),
          ),
        ),
        FlatButton(
          onPressed: widget.onClose,
          child: Text(
            "CANCEL",
            style: new TextStyle(color: Theme.of(context).accentColor),
          ),
        ),
      ],
    );
  }
}

TaskList.dart:

import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:todo/models/TodoItemModel.dart';
import 'package:todo/services/TodoServiceProvider.dart';
import 'package:todo/widgets/TodoItem.dart';
import 'package:todo/widgets/TodoItemForm.dart';

class TaskList extends StatefulWidget {
  TaskList({Key key}) : super(key: key);

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

class _TaskListState extends State<TaskList> {
  List<TodoItemModel> _items = [];
  final _todoItemTextController = new TextEditingController();

  @override
  void initState() {
    super.initState();
    this.refreshTodos();
  }

  void refreshTodos() {
    TodoServiceProvider.getTodoItems().then((todoList) {
      setState(() {
        _items = todoList;
      });
    });
  }

  void _handleSubmit(TodoItemModel newItem) {
    TodoServiceProvider.createTodo(newItem).then((todoItem) {
      this.refreshTodos();
      this._handleClose();
    });
  }

  void _handleEdit(TodoItemModel item) {
    TodoServiceProvider.updateTodo(item).then((todoItem) {
      this.refreshTodos();
      this._handleClose();
    });
  }

  void _handleClose() {
    Navigator.pop(context);
    _todoItemTextController.clear();
  }

  Future<bool> _handleItemCompleted(TodoItemModel model, DismissDirection dir) {
    return TodoServiceProvider.deleteTodo(model.id).then((response) {
      if (response) {
        setState(() {
          _items.remove(model);
        });
        return Future.value(true);
      }
      return Future.value(false);
    }).catchError((error) => Future.value(false));
  }

  void _showTodoItemForm({TodoItemModel item: null}) {
    final alert = TodoItemForm(
      context: context,
      item: item,
      onSubmit: item == null ? this._handleSubmit : this._handleEdit,
      onClose: this._handleClose,
    );

    showDialog(
      context: context,
      builder: (_) {
        return alert;
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Todo"),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: _showTodoItemForm,
      ),
      body: Container(
        padding: EdgeInsets.all(12),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(
              child: ReorderableListView(
                onReorder: (oldIndex, newIndex) {
                  this.setState(() {
                    final aux = _items[oldIndex];
                    if (oldIndex > newIndex) {
                      _items.removeAt(oldIndex);
                      _items.insert(newIndex, aux);
                    } else {
                      _items.insert(newIndex, aux);
                      _items.removeAt(oldIndex);
                    }
                  });
                },
                children: [
                  for (final _item in _items)
                    FlatButton(
                      key: ValueKey(_item),
                      child: TodoItem(
                        model: _item,
                        onItemCompleted: this._handleItemCompleted,
                      ),
                      onPressed: () {
                        this._showTodoItemForm(item: _item);
                      },
                    ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}

TodoItemModel.dart:

import 'package:todo/widgets/TodoItem.dart';

class TodoItemModel {
  int _id;
  String _text;
  bool _finished;
  DateTime _creationDate;
  DateTime _dueDate;

  TodoItemModel(this._text, this._finished, this._creationDate, this._dueDate);

  int get id => _id;
  String get text => _text;
  DateTime get creationDate => _creationDate;

  void set text(String value) {
    _text = value;
  }

  void set id(int value) => _id = value;

      
  Map<String, dynamic> toJSON() {
    var map = new Map<String, dynamic>();
    map["text"] = _text;
    map["creation_date"] = _creationDate.toIso8601String();
    if (_id != null) map["id"] = _id;

    return map;
  }

  TodoItemModel.fromJSON(Map<String, dynamic> json) {
    this._id = json["id"];
    this._text = json["text"];
    this._creationDate = DateTime.parse(json["creation_date"]);
  }
}

完整的项目网址: https://drive.google.com/drive/folders/1tNue3EfdwV_7M7zHt_A7A4RsNIdppHqj?usp=sharing

【问题讨论】:

  • 你能分享你的代码,这样我就可以跟踪你的问题。当错误出现时,Flutter 在发布模式下显示灰屏。
  • @NikhilVadoliya 抱歉耽搁了,感谢您的回答,希望您仍然可以帮助我...我已经用代码和完整项目的链接更新了我的问题。提前致谢
  • 请添加 TodoItemModel 文件或分享 github/git 链接
  • 解压不了
  • @NikhilVadoliya 感谢您的帮助!,我已将模型代码添加到问题中,并使用文件夹而不是压缩文件更新了链接......我在 github 上没有这个项目所以它这种方式更快。再次感谢。

标签: android flutter flutter-layout


【解决方案1】:

我认为问题出在 AlertDialog 中的Expanded Widget

TodoItemForm.dart:

import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:todo/models/TodoItemModel.dart';

class TodoItemForm extends StatefulWidget {
  TodoItemForm({
    Key key,
    @required this.context,
    this.item,
    this.onSubmit,
    this.onClose,
  }) : super(key: key) {
    if (this.item == null)
      this.item = new TodoItemModel("", false, DateTime.now(), DateTime.now());
  }

  final BuildContext context;
  TodoItemModel item;
  final ValueChanged<TodoItemModel> onSubmit;
  final VoidCallback onClose;
  @override
  _TodoItemFormState createState() => _TodoItemFormState();
}

class _TodoItemFormState extends State<TodoItemForm> {
  TextEditingController _todoItemTextController = new TextEditingController();
  @override
  void initState() {
    super.initState();
    if (widget.item != null) {
      _todoItemTextController.value = TextEditingValue(text: widget.item.text);
    } else {
      widget.item = new TodoItemModel(
          _todoItemTextController.text, false, DateTime.now(), DateTime.now());
    }
  }

  void onSubmit() {
    widget.item.text = _todoItemTextController.text;
    widget.onSubmit(widget.item);
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Row(
        children: <Widget>[
          Container(
            margin: EdgeInsets.fromLTRB(0, 0, 10, 0),
            child: Icon(
              Icons.playlist_add,
              color: Theme.of(context).primaryColor,
            ),
          ),
          Text(
            "New To Do Item",
          ),
        ],
      ),
      insetPadding: EdgeInsets.symmetric(horizontal: 2),
      content: Container( //Change this line
        child: TextField(
          controller: _todoItemTextController,
          autofocus: true,
          decoration: InputDecoration(
            labelText: "Task to do:",
            hintText: "Buy Groseries!",
          ),
        ),
      ),
      actions: <Widget>[
        FlatButton(
          onPressed: this.onSubmit,
          child: Text(
            "SAVE",
            style: new TextStyle(color: Theme.of(context).accentColor),
          ),
        ),
        FlatButton(
          onPressed: widget.onClose,
          child: Text(
            "CANCEL",
            style: new TextStyle(color: Theme.of(context).accentColor),
          ),
        ),
      ],
    );
  }
}

【讨论】:

  • 我会检查这个。对我来说似乎很奇怪的是开发 apk 工作正常,但构建 apk 是破坏的。我将在今天晚些时候对此进行测试。谢谢!
  • 我在发布版本的对话框中遇到了同样的问题,但对我来说,是对话框中的“Spacer()”导致了这种行为。
猜你喜欢
  • 1970-01-01
  • 2021-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
  • 2020-09-17
  • 1970-01-01
  • 2021-06-14
相关资源
最近更新 更多