Poperton
Poperton

Reputation: 2127

How to draw simple radio button?

I want to do this button on Flutter as an exercise:

enter image description here

Here's what I did already:

              Stack(
                  children: [
                    Align(
                        alignment: Alignment.center,
                        child: ClipOval(
                          child: Container(
                              color: Colors.grey,
                              width: 15,
                              height: 15),
                        )),
                    ClipOval(
                      child: Center(
                          child: Container(
                              color: widget.selected == i
                                  ? Color(0xFF3ACE00)
                                  : Colors.grey,
                              width: 11,
                              height: 11)),
                    ),
                  ],
                )

The 2 problems are:

I cannot make the green button to be on the center, even if I use Align

I cannot draw a border on the button just like in the image, ClipOval does not have this option

Upvotes: 0

Views: 139

Answers (1)

Ketan Ramteke
Ketan Ramteke

Reputation: 10651

You can use the Container widget instead.

enter image description here

Here is how:

class MyWidget extends StatelessWidget {
  final bool checked;
  MyWidget({this.checked});
  @override
  Widget build(BuildContext context) {
    return Container(
        width: 50,
        height: 50,
        decoration: BoxDecoration(
          color: Colors.white,
          shape: BoxShape.circle,
          border: Border.all(color: Colors.black, width: 3.0),
        ),
        child: Container(
          margin: EdgeInsets.all(10.0),
          decoration: BoxDecoration(
            color: checked ? Colors.green : Colors.red,
            shape: BoxShape.circle,
          ),
        ));
  }
}

Full Example:

// Copyright (c) 2019, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool checked = false;

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Container(
            height: double.infinity,
            child: Center(
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    InkWell(
                      onTap: _toggle,
                      child: MyWidget(checked: checked),
                    ),
                    Text(checked ? "checked" : "unchecked")
                  ]),
            )));
  }
}

class MyWidget extends StatelessWidget {
  final bool checked;
  MyWidget({this.checked});
  @override
  Widget build(BuildContext context) {
    return Container(
        width: 50,
        height: 50,
        decoration: BoxDecoration(
          color: Colors.white,
          shape: BoxShape.circle,
          border: Border.all(color: Colors.black, width: 3.0),
        ),
        child: Container(
          margin: EdgeInsets.all(10.0),
          decoration: BoxDecoration(
            color: checked ? Colors.green : Colors.red,
            shape: BoxShape.circle,
          ),
        ));
  }
}

Upvotes: 1

Related Questions