flutter的dialog

学习flutter的第四天
每天进步一点点,每天努力一点点
争取早日能买房


class DialogMemo extends StatelessWidget {
  const DialogMemo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        ElevatedButton(onPressed: () async {
       var result=await  showDialog(context: context, builder: (context){
           return AlertDialog(
             title: const Text("title"),
             content: const Text("content"),
             actions: [
               ElevatedButton(onPressed: (){
                 Navigator.of(context).pop("ok");
               },
                   child: const Text("确定")),
               ElevatedButton(onPressed: (){
                 Navigator.of(context).pop("cancel");
               },
                   child: const Text("取消")),
             ],
           );
         });
       print(result);
        }, child: const Text("dialog")),

        ElevatedButton(onPressed: () async {
       var simResult=await   showDialog(context: context, builder: (context){
            return SimpleDialog(
              title: const Text("simple title"),
              children: [
               SimpleDialogOption(onPressed: (){
                 Navigator.of(context).pop("data1");
               },child: Text("data1"),),
                SimpleDialogOption(onPressed: (){
                  Navigator.of(context).pop("data2");
                },child: Text("data2"),),
                SimpleDialogOption(onPressed: (){
                  Navigator.of(context).pop("data3");
                },child: Text("data3"),),
                Row(
                  children: [
                    ElevatedButton(onPressed: (){
                      Navigator.of(context).pop("ok");
                    },
                        child: const Text("确定")),
                    ElevatedButton(onPressed: (){
                      Navigator.of(context).pop("cancel");
                    },
                        child: const Text("取消")),
                  ],
                )
              ],

            );
          });
       print("result:$simResult");
        }, child: Text("simpleDialog")),

        ElevatedButton(onPressed: (){
          showModalBottomSheet(context: context, builder: (context){
            return ListView(
              children: _getList(),
            );
          });
        }, child: Text("bottomDialog")),
        ElevatedButton(onPressed: () async{
          var result= await showDialog(context: context, builder: (context){
            return MyDialog(title: "自定义dialgo title", onClosed: (){
              Navigator.of(context).pop("close");
            });
          });
          print(result);
        }, child: Text("自定义dialgo")),
      ],
    );
   
  }
}

List _getList(){
  List data=[];
  for(int i=0;i <10;i++){
    data.add(ListTile(
      title: Text(" value & data :$i"),
    ));
  }
  return data;
}

自定义的dialog

//自定义dialog
class MyDialog extends Dialog{
  String title;
  String content;
  Function()? onClosed;
  MyDialog({Key? key,required this.title,
  required this.onClosed, this.content="自定义dialog"}):super(key: key);

  @override
  Widget build(BuildContext context) {

    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
          height: 300,
          width: 300,
          color: Colors.white,
          child: Column(
            children: [
              Padding(padding:const EdgeInsets.all(10),
                child: Stack(
                  children: [
                    Align(
                      alignment: Alignment.center,
                      child: Text(title),
                    ),
                    Align(
                      alignment: Alignment.topRight,
                      child: InkWell(
                        onTap: onClosed,
                        child:const Icon(Icons.close),
                      ),
                    )
                  ],
                ),
              ),
              const Divider(),
              Container(
                padding:const  EdgeInsets.all(10),
                width: double.infinity,
                child: Text(content,textAlign: TextAlign.center,),
              ),
              Row(
                children: [
                  ElevatedButton(onPressed: (){
                    Navigator.of(context).pop("ok");
                  },
                      child: const Text("确定")),
                  ElevatedButton(onPressed: (){
                    Navigator.of(context).pop("cancel");
                  },
                      child: const Text("取消")),
                ],
              )

            ],
          ),
        ),
      ),
    );
  }
}

你可能感兴趣的:(flutter的dialog)