0

I was looking for Android Visibility.GONE Equivalent option.

Show/hide widgets in Flutter programmatically

Above link approved solution will take space after setting Opacity to 0. For not taking any space as in Android That solution says

To make it not occupy space, replace it with an empty Container().

Someone please tell me how to achieve this. I need to make a Widget completely erase without taking any space programatically.

Note : I need similarly performing solution like Android native code view.setVisibility(View.GONE)

3 Answers3

4

Try Offstage:

Offstage(offstage: isHide, child:Text('test'),);
Jim
  • 6,928
  • 1
  • 7
  • 18
1

Do this in your statefullwidget with conditional display: See the below example:

class _MyHomePageState extends State<MyHomePage> {
  bool _isShowing = true;

  void _toggle() {
    setState(() {
      _isShowing =!_isShowing ;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Visibility Page"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(color: Colors.red, height: 50),
            if(_isShowing )
            Container(color: Colors.green, height: 50),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _toggle,
        tooltip: 'Toggle',
        child: Icon(Icons.add),
      ),
    );
  }
}
Jhakiz
  • 1,519
  • 9
  • 16
1

A stateful widget is one of the ways of doing this.

So, once you press the button, it triggers the _toggle() function. That function switches the boolean to false.

The SetState(){} function calls for a rebuild.

When isShowing is false, it will then show container...

Example below:

import 'package:flutter/material.dart';

class HomePageScreen extends StatefulWidget {
  @override
  _HomePageScreenState createState() => _HomePageScreenState();
}

class _HomePageScreenState extends State<HomePageScreen> {
  bool _isShowing = true;

  void _toggle() {
    setState(() {
      _isShowing = !_isShowing;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Remove Text for Empty Container"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            _isShowing
                ? Text("Remove Text")
                : Container(),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _toggle,
        tooltip: 'Toggle',
        child: Icon(Icons.add),
      ),
    );
  }
}
JFB
  • 81
  • 5