Flutter添加TextFormField时位置参数相关报错如何解决
错误原因
Padding组件仅支持接收1个child参数,你的代码中在Text组件后直接将SizedBox作为第二个参数传入Padding,违反了参数传递规则,直接触发「位置参数必须在命名参数之前」「位置参数过多」报错- 笔误问题:Flutter内置组件名是大驼峰写法,你写的小写
sizedBox是错误的,正确写法为SizedBox
修复方案
将Padding的child替换为Column组件,把文本、间隔、TextFormField按顺序放入Column的子组件列表即可,修改后的完整代码如下:
import 'package:flutter/material.dart'; class ForgotPassword extends StatefulWidget { const ForgotPassword({Key? key}) : super(key: key); @override _ForgotPasswordState createState() => _ForgotPasswordState(); } class _ForgotPasswordState extends State<ForgotPassword> { // 用来控制输入框内容的控制器 final TextEditingController _emailController = TextEditingController(); @override void dispose() { // 页面销毁时释放控制器,避免内存泄漏 _emailController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( leading: IconButton(onPressed: () {}, icon: const Icon(Icons.exit_to_app)), ), body: SingleChildScrollView( child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[ Padding( padding: const EdgeInsets.symmetric( vertical: 35.0, ), child: const Text( 'Forgot Password', style: TextStyle( color: Colors.blue, fontWeight: FontWeight.bold, fontSize: 20.0), ), ), Padding( padding: const EdgeInsets.fromLTRB(0, 12, 0, 0), child: Container( child: Image.asset('assets/forgot password 1.png'), ), ), Container( height: 440.0, color: Colors.transparent, child: Container( decoration: BoxDecoration( color: Colors.blue, borderRadius: const BorderRadius.only( topLeft: Radius.circular(40.0), topRight: Radius.circular(40.0), )), child: Align( alignment: Alignment.topCenter, child: Padding( padding: const EdgeInsets.symmetric(vertical: 40, horizontal: 24), // 这里替换为Column承载多个子组件 child: Column( mainAxisSize: MainAxisSize.min, children: [ const Text( 'Masukkan Email', style: TextStyle( color: Colors.white, fontSize: 14, fontWeight: FontWeight.bold), ), const SizedBox(height: 20,), // 新增的TextFormField,已适配蓝色背景的样式 TextFormField( controller: _emailController, keyboardType: TextInputType.emailAddress, style: const TextStyle(color: Colors.black87), decoration: InputDecoration( filled: true, fillColor: Colors.white, hintText: 'Contoh: email@gmail.com', contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14), border: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: BorderSide.none ) ), ) ], ), ), ), ), ), ], ), ), ), ); } }
内容的提问来源于stack exchange,提问作者Imam Rizky GANTENG
相关产品推荐
相关产品推荐

