Flutter Widgets库异常:ParentDataWidget使用错误求助
Flutter Widgets库异常排查:Incorrect use of ParentDataWidget
开发AdminLoginScreen页面时触发Widgets库异常,提示Flexible组件被错误放置在Stack组件中,但已删除代码中的Flexible和Padding组件,问题仍未解决。
问题代码
class _AdminLoginScreenState extends State<AdminLoginScreen> { var _formkey = GlobalKey<FormState>(); var emailController = TextEditingController(); var passwordController = TextEditingController(); @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Color(0XFFE4C9E5), body: SafeArea( child: CustomScrollView( slivers: [ SliverFillRemaining( hasScrollBody: false, child: Form( key: _formkey, child: Stack( children: [ Positioned( left: -30, right: 275, top: -175, bottom: 100, child: Container( decoration: BoxDecoration( shape: BoxShape.circle, color: Color(0XFFFEB06A), ), )), Positioned( left: 275, right: -40, top: 100, bottom: -350, child: Container( decoration: BoxDecoration( shape: BoxShape.circle, color: Color(0XFF36D6E7), ), )), Positioned( left: 37, right: 37, top: 200, bottom: 100, child: Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(40), color: Colors.white, ), ), ), Column( // children: [ // Flexible( // child: Padding( // padding: const EdgeInsets.symmetric( // horizontal: 50, // ), // child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ SizedBox( height: 300, ), Text( "LogIn ", style: TextStyle( fontSize: 35, color: Colors.black26, fontWeight: FontWeight.bold, ), ), SizedBox( height: 20, ), Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(20), color: Color(0XFFD3D3D3), ), child: TextFormField( controller: emailController, decoration: InputDecoration( border: OutlineInputBorder( borderRadius: BorderRadius.circular(20), borderSide: BorderSide(color: Colors.black)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.black), borderRadius: BorderRadius.circular(20), ), labelText: 'Email', labelStyle: TextStyle( color: Color(0XFF4D4C4C), ), ), validator: (value) { if (value == null || value.isEmpty || !RegExp(r'sidra@gmail.com') // r'^[\w-\,]+@([\w-]+\.)+[\w]{2,4}' .hasMatch(value!)) { return 'Please enter correct email'; } else { return null; } }, ), ), SizedBox( height: 20, ), Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(20), color: Color(0XFFD3D3D3), ), child: TextFormField( controller: passwordController, decoration: InputDecoration( border: OutlineInputBorder( borderRadius: BorderRadius.circular(20), borderSide: BorderSide(color: Colors.black)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.black), borderRadius: BorderRadius.circular(20), ), labelText: 'Password', labelStyle: TextStyle( color: Color(0XFF4D4C4C), ), ), validator: (value) { if (value!.isEmpty || !RegExp(r'^[a-z A-Z 0-9]+$') .hasMatch(value!)) { return 'Please enter correct password'; } else { return null; } }), ), SizedBox( height: 20, ), Center( child: Container( width: 300, height: 50, decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), color: Color(0XFFFEB06A), ), child: TextButton( onPressed: () { if (_formkey.currentState!.validate()) { Navigator.pushNamed(context, '/register'); /* LoginCubit.get(context) .userLogin( email: emailController.text, password: passwordController.text, );*/ /*BlocProvider.of<LoginCubit>( context) .userLogin( email: emailController.text, password: password.text);*/ } }, child: Text( 'LogIn', style: TextStyle( fontSize: 15, fontWeight: FontWeight.bold, color: Colors.white, ), ), ), ), ), Center( child: TextButton( onPressed: () { Navigator.push( context, MaterialPageRoute( builder: ((context) => ForgotPass()))); }, child: Text( 'Forgot Your Password?', style: TextStyle( fontSize: 10, color: Colors.black, ), ), ), ), SizedBox( height: 150, ), ], ), // ), // ), ], ), // ], ), ), // ), ], ), ), ); } }
异常信息
Exception caught by widgets library ═══════════════════════════════════ The following assertion was thrown while applying parent data.: Incorrect use of ParentDataWidget. The ParentDataWidget Flexible(flex: 1) wants to apply ParentData of type FlexParentData to a RenderObject, which has been set up to accept ParentData of incompatible type StackParentData. Usually, this means that the Flexible widget has the wrong ancestor RenderObjectWidget. Typically, Flexible widgets are placed directly inside Flex widgets. The offending Flexible is currently placed inside a Stack widget. The ownership chain for the RenderObject that received the incompatible parent data was: Padding ← Flexible ← Stack ← _FormScope ← WillPopScope ← Form-[LabeledGlobalKey<FormState>#ac9e2] ← _SliverFillRemainingWithoutScrollable ← SliverFillRemaining ← Viewport ← IgnorePointer-[GlobalKey#93db6] ← ⋯ When the exception was thrown, this was the stack
排查与解决方法
清除热重载缓存:
仅注释掉组件不会彻底清除缓存,热重载可能残留旧布局结构。执行冷重启(点击IDE的重启按钮,而非热重载按钮),彻底刷新应用状态。修复Stack子组件约束:
当前Column直接作为Stack子组件,无明确位置约束,易引发布局冲突。给Column包裹Positioned来限定位置,示例:Positioned( left: 37, right: 37, top: 300, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // 移除原Column内的SizedBox(height:300),直接放置子组件 ], ), )检查关联页面:
确认跳转目标页面ForgotPass中是否存在Flexible错误放置在Stack中的情况,避免页面切换时触发同类异常。
内容的提问来源于stack exchange,提问作者Sidra Shibani
相关产品推荐
相关产品推荐

