我们如何使用舍入的复选框制作网格视图并在Flutter中选择颜色

时间:2019-04-30 11:31:10

标签: android flutter flutter-layout

朋友们,我需要一个颜色选择器,我想使其变得扑朔迷离,并将所有颜色保留在四舍五入的支票中。

请查看屏幕截图。我想要与图片相同的UI。enter image description here

1 个答案:

答案 0 :(得分:2)

您可以根据需要将颜色添加到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,
          ),
        )
    );
  }
}

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