动态创建TextField

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:trade_app/base/baseColor.dart';
import 'package:trade_app/base/baseSize.dart';
import 'package:trade_app/base/baseTextStyle.dart';
import 'package:trade_app/pages/utils/icon_utils.dart';

class StoreListPage extends StatefulWidget {
  @override
  StoreListPageState createState() => StoreListPageState();
}

class StoreListPageState extends State<StoreListPage> {
  FocusScopeNode node = FocusScopeNode();

  ///数据模型
  var stringListReturnedFromApiCall = ["first", "second", "third", "fourth"];
  //TextEditingController数组
  List textEditingControllers = <TextEditingController>[];

  var cellEdits = <Widget>[];

  @override
  void initState() {
    super.initState();
    stringListReturnedFromApiCall.forEach((str) {
      final textEditingController = new TextEditingController(text: str);
      textEditingControllers.add(textEditingController);
      return cellEdits
          .add(cellEdit(context, str, str, textEditingController, node));
    });
  }

  @override
  Widget build(BuildContext context) {
    BaseSize.getInstance().init(context);
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        brightness: Brightness.light,
        leading: IconButton(
            icon: Image.asset(IconUtils.getIconPath('fanhui')),
            onPressed: () => Navigator.pop(context)),
        title: Text('店铺详情',
            style: TextStyle(color: BaseColor.colorFF262626, fontSize: 18)),
        backgroundColor: Colors.white,
      ),
      body: Container(
          child: FocusScope(
        node: node,
        child: ListView(
          children: <Widget>[
            new Column(
              children: cellEdits,
            )
          ],
        ),
      )),
      backgroundColor: BaseColor.colorFFF5F5F5,
    );
  }
}

Widget cellEdit(
  BuildContext context,
  String name,
  String pleame,
  TextEditingController _textEditingController,
  FocusScopeNode node,
) {
  return Container(
    padding: EdgeInsets.only(left: 16, right: 16),
    height: BaseSize.dp(56),
    width: BaseSize.screenWidth - BaseSize.dp(32),
    color: Colors.white,
    child: Row(
      children: <Widget>[
        SizedBox(
          width: 100,
          child: Text(name, style: BaseTextStyle.style999999_14),
        ),
        Expanded(
            child: TextField(
          controller: _textEditingController,
          style: BaseTextStyle.style262626_14, //文本框内容风格
          textInputAction: TextInputAction.next, //右下角键盘文字:下一项
          decoration: InputDecoration(
            border: InputBorder.none, // 去掉下划线
            hintText: '请填写', //占位字符
            hintStyle: BaseTextStyle.styleD9D9D9_14, //占位字符样式
          ),
          obscureText: false, //明/密文
          inputFormatters: <TextInputFormatter>[
            LengthLimitingTextInputFormatter(16) //限制长度
          ],
          onEditingComplete: () {
            if (node.focusedChild == node.children.last) {
              print('submit');
              
            } else {
              node.nextFocus();
            }
          },
        )),
      ],
    ),
  );
}

参考

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页