【问题标题】:how to text field visibility show click on alert dialog button in flutter如何在颤动中显示文本字段可见性单击警报对话框按钮
【发布时间】:2020-11-22 08:42:00
【问题描述】:

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

我的代码:

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


    【解决方案1】:

    您需要更改您在对话框中调用的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();
                                  },
                                ),
                              )
                            ]
                        )
                    );
                  }
              );
            });
      }
    

    【讨论】:

    • 感谢您给我答复。这个答案对我有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多