flutter 处理dialog点击事件回调

flutter 处理dialog点击事件回调

import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';

void main() {
  runApp(new RootLayout());
}

class RootLayout extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new RootLayoutM();
  }
}

class RootLayoutM extends State<RootLayout> implements OnDialogClickListener {
  String str = "show simple dialog";
  String showMsg = "show simple dialog";

  @override
  void onOk() {
    print('onOK');
    setState(() {
      showMsg = str + " onOK Click";
    });
  }

  @override
  void onCancel() {
    print('onCancel');
    setState(() {
      showMsg = str + " onCancel Click";
    });
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        home: new Scaffold(
      body: new Center(
        child:
            new Text(showMsg, style: new TextStyle(color: Color(0xFF00FF00))),
      ),
      floatingActionButton: new MyFloat(this),
    ));
  }
}

//定义一个抽象类
abstract class OnDialogClickListener {
  void onOk();

  void onCancel();
}

class MyFloat extends StatelessWidget {
  final OnDialogClickListener callback;

  MyFloat(this.callback);

  _showMyMaterialDialog(BuildContext context) {
    print("_showMyMaterialDialog");
    showDialog(
        context: context,
        builder: (context) {
          return new AlertDialog(
            title: new Text("title"),
            content: new Text("内容内容内容内容内容内容内容内容内容内容内容"),
            actions: <Widget>[
              new FlatButton(
                onPressed: () {
                  callback.onOk();
                  Navigator.of(context).pop();
                },
                child: new Text("确认"),
              ),
              new FlatButton(
                onPressed: () {
                  callback.onCancel();
                  Navigator.of(context).pop();
                },
                child: new Text("取消"),
              ),
            ],
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new FloatingActionButton(
        child: new Text("showDialog"),
        onPressed: () {
          _showMyMaterialDialog(context);
        });
  }
}

猜你喜欢

转载自www.cnblogs.com/mingfeng002/p/11586695.html