首页 > 解决方案 > 带有条件 Flutter 的 TextFormField

问题描述

我创建了 3 TextFormField。我想让第二个和第三个TextFormField禁用,直到用户输入第一个TextFormField,如果用户删除 first 的值,再次禁用TextFormField。我onchanged在 first 上创建属性TextFormField,该功能可以工作,但是当用户从 first 中删除该值时TextFormField,第二个和第三个TextFormField不会再次禁用自身。

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final PageController _pagecontroller = PageController();
  int _selectedindex = 0;
  final _formKey = GlobalKey<FormState>();
  bool _isEnable = false;

  void _ontap(int index) {
    setState(() {
      _selectedindex = index;
    });
    _pagecontroller.jumpToPage(index);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Padding(
          padding: EdgeInsets.all(24.0),
          child: PageView(
            controller: _pagecontroller,
            children: [
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text('Home'),
                  Padding(
                    padding: EdgeInsets.all(16.0),
                    child: Form(
                        key: _formKey,
                        child: Column(
                          children: [
                            TextFormField(
                              onChanged: (String _value) {
                                if (_value != null) {
                                  setState(() {
                                    _isEnable = true;
                                  });
                                } else {
                                  setState(() {
                                    _isEnable = false;
                                  });
                                }
                              },
                            ),
                            TextFormField(
                              enabled: _isEnable,
                            ),
                            TextFormField(
                              enabled: _isEnable,
                            )
                          ],
                        )),
                  ),
                  FlatButton(
                    onPressed: () {
                      setState(() {
                        _selectedindex++;
                      });
                      _pagecontroller.jumpToPage(_selectedindex);
                    },
                    child: Text('Profile'),
                    color: Colors.orange,
                  ),
                ],
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text('Profile'),
                ],
              ),
            ],
            onPageChanged: (page) {
              setState(() {
                _selectedindex = page;
              });
            },
          ),
        ),
        bottomNavigationBar: BottomNavigationBar(
          items: [
            BottomNavigationBarItem(
              icon: Icon(Icons.home),
              title: Text('Home'),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.person),
              title: Text('Profile'),
            ),
          ],
          currentIndex: _selectedindex,
          onTap: _ontap,
        ),
      ),
    );
  }
}

标签: flutter

解决方案


第一次条件为假,因为valueisnull但是一旦你输入一些东西,value就会有一个值,当你删除你输入的值时,value仍然会有一个空字符串的值,就是 this''并且不会再继续为空,而不是像这样检查空检查''

  onChanged: (String value) {
                                if (value != '') {
                                  setState(() {
                                    _isEnable = true;
                                  });
                                } else {
                                  setState(() {
                                    _isEnable = false;
                                  });
                                }
                              },

另外我认为命名参数不是一个好习惯,_因为它们是本地的并且只属于该方法。 在此处输入图像描述


推荐阅读