Hide And Show Radio Button Using Flutter Android App

admin_img Posted By Bajarangi soft , Posted On 30-10-2020

In flutter there is a specific widget named as Visibility which is used hide any given child widget using Boolean true false values. We can easily maintain Boolean value using State. Sometimes app developer wants to hide ListView or any other components like Text, Container, TextField etc on button click event.

Hide And Show Radio Button Using Flutter Android App

Hide And Show Radio Button 
Complete Code For Hide And Show Radio Button In Flutter
main.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main(){
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp
    ]);
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Animation Playground',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: ShowHideRadio(),
    );
  }
}

class ShowHideRadio extends StatefulWidget {
  @override
  _ShowHideRadioState createState() => _ShowHideRadioState();
}

class _ShowHideRadioState extends State<ShowHideRadio> {
  bool _isRadioChecked = true;
  int _currValue = 1;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.cyan,
        title: Text("Hide & Show Radio Button"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          mainAxisSize: MainAxisSize.max,
          children: <Widget>[
            _isRadioChecked
                ? RadioListTile(
              activeColor: Colors.cyan,
              groupValue: _currValue,
              value: 1,
              onChanged: (val) => setState(() => _currValue = val),
              title: Text("Radio Button Text"),
            )
                : SizedBox(),
            SizedBox(height: 30.0),
            RaisedButton(
              color: Colors.cyan,
              child: Text(_isRadioChecked ? "Hide" : "Show",style: TextStyle(color: Colors.white),),
              onPressed: () {
                setState(() => _isRadioChecked = !_isRadioChecked);
              },
            ),
          ],
        ),
      ),
    );
  }
}

Related Post