如何启用按钮

How to enable buttons

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

@override
Widget build(BuildContext context) {
    bool btnenabled = false;
    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)
          ],
        ),
    );
  }
}

您可以复制粘贴运行下面的完整代码
您可以将 bool btnenabled = false; 移出 Widget build
变化自

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

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");
  }
}