Show/hide widgets in Flutter programmatically

前端 未结 13 1296
轮回少年
轮回少年 2020-11-28 04:13

In Android, every single View subclass has a setVisibility() method that allows you modify the visibility of a View object

The

13条回答
  •  孤城傲影
    2020-11-28 04:40

    To collaborate with the question and show an example of replacing it with an empty Container().

    Here's the example below:

    import "package:flutter/material.dart";
    
    void main() {
      runApp(new ControlleApp());
    }
    
    class ControlleApp extends StatelessWidget { 
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: "My App",
          home: new HomePage(),
        );
      }
    }
    
    class HomePage extends StatefulWidget {
      @override
      HomePageState createState() => new HomePageState();
    }
    
    class HomePageState extends State {
      bool visibilityTag = false;
      bool visibilityObs = false;
    
      void _changed(bool visibility, String field) {
        setState(() {
          if (field == "tag"){
            visibilityTag = visibility;
          }
          if (field == "obs"){
            visibilityObs = visibility;
          }
        });
      }
    
      @override
      Widget build(BuildContext context){
        return new Scaffold(
          appBar: new AppBar(backgroundColor: new Color(0xFF26C6DA)),
          body: new ListView(
            children: [
              new Container(
                margin: new EdgeInsets.all(20.0),
                child: new FlutterLogo(size: 100.0, colors: Colors.blue),
              ),
              new Container(
                margin: new EdgeInsets.only(left: 16.0, right: 16.0),
                child: new Column(
                  children: [
                    visibilityObs ? new Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        new Expanded(
                          flex: 11,
                          child: new TextField(
                            maxLines: 1,
                            style: Theme.of(context).textTheme.title,
                            decoration: new InputDecoration(
                              labelText: "Observation",
                              isDense: true
                            ),
                          ),
                        ),
                        new Expanded(
                          flex: 1,
                          child: new IconButton(
                            color: Colors.grey[400],
                            icon: const Icon(Icons.cancel, size: 22.0,),
                            onPressed: () {
                              _changed(false, "obs");
                            },
                          ),
                        ),
                      ],
                    ) : new Container(),
    
                    visibilityTag ? new Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        new Expanded(
                          flex: 11,
                          child: new TextField(
                            maxLines: 1,
                            style: Theme.of(context).textTheme.title,
                            decoration: new InputDecoration(
                              labelText: "Tags",
                              isDense: true
                            ),
                          ),
                        ),
                        new Expanded(
                          flex: 1,
                          child: new IconButton(
                            color: Colors.grey[400],
                            icon: const Icon(Icons.cancel, size: 22.0,),
                            onPressed: () {
                              _changed(false, "tag");
                            },
                          ),
                        ),
                      ],
                    ) : new Container(),
                  ],
                )
              ),
              new Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  new InkWell(
                    onTap: () {
                      visibilityObs ? null : _changed(true, "obs");
                    },
                    child: new Container(
                      margin: new EdgeInsets.only(top: 16.0),
                      child: new Column(
                        children: [
                          new Icon(Icons.comment, color: visibilityObs ? Colors.grey[400] : Colors.grey[600]),
                          new Container(
                            margin: const EdgeInsets.only(top: 8.0),
                            child: new Text(
                              "Observation",
                              style: new TextStyle(
                                fontSize: 12.0,
                                fontWeight: FontWeight.w400,
                                color: visibilityObs ? Colors.grey[400] : Colors.grey[600],
                              ),
                            ),
                          ),
                        ],
                      ),
                    )
                  ),
                  new SizedBox(width: 24.0),
                  new InkWell(
                    onTap: () {
                      visibilityTag ? null : _changed(true, "tag");
                    },
                    child: new Container(
                      margin: new EdgeInsets.only(top: 16.0),
                      child: new Column(
                        children: [
                          new Icon(Icons.local_offer, color: visibilityTag ? Colors.grey[400] : Colors.grey[600]),
                          new Container(
                            margin: const EdgeInsets.only(top: 8.0),
                            child: new Text(
                              "Tags",
                              style: new TextStyle(
                                fontSize: 12.0,
                                fontWeight: FontWeight.w400,
                                color: visibilityTag ? Colors.grey[400] : Colors.grey[600],
                              ),
                            ),
                          ),
                        ],
                      ),
                    )
                  ),
                ],
              )                    
            ],
          )
        );
      }
    }
    

提交回复
热议问题