edit_text_page.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import 'package:flutter/material.dart';
  2. import 'package:liftmanager/res/resources.dart';
  3. import 'package:liftmanager/utils/toast.dart';
  4. import 'package:liftmanager/widgets/app_bar.dart';
  5. class EditTextPage extends StatefulWidget {
  6. String title = "";
  7. String value = "";
  8. int mType = 0;
  9. EditTextPage({this.title, this.value, this.mType});
  10. @override
  11. State<StatefulWidget> createState() {
  12. return EditTextState();
  13. }
  14. }
  15. class EditTextState extends State<EditTextPage> {
  16. TextEditingController _valueController = TextEditingController();
  17. ///保存数据
  18. void saveEditText() {
  19. String value = _valueController.text.toString();
  20. if (value.length == 0) {
  21. toasts("请输入内容");
  22. return;
  23. } else {
  24. Navigator.of(context).pop(value);
  25. }
  26. }
  27. @override
  28. void initState() {
  29. super.initState();
  30. _valueController = TextEditingController.fromValue(
  31. TextEditingValue(
  32. text: widget.value
  33. ),
  34. );
  35. }
  36. @override
  37. Widget build(BuildContext context) {
  38. return Scaffold(
  39. appBar: MyAppBar(
  40. centerTitle: widget.title,
  41. actions: <Widget>[
  42. FlatButton(
  43. child: Text("完成"),
  44. textColor: Colours.dark_text,
  45. highlightColor: Colors.transparent,
  46. onPressed: () {
  47. saveEditText();
  48. },
  49. )
  50. ],
  51. ),
  52. body: ListView(
  53. children: <Widget>[
  54. _bodyWidget(),
  55. ],
  56. ),
  57. );
  58. }
  59. Widget _bodyWidget() {
  60. switch (widget.mType) {
  61. case 0:
  62. return singleLineEditText();
  63. break;
  64. case 1:
  65. return moreLineEditText();
  66. break;
  67. }
  68. }
  69. Widget singleLineEditText() {
  70. return Container(
  71. height: 50,
  72. padding: EdgeInsets.only(left: 15, right: 15),
  73. child: TextField(
  74. controller: _valueController,
  75. decoration: InputDecoration(
  76. border: InputBorder.none,
  77. hintText: "请填写",
  78. hintStyle: TextStyle(color: Colours.text),
  79. ),
  80. maxLines: 1,
  81. ),
  82. );
  83. }
  84. Widget moreLineEditText() {
  85. return Container(
  86. height: 100,
  87. padding: EdgeInsets.only(left: 15, right: 15),
  88. decoration: BoxDecoration(
  89. border: Border(
  90. bottom: BorderSide(
  91. color: Colours.text, style: BorderStyle.solid, width: 0.5))),
  92. child: TextField(
  93. maxLength: 30,
  94. controller: _valueController,
  95. decoration: InputDecoration(
  96. border: InputBorder.none,
  97. hintText: "请填写",
  98. hintStyle: TextStyle(color: Colours.text),
  99. ),
  100. maxLines: 2,
  101. ),
  102. );
  103. }
  104. }