首页 > 解决方案 > 如何在文本字段颤动中获得价值

问题描述

如何在文本字段中输入值?我有从 api 获得的值,即名称数据在这里我编辑数据,然后想要更新数据的内容。但是如果不编辑数据,我仍然对初始化数据感到困惑

我附上代码:

class ProfileData extends StatefulWidget {
  @override
  _ProfileDataState createState() => _ProfileDataState();
}

class _ProfileDataState extends State<ProfileData> {

  TextEditingController fnameController = TextEditingController(text: '');
  
bool isLoading = false;

  @override
  Widget build(BuildContext context) {

    UserProvider user = Provider.of<UserProvider>(context);
    //here i call api using provider, if I print it will produce a value which is a name 'alu card' , ex : user.user.fname -> value('alu card')

    handleSubmit() async {
      setState(() {
        isLoading = true;
      });

      if (await user.submit( //method put to service
        fname: fnameController.text,
      )) {
        Navigator.pushNamed(context, '/main');
      } else { 
       print('err');
      }

      setState(() {
        isLoading = false;
      });
    }

   Widget input() {
      return Container(
        child: Column(
          children: [
            Expanded(
                child: TextFormField(
              style: blackTextStyle,
              controller: fnameController,
              decoration: InputDecoration.collapsed(
                hintText: user.user.fname, // this value 'alucard'
                hintStyle: blackTextStyle,
              ),
            ))
          ],
        ),
      );
     ....... //widget button submit 
     .......
      onpress : handleSubmit
    }

    return Scaffold(
      backgroundColor: Colors.white,
      body: input(),
    );
  }
}

标签: flutterflutter-layoutflutter-android

解决方案


试试下面的答案,我添加了一行来获取 Textfield 的价值

class _ProfileDataState extends State<ProfileData> {

  TextEditingController fnameController = TextEditingController(text: '');
  
bool isLoading = false;

  @override
  Widget build(BuildContext context) {

    UserProvider user = Provider.of<UserProvider>(context);
    fnameController.text = user.user.fname  // Added this line here.

    handleSubmit() async {
      setState(() {
        isLoading = true;
      });

      if (await user.submit( //method put to service
        fname: fnameController.text,
      )) {
        Navigator.pushNamed(context, '/main');
      } else { 
       print('err');
      }

      setState(() {
        isLoading = false;
      });
    }

   Widget input() {
      return Container(
        child: Column(
          children: [
            Expanded(
                child: TextFormField(
              style: blackTextStyle,
              controller: fnameController,
              decoration: InputDecoration.collapsed(
                hintText: user.user.fname, // this value 'alucard'
                hintStyle: blackTextStyle,
              ),
            ))
          ],
        ),
      );
     ....... //widget button submit 
     .......
      onpress : handleSubmit
    }

    return Scaffold(
      backgroundColor: Colors.white,
      body: input(),
    );
  }
}

推荐阅读