Flutter 如何启用按钮

Flutter 如何启用按钮,flutter,button,disable,Flutter,Button,Disable,在我的一个颤振应用程序页面中,我想首先禁用next按钮。如果用户在文本字段中输入任何新值,我想启用next按钮。这是我的代码,但无论发生什么情况,next按钮都将保持禁用状态。当用户输入值时,如何启用next按钮 @覆盖 小部件构建(构建上下文){ bool btnenabled=错误; 返回脚手架( appBar:appBar( 背景颜色:Colors.red, 标题:文本(“地址”), ), 正文:专栏( 儿童:[ ..... TextFormField( 一旦更改:(newValue){

在我的一个颤振应用程序页面中,我想首先禁用
next
按钮。如果用户在文本字段中输入任何新值,我想启用
next
按钮。这是我的代码,但无论发生什么情况,
next
按钮都将保持禁用状态。当用户输入值时,如何启用
next
按钮

@覆盖
小部件构建(构建上下文){
bool btnenabled=错误;
返回脚手架(
appBar:appBar(
背景颜色:Colors.red,
标题:文本(“地址”),
),
正文:专栏(
儿童:[
.....
TextFormField(
一旦更改:(newValue){
_currentaddress='$addressJSON'+newValue;
如果(newValue.length>0){
设置状态(){
btnenabled=真;
});
}
}),
文本按钮(
样式:TextButton.styleFrom(
背景色:颜色。蓝色[700],原色:颜色。白色),
子项:文本('next',
),
onPressed:btnenabled==true
?()=>Navigator.push(上下文,
MaterialPage路由(生成器:(上下文)=>Summary())
:null)
],
),
);
}
}

您可以复制粘贴运行下面的完整代码
您可以移动
boolbtnenabled=false超出
小部件构建

Widget build(BuildContext context) {
    bool btnenabled = false;

工作演示

完整代码

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool btnenabled = false;
  String _currentaddress = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.red,
        title: Text('Address'),
      ),
      body: Column(
        children: <Widget>[
          TextFormField(onChanged: (newValue) {
            _currentaddress = 'addressJSON' + newValue;
            if (newValue.length > 0) {
              setState(() {
                btnenabled = true;
              });
            }
          }),
          TextButton(
              style: TextButton.styleFrom(
                  backgroundColor: Colors.blue[700], primary: Colors.white),
              child: Text(
                'next',
              ),
              onPressed: btnenabled == true
                  ? () => Navigator.push(context,
                      MaterialPageRoute(builder: (context) => Summary()))
                  : null)
        ],
      ),
    );
  }
}

class Summary extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text("Summary");
  }
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
bool btnenabled=错误;
字符串_currentaddress=“”;
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
背景颜色:Colors.red,
标题:文本(“地址”),
),
正文:专栏(
儿童:[
TextFormField(onChanged:(newValue){
_currentaddress='addressJSON'+newValue;
如果(newValue.length>0){
设置状态(){
btnenabled=真;
});
}
}),
文本按钮(
样式:TextButton.styleFrom(
背景色:颜色。蓝色[700],原色:颜色。白色),
子:文本(
“下一个”,
),
onPressed:btnenabled==true
?()=>Navigator.push(上下文,
MaterialPage路由(生成器:(上下文)=>Summary())
:null)
],
),
);
}
}
类摘要扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回文本(“摘要”);
}
}
bool btnenabled = false;
...
Widget build(BuildContext context) {
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool btnenabled = false;
  String _currentaddress = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.red,
        title: Text('Address'),
      ),
      body: Column(
        children: <Widget>[
          TextFormField(onChanged: (newValue) {
            _currentaddress = 'addressJSON' + newValue;
            if (newValue.length > 0) {
              setState(() {
                btnenabled = true;
              });
            }
          }),
          TextButton(
              style: TextButton.styleFrom(
                  backgroundColor: Colors.blue[700], primary: Colors.white),
              child: Text(
                'next',
              ),
              onPressed: btnenabled == true
                  ? () => Navigator.push(context,
                      MaterialPageRoute(builder: (context) => Summary()))
                  : null)
        ],
      ),
    );
  }
}

class Summary extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text("Summary");
  }
}