首页 > 解决方案 > 我们如何使用圆形复选框制作网格视图并在颤动中选择颜色

问题描述

朋友们,我需要一个颜色选择器,我想让它在颤动中,并将所有颜色保持在圆形检查中。

请看截图。我想要与图片相同的用户界面。在此处输入图像描述

标签: androidflutterflutter-layout

解决方案


colorsData 您可以根据需要将颜色添加到列表中

这对你有帮助,

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

class MainPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp (
        debugShowCheckedModeBanner: false,
        home:Demo(),
    );
  }

}

class Demo extends StatefulWidget {
  @override
  _DemoState createState() => _DemoState();
}

class _DemoState extends State<Demo> {

  List colorsData = [Colors.green,Colors.red,Colors.greenAccent,Colors.cyan,Colors.purple,Colors.yellow,Colors.blue,Colors.black,Colors.brown,Colors.orange, Colors.teal, Colors.purpleAccent, Colors.blueGrey, Colors.tealAccent, Colors.deepOrangeAccent, Colors.lightBlueAccent];
  int selectedColor = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Color picker"),
        ),
        body: new Container(
          padding: EdgeInsets.all(16.0),
          color: Colors.black12,
          child: GridView.builder(
            scrollDirection: Axis.vertical,
            itemCount: colorsData.length,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4 ,childAspectRatio:1),
            itemBuilder: (BuildContext context,int index){

              return Padding(
                padding: const EdgeInsets.only(right: 8.0),
                child: Column(
                  children: <Widget>[
                    new FloatingActionButton(
                      onPressed: (){
                        setState(() {
                          selectedColor = index;
                        });
                      },
                      child: Icon(Icons.done,color: index == selectedColor ? Colors.white :colorsData.elementAt(index),size: 28),
                      backgroundColor: colorsData.elementAt(index),
                      elevation: 0.0,
                      heroTag: null,

                    ),
                    Offstage(
                      offstage: index != selectedColor,
                      child: Text("Color Name"),
                    ),
                  ],
                ),
              );
            },
            shrinkWrap: true,
          ),
        )
    );
  }
}

要显示颜色名称,请更改 Text 静态值 instedColor Name分配适当的颜色名称


推荐阅读