首页 > 解决方案 > 如何在颤动中显示文本字段可见性单击警报对话框按钮

问题描述

我想要当我单击警报对话框的确定​​按钮时,然后显示文本字段可见性(此文本字段在“打开对话框”按钮下方给出,并且此文本字段可见性默认为隐藏)。但它没有发生我不明白这里的错误是什么。

我的代码:

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
void main()=> runApp(MyHomePages());
class MyHomePages  extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: "KYC Formm",
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: MyHomePageDesign(title: "Kyc form"),
    );
  }
}
class MyHomePageDesign extends StatefulWidget {
  MyHomePageDesign({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePageDesign> {
  bool _isVisible = false ;
  bool  _isRcVisible = false;
  String textRcValue;
  TextEditingController _textFieldControllerD = TextEditingController();
  TextEditingController _textRcFieldController = TextEditingController();
  void _displayDialog(BuildContext context) async {
    return showDialog(
        barrierDismissible: true,
        context: context,
        builder: (context) {
          // this was required, rest is same
          return StatefulBuilder(
              builder: (BuildContext context, setState){
                return AlertDialog(
                    title: Text('Choose any one'),
                    content: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: <Widget>[
                          Align(
                            alignment: Alignment.centerLeft,
                            child: Text("Capture Photo",
                              style: TextStyle(
                                fontSize: 20.0,),),
                          ),
                          SizedBox(height: 10.0,),
                          Align(
                            alignment: Alignment.centerLeft,
                            child: Text("Pick Photo",
                              style: TextStyle(
                                fontSize: 20.0,),),
                          ),
                          SizedBox(height: 10.0,),
                          Align(
                            alignment: Alignment.centerLeft,
                            child: InkWell(
                              onTap: (){
                                setState(() => _isVisible = !_isVisible);
                              },
                              child: Text("Enter RC Number",
                                style: TextStyle(
                                  fontSize: 20.0,),),
                            ),
                          ),
                          SizedBox(height: 10.0,),
                          Visibility(
                            visible: _isVisible,
                            child: Align(
                              alignment: Alignment.centerLeft,
                              child: Container(
                                width: 200.0,
                                height: 60.0,
                                padding: const EdgeInsets.only(top: 10.0, left: 0.0,bottom: 10.0,right: 0.0),
                                child: TextField(
                                  controller: _textFieldControllerD,
                                  decoration: InputDecoration(
                                    border: OutlineInputBorder(),
                                    labelText: 'Enter RC No',
                                  ),
                                  style: TextStyle(
                                    color: Colors.red,
                                    fontSize: 18.0,),),
                              ),
                            ),
                          ),
                          Align(
                            alignment: Alignment.centerRight,
                            child: new FlatButton(
                              child: new Text('OK',
                                style: TextStyle(
                                    color: Colors.teal
                                ),),
                              onPressed: () {
                                textRcValue= _textFieldControllerD.text;
                                setState(() {
                                  _isRcVisible = true;
                                });
                                _textRcFieldController.text=  textRcValue;
                                print("RcValue:");
                                print(textRcValue);
                                Navigator.of(context).pop();
                              },
                            ),
                          )
                        ]
                    )
                );
              }
          );
        });
   }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Demo'),
      ),
      body: Center(
        child:Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            Container(
              width: 160.0,
              height: 50.0,
              padding: const EdgeInsets.only(top: 0.0, left: 5.0,bottom: 10.0,right: 0.0),
              child: RaisedButton(
                color: Colors.green,
                child: Text('Open Dialog'),
                onPressed: () {
                  _displayDialog(context);
                },
              ),

            ),
            Visibility(
              visible: _isRcVisible,
              child: TextField(
                controller: _textRcFieldController,
                decoration: InputDecoration(hintText: "Enter Rc Number"),
                onTap: (){},
              ),
            ),
          ],
        ),
      ),
    );
  }
}

标签: flutter

解决方案


您需要更改setState您在对话框中调用的方法,以使您的主小部件isRcVisible中的函数调用成为真实的。State

_setRcVisible() { // this is new
    this.setState(() {
      _isRcVisible = true;
    });
  }

  void _displayDialog(BuildContext context) async {
    return showDialog(
        barrierDismissible: true,
        context: context,
        builder: (context) {
          // this was required, rest is same
          return StatefulBuilder(
              builder: (BuildContext context, setState){
                return AlertDialog(
                    title: Text('Choose any one'),
                    content: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: <Widget>[
                          Align(
                            alignment: Alignment.centerLeft,
                            child: Text("Capture Photo",
                              style: TextStyle(
                                fontSize: 20.0,),),
                          ),
                          SizedBox(height: 10.0,),
                          Align(
                            alignment: Alignment.centerLeft,
                            child: Text("Pick Photo",
                              style: TextStyle(
                                fontSize: 20.0,),),
                          ),
                          SizedBox(height: 10.0,),
                          Align(
                            alignment: Alignment.centerLeft,
                            child: InkWell(
                              onTap: (){
                                setState(() => _isVisible = !_isVisible);
                              },
                              child: Text("Enter RC Number",
                                style: TextStyle(
                                  fontSize: 20.0,),),
                            ),
                          ),
                          SizedBox(height: 10.0,),
                          Visibility(
                            visible: _isVisible,
                            child: Align(
                              alignment: Alignment.centerLeft,
                              child: Container(
                                width: 200.0,
                                height: 60.0,
                                padding: const EdgeInsets.only(top: 10.0, left: 0.0,bottom: 10.0,right: 0.0),
                                child: TextField(
                                  controller: _textFieldControllerD,
                                  decoration: InputDecoration(
                                    border: OutlineInputBorder(),
                                    labelText: 'Enter RC No',
                                  ),
                                  style: TextStyle(
                                    color: Colors.red,
                                    fontSize: 18.0,),),
                              ),
                            ),
                          ),
                          Align(
                            alignment: Alignment.centerRight,
                            child: new FlatButton(
                              child: new Text('OK',
                                style: TextStyle(
                                    color: Colors.teal
                                ),),
                              onPressed: () {
                                textRcValue= _textFieldControllerD.text;
                                _setRcVisible(); // this is new
                                _textRcFieldController.text=  textRcValue;
                                print("RcValue:");
                                print(textRcValue);
                                Navigator.of(context).pop();
                              },
                            ),
                          )
                        ]
                    )
                );
              }
          );
        });
  }

推荐阅读