flutter - 带有条件 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,
),
),
);
}
}
解决方案
推荐阅读
- c++ - C++/WinRT:对 IObservableVector 进行排序
- javascript - 空值选择选项未在 laravel/voyager 中显示
- c# - 如何在 c# MVC 中从另一个选项卡暂停一个选项卡?
- r - R:为什么即使没有循环,if 语句也会被计算两次?
- reporting-services - 显示销售额首次高于 0 的日期
- c# - Visual Studio、WinForms 发出 CSharp
- javascript - mmenu 改变滑动的起始位置
- email - 是否应该为发送的每封邮件随机生成多部分电子邮件的边界值?
- c# - 如何串行而不是并行运行 Nunit 测试
- ruby-on-rails - nil:NilClass 的未定义方法“课程”