4

在我的第一个Flutter 项目中,我试图从Alert Dialog中的TextFormField获取价值。我正在尝试验证输入并基于该验证我想启用/禁用AlertDialog按钮。

我为显示对话框所做的工作:

 _displayDialog(BuildContext context) async {
    return showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text('TextField AlertDemo'),
            content: _container(),
            actions: <Widget>[
              RaisedButton(
                onPressed: isValid
                    ? () {
                        print("ISVALID:");
                      }
                    : null,
                child: Text("Click Me"),
              )
            ],
          );
        });
  }

_container() 方法代码:

 Widget _container() {
    return Container(
      margin: EdgeInsets.all(25),
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextFormField(
              onChanged: (text) {
                setState(() {
                  if (text.length > 5) {
                    isValid = true;
                  } else {
                    isValid = false;
                  }
                });
              },
              decoration: InputDecoration(labelText: 'Enter Text'),
            ),
            SizedBox(height: 15),
            RaisedButton(
              onPressed: isValid
                  ? () {
                      print("ISVALID:");
                    }
                  : null,
              child: Text("Done!"),
            )
          ],
        ),
      ),
    );
  }

不幸的是,它正在脚手架的主体中工作:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          _container(),
          RaisedButton(
            onPressed: (){_displayDialog(context);},
            child: Text("Show Alert"),
          )
        ],
      ),
    );
  }

谁能帮我解决这个问题。TIA

4

1 回答 1

5

复制粘贴此代码,它工作正常,我使用Widget来StateFullBuilder更新state.AlertDialogBox

有关更新或刷新的更多参考,请DialogBox 单击此处

下面是Code

import 'dart:async';

import 'package:flutter/material.dart';

class MyTimer extends StatefulWidget {
  @override
  _MyTimerState createState() => _MyTimerState();
}

class _MyTimerState extends State<MyTimer> {
  
  bool isValidAlert = false;
  bool isValidScafold = false;

  _displayDialog(BuildContext context) async {
    return showDialog(
        context: context,
        builder: (context) {
          return StatefulBuilder(builder: (context, setState) {
            return AlertDialog(
              title: Text('TextField AlertDemo'),
              content: _containerAlert(setState),
              actions: <Widget>[
                RaisedButton(
                  onPressed: isValidAlert
                      ? () {
                          print("ISVALID:");
                        }
                      : null,
                  child: Text("Click Me"),
                )
              ],
            );
          });
        });
  }

  Widget _container() {
    return Container(
      margin: EdgeInsets.all(25),
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextFormField(
              onChanged: (text) {
                setState(() {
                  if (text.length > 5) {
                    isValidScafold = true;
                  } else {
                    isValidScafold = false;
                  }
                });
              },
              decoration: InputDecoration(labelText: 'Enter Text'),
            ),
            SizedBox(height: 15),
            RaisedButton(
              onPressed: isValidScafold
                  ? () {
                      print("ISVALID:");
                    }
                  : null,
              child: Text("Done!"),
            )
          ],
        ),
      ),
    );
  }

  Widget _containerAlert(StateSetter setState) {
    return Container(
      margin: EdgeInsets.all(25),
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextFormField(
              onChanged: (text) {
                setState(() {
                  if (text.length > 5) {
                    isValidAlert = true;
                  } else {
                    isValidAlert = false;
                  }
                  print(text);
                });
              },
              decoration: InputDecoration(labelText: 'Enter Text'),
            ),
            SizedBox(height: 15),
            RaisedButton(
              onPressed: isValidAlert
                  ? () {
                      print("ISVALID:");
                    }
                  : null,
              child: Text("Done!"),
            )
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          _container(),
          RaisedButton(
            onPressed: () {
              _displayDialog(context);
            },
            child: Text("Show Alert"),
          )
        ],
      ),
    );
  }
}
于 2020-12-31T10:48:09.143 回答