Flutter实现页面底部固定Material Button的解决方案咨询
你当前按钮无法固定的原因是它被放在了SingleChildScrollView内部,会跟随表单内容滚动,只需调整布局结构,分离可滚动区域和固定按钮区域即可,修改后完整代码如下:
import 'dart:ui'; import 'package:crmapp/widgets/components/alert.dart'; import 'package:crmapp/widgets/theme/constants.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; class SignUpScreen extends StatefulWidget { @override _SignUpScreenState createState() => _SignUpScreenState(); } class _SignUpScreenState extends State< SignUpScreen > { // 表单校验控制器 TextEditingController fNameController = TextEditingController(); TextEditingController lNameController = TextEditingController(); TextEditingController nickNameController = TextEditingController(); TextEditingController phoneController = TextEditingController(); bool _isButtonEnabled = false; bool isConfirm=false; check (BuildContext context){ if(fNameController.text.isNotEmpty && lNameController.text.isNotEmpty && nickNameController.text.isNotEmpty && phoneController.text.isNotEmpty){ setState(() { _isButtonEnabled = true; }); } else { setState(() { _isButtonEnabled = false; }); } } final GlobalKey<FormState> formKey = GlobalKey<FormState>(); @override Widget build(BuildContext context) { return Scaffold( // 键盘弹出时按钮自动上移,避免遮挡输入框 resizeToAvoidBottomInset: true, body: SafeArea( child: Stack( fit: StackFit.expand, children: [ _signUp(), ], ), ), ); } Widget _signUp() { return Container( constraints: BoxConstraints.expand( height: MediaQuery.of(context).size.height, width: MediaQuery.of(context).size.width, ), decoration: BoxDecoration( gradient: LinearGradient( colors: const [ Color(0xFF2A3476), Color(0xFF2A3476), ], begin: Alignment.topLeft, end: Alignment.centerRight, ), ), // 根布局改为Column,垂直排列表单+固定按钮 child: Column( children: [ // Expanded包裹滚动区域,占满除底部按钮外的所有空间 Expanded( child: Form( key: formKey, child: SingleChildScrollView( child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: const EdgeInsets.symmetric(vertical: 36.0, horizontal: 24.0), child: Column( mainAxisAlignment: MainAxisAlignment.end, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( "Create Account", style: TextStyle( color: Colors.white, fontSize: 34.0,fontFamily: "medium", fontWeight: FontWeight.w800, ), ), ], ), ), Container( width: double.infinity, decoration: const BoxDecoration( color: Colors.white, borderRadius: BorderRadius.only( topLeft: Radius.circular(30), topRight: Radius.circular(30), ), ), child: Padding( padding: const EdgeInsets.all(24.0), child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( "Hello, sign up to", style: TextStyle( fontSize: 29, fontFamily: "regular", fontWeight: FontWeight.w300, color: Colors.black, ), ), Text( "continue", style: TextStyle( fontSize: 29, fontFamily: "regular", fontWeight: FontWeight.w300, color: Colors.black, ), ), const SizedBox( height: 20.0, ), const Text( 'First Name', style: TextStyle( fontSize: 15, fontFamily: "regular",), ), const SizedBox( height: 12.0, ), TextFormField( controller: fNameController, onChanged: (val){ check(context); }, decoration: InputDecoration( contentPadding: const EdgeInsets.all(8), border: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: BorderSide.none, ), filled: true, fillColor: const Color(0xFFE1E8F7), hintText: "", ), ), const SizedBox( height: 20.0, ), const Text( 'Last Name', style: TextStyle( fontSize: 15, fontFamily: "regular", ), ), const SizedBox( height: 12.0, ), TextField( controller: lNameController, onChanged: (val){ check(context); }, decoration: InputDecoration( contentPadding: const EdgeInsets.all(8), border: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: BorderSide.none, ), filled: true, fillColor: const Color(0xFFE1E8F7), hintText: "", ), ), const SizedBox( height: 20.0, ), const Text( 'Nick Name', style: TextStyle( fontSize: 15, fontFamily: "regular",), ), const SizedBox( height: 12.0, ), TextField( controller: nickNameController, onChanged: (val){ check(context); }, decoration: InputDecoration( contentPadding: const EdgeInsets.all(8), border: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: BorderSide.none, ), filled: true, fillColor: const Color(0xFFE1E8F7), hintText: "", ), ), const SizedBox( height: 20.0, ), const Text( 'Mobile Number', style: TextStyle( fontSize: 15, fontFamily: "regular",), ), const SizedBox( height: 12.0, ), TextFormField( controller: phoneController, onChanged: (val){ check(context); }, maxLength: 10, keyboardType: TextInputType.phone, decoration: InputDecoration( contentPadding: const EdgeInsets.all(8), border: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: BorderSide.none, ), filled: true, fillColor: const Color(0xFFE1E8F7), hintText: "077xxxxxxx", ), ), const SizedBox( height: 20.0, ), Row( mainAxisAlignment: MainAxisAlignment.center, children: [ RichText( textAlign: TextAlign.center, text: TextSpan(children: <TextSpan>[ TextSpan( text: "Already have an account?", style: TextStyle(color: m_titleColor,fontWeight: FontWeight.normal, fontFamily: "regular")), TextSpan( text: " Sign in", style: const TextStyle( color: Color(0xFF2A3476), fontWeight: FontWeight.w600, fontFamily: "medium")), ]), ) ], ), const SizedBox( height:20.0, ), ], ), ), ), ], ), ), ), ), // 按钮放在Column底部,固定不跟随滚动 Container( color: Colors.white, padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 16.0), child: Row( children: [ Expanded( child: MaterialButton( height: 44, onPressed: _isButtonEnabled ? () { FocusScope.of(context).requestFocus(FocusNode()); // 后续表单提交逻辑可写在这里 } : null, shape: RoundedRectangleBorder(borderRadius: const BorderRadius.all(Radius.circular(8.0))), color: _isButtonEnabled ? const Color(0xFF2A3476) : const Color(0x201E1E99), elevation: 0, highlightElevation: 0, child: Text( "Next", style: TextStyle(color: m_fillColor,fontSize: 18,fontWeight: FontWeight.w600 , fontFamily: "regular",), ), ), ), ], ), ) ], ), ); } }
核心修改说明
- 原布局中将按钮放在滚动视图内部,现在将按钮移到滚动视图外部,作为Column的底部子组件,天然具备固定效果
- 用
Expanded包裹滚动区域,自动适配剩余屏幕高度,不会出现内容溢出问题 - 按钮区域增加了白色背景和统一内边距,和上方表单的白色背景衔接更自然
- 补充了按钮不可点击状态下
onPressed设为null的逻辑,符合交互规范
内容的提问来源于stack exchange,提问作者core114
相关产品推荐
相关产品推荐

