Conversation

Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!

Join the discussion and share your insights now!

Comments 0

Sharpen your coding skills—try JavaScript challenges on TOOLX now!

advertisement

RadioButton Widget in Flutter

radio button in flutter

This button is commonly used when choosing a single option from a list of options. Options such as Male/Female/Other, True/False, Single/Married/Widow, and so on can be given and one value can be chosen from this list using the RadioButton to choose only one option. This behavior cannot be changed.


import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _selectedValue = 1;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('RadioButton Example'),
        ),
        body: Column(
          children: [
            ListTile(
              title: Text('Option 1'),
              leading: Radio<int>(
                value: 1,
                groupValue: _selectedValue,
                onChanged: (int? value) {
                  setState(() {
                    _selectedValue = value!;
                  });
                },
              ),
              trailing: Icon(Icons.male),
            ),
            ListTile(
              title: Text('Option 2'),
              leading: Radio<int>(
                value: 2,
                groupValue: _selectedValue,
                onChanged: (int? value) {
                  setState(() {
                    _selectedValue = value!;
                  });
                },
              ),
            ),
            ListTile(
              title: Text('Option 3'),
              leading: Radio<int>(
                value: 3,
                groupValue: _selectedValue,
                onChanged: (int? value) {
                  setState(() {
                    _selectedValue = value!;
                  });
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}



RadioButton Widget in Flutter - User Input Widgets - Flutter Tutorial




flutter widgets Radio button in flutter radio button widget in flutter flutter radio button flutter select options

advertisement