当键盘出现在Flutter中时如何避免RenderFlow错误

ggazkfy8  于 2022-11-25  发布在  Flutter
关注(0)|答案(2)|浏览(118)

我设计的屏幕有两个主要部分:
1.下方有TextField的转盘滑块
1.一种纽扣
我希望按钮位于屏幕底部。

为此,我将1.的列 Package 在一个Flexible小部件中,并将Column的mainAxisSize设置为max,将包含按钮的Column的mainAxis大小设置为min。
现在,当我单击TextField时,会出现键盘,并收到Bottom Overflowed错误,其中Button出现在TextField的顶部。

我如何确保当键盘出现时,Button停留在屏幕的底部?我尝试过将两个Column Package 在另一个Column中,而该Column又被SingleChildScrollView小部件 Package ,但这显然覆盖了MainAxisSize.max属性,并将下面的Column(包含Button)呈现在上面的Column的下面,如下所示。

我的代码

class SigninScreen extends StatefulWidget {
  const SigninScreen({super.key});

  @override
  State<SigninScreen> createState() => _SigninScreenState();
}

class _SigninScreenState extends State<SigninScreen> {

  int _currentCarouselIndex = 0;

  List<Widget> indicators(imagesLength, currentIndex) {
    return List<Widget>.generate(imagesLength, (index) {
      return Container(
        margin: EdgeInsets.symmetric(
          vertical: 1.h,
          horizontal: 2,
        ),
        width: currentIndex == index ? 15 : 10,
        height: 3,
        decoration: BoxDecoration(
          color: currentIndex == index ? primary : grey,
          borderRadius: const BorderRadius.all(
            Radius.circular(2),
          ),
        ),
      );
    });
  }

  TextEditingController phoneController = TextEditingController();

  @override
  void dispose() {
    phoneController.dispose(); 
    super.dispose();
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: GestureDetector(
          onTap: (){ FocusManager.instance.primaryFocus?.unfocus();},
          behavior: HitTestBehavior.opaque,
          child: SingleChildScrollView(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Flexible(
                  fit: FlexFit.loose,
                  child: Column(
                    mainAxisSize: MainAxisSize.max,
                    children: [
                      CarouselSlider(
                        items: carouselImageList.map<Widget>((i){
                          return Builder(
                            builder: (context){
                              return Container(
                                width: 100.w,
                                height: 83.w,
                                decoration: BoxDecoration(
                                  image: DecorationImage(image: AssetImage(i), fit: BoxFit.fill),
                                ),
                              );
                            }
                          );
                        }).toList(), 
                        options: CarouselOptions(
                          height: 83.w,
                          aspectRatio: 1/0.83,
                          autoPlay: true,
                          autoPlayInterval: const Duration(seconds: 3),
                          initialPage: 0,
                          viewportFraction: 1,
                          onPageChanged: (index, timed) {
                            setState(() {
                              _currentCarouselIndex = index;
                            });
                          }
                        ),
                      ),
                      SizedBox(height: 1.h,),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: indicators(
                            carouselImageList.length, _currentCarouselIndex),
                      ),
                      SizedBox(height: 2.h,),
                      Padding(
                        padding: EdgeInsets.symmetric(horizontal: 5.w),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              children: [
                                Text(isEnglish ? "Enter your phone number" : "अपना फ़ोन नंबर दर्ज करें", style: globalTextStyle.copyWith(fontSize: 5.w, fontWeight: FontWeight.bold),),
                              ],
                            ),
                            SizedBox(height: 1.h),
                            PhoneNumberField(phoneController: phoneController),
                            SizedBox(height: 1.h),
                            Text(isEnglish ? "OTP will be sent on this number." : "इस नंबर पर ओटीपी भेजा जाएगा।", style: globalTextStyle.copyWith(fontSize: 3.w,),),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    CustomButton(width: 90.w, height: 15.w, color: primary, onTap: (){
                      // Navigator.pushNamed(context, otp);
                      (phoneController.text.length == 10) ?
                        Navigator.push(context, MaterialPageRoute(builder: (context) => 
                        OTPScreen(phoneNumber: "+91${phoneController.text}"))) 
                        : ShowSnackbar.showSnackBar(context, isEnglish ? "Enter a valid 10 digit phone number." : "एक मान्य 10 अंकों का फ़ोन नंबर दर्ज करें।");
                    }, text: isEnglish ? "Get OTP" : "ओटीपी प्राप्त करें", fontColor: white, borderColor: primary,),
                    SizedBox(height: 1.h,),
                    Text(isEnglish ? "By signing up, you agree to our Terms and Services" : "साइन अप करके, आप हमारी शर्तों से सहमत होते हैं और सेवाएं", style: globalTextStyle.copyWith(fontSize: 2.5.w,),),
                    SizedBox(height: 2.h,),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

如何使按钮即使在键盘出现时也停留在屏幕底部?

z31licg0

z31licg01#

删除Flexible并尝试以下代码。

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: GestureDetector(
          onTap: () {
            FocusManager.instance.primaryFocus?.unfocus();
          },
          behavior: HitTestBehavior.opaque,
          child: SingleChildScrollView(
            child: SizedBox(
              height: MediaQuery.of(context).size.height,
              width: MediaQuery.of(context).size.width,
              child: Column(
                children: [
                  CarouselSlider(
                    items: carouselImageList.map<Widget>((i) {
                      return Builder(builder: (context) {
                        return Container(
                          width: 100.w,
                          height: 100.w,
                          decoration: BoxDecoration(
                            image: DecorationImage(
                                image: AssetImage(i), fit: BoxFit.fill),
                          ),
                        );
                      });
                    }).toList(),
                    options: CarouselOptions(
                        height: 83.w,
                        aspectRatio: 1 / 0.83,
                        autoPlay: true,
                        autoPlayInterval: const Duration(seconds: 3),
                        initialPage: 0,
                        viewportFraction: 1,
                        onPageChanged: (index, timed) {
                          setState(() {
                            _currentCarouselIndex = index;
                          });
                        }),
                  ),
                  SizedBox(
                    height: 1.h,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: indicators(
                        carouselImageList.length, _currentCarouselIndex),
                  ),
                  SizedBox(
                    height: 2.h,
                  ),
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 5.w),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            Text(
                              "Enter your phone number",
                              style: TextStyle(
                                  fontSize: 5.w, fontWeight: FontWeight.bold),
                            ),
                          ],
                        ),
                        SizedBox(height: 1.h),
                        TextFormField(
                          controller: phoneController,
                          keyboardType: TextInputType.phone,
                        ),
                        SizedBox(height: 1.h),
                        Text(
                          "OTP will be sent on this number.",
                          style: TextStyle(
                            fontSize: 3.w,
                          ),
                        ),
                      ],
                    ),
                  ),
                  Expanded(
                    child: Container(),
                  ),
                  Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      CustomButton(
                        width: 90.w,
                        height: 15.w,
                        color: primary!,
                        onTap: () {},
                        text: "Get OTP",
                        fontColor: white!,
                        borderColor: primary!,
                      ),
                      SizedBox(
                        height: 1.h,
                      ),
                      Text(
                        "By signing up, you agree to our Terms and Services",
                        style: TextStyle(
                          fontSize: 2.5.w,
                        ),
                      ),
                      SizedBox(
                        height: 2.h,
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
rmbxnbpk

rmbxnbpk2#

使用SingleChildScrollView小部件 Package Scaffold,并将滚动方向设置为scrollDirection: Axis.vertical ,

相关问题