Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何绘制简单的单选按钮?_Flutter_Dart - Fatal编程技术网

Flutter 如何绘制简单的单选按钮?

Flutter 如何绘制简单的单选按钮?,flutter,dart,Flutter,Dart,我想在颤振上按此按钮作为练习: 以下是我已经做过的: Stack( children: [ Align( alignment: Alignment.center, child: ClipOval( child: Container(

我想在颤振上按此按钮作为练习:

以下是我已经做过的:

              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)),
                    ),
                  ],
                )
这两个问题是:

即使我使用
Align


我无法像在图像中那样在按钮上绘制边框,ClipOval没有此选项

您可以使用
容器
小部件

以下是如何:

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,
          ),
        ));
  }
}
完整示例:

// 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,
          ),
        ));
  }
}

//版权所有(c)2019,Dart项目作者。请参阅作者档案
//详情请参阅。版权所有。此源代码的使用受
//可以在许可证文件中找到的BSD样式的许可证。
进口“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
debugShowCheckedModeBanner:false,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
bool checked=false;
void _toggle(){
设置状态(){
选中=!选中;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
主体:容器(
高度:双无限,
儿童:中心(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
墨水池(
onTap:_切换,
子项:MyWidget(选中:选中),
),
文本(选中?“选中”:“未选中”)
]),
)));
}
}
类MyWidget扩展了无状态Widget{
检查最终bool;
MyWidget({this.checked});
@凌驾
小部件构建(构建上下文){
返回容器(
宽度:50,
身高:50,
装饰:盒子装饰(
颜色:颜色,白色,
形状:BoxShape.circle,
边框:边框。全部(颜色:Colors.black,宽度:3.0),
),
子:容器(
保证金:所有边缘套(10.0),
装饰:盒子装饰(
颜色:选中?颜色。绿色:颜色。红色,
形状:BoxShape.circle,
),
));
}
}