How To Set A Radio Button Checked By Default Using Flutter

admin_img Posted By Bajarangi soft , Posted On 27-11-2020

Want to set a radio button checked by default in flutter? Use the below example code to set a radio button checked by default in flutter.

How To Set A Radio Button Checked By Default Using Flutter

Set A Radio Button Checked By Default
Complete Code For Set A Radio Button Checked By Default In Flutter
main.dart

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Downloading...',
      theme: ThemeData(
        primarySwatch: Colors.blue,

        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Downloading...'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  int _currValue = 1;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.green,
        title: Text("Default Radio Button"),
      ),
      body: Center(
        child: Radio(
          activeColor: Colors.green,
          groupValue: _currValue,
          onChanged: (int i) => setState(() => _currValue = i),
          value: _currValue,
        ),
      ),
    );
  }
}

Related Post