颤振另一个类别的RFlutter警报

时间:2020-11-02 09:21:46

标签: flutter

我使用了flutter警报包rflutter_alert 1.1.0,在同一页面上编写相同代码时可以吗?但是由于有许多功能,我的代码在一页纸上太长了。

所以我尝试将警报功能拆分到另一个dart文件,但出现错误

A value of type 'Future<bool>' can't be returned from method 'build' because it has a return type of 'Widget'.dart(return_of_invalid_type)

如果我这样写

对话飞镖

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

class Dialog extends StatefulWidget {
  @override
  _DialogState createState() => _DialogState();
}

class _DialogState extends State<Dialog> {
  @override
  Widget build(BuildContext context) {
    return Alert(
      context: context,
      type: AlertType.error,
      title: "RFLUTTER ALERT",
      desc: "Flutter testing Testing testing.",
      buttons: [
        DialogButton(
          child: Text(
            "COOL",
            style: TextStyle(color: Colors.white, fontSize: 20),
          ),
          onPressed: () => Navigator.pop(context),
          width: 120,
        )
      ],
    ).show();
  }
}

按r时,分割rFlutter警报代码并调用它的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

build不需要窗口小部件Alert即可调用警报,但单击回调内的代码(例如Button)。

@override
  Widget build(BuildContext context) {
    return Container(
      child: FlatButton(onPressed: (){
        Alert(
          context: context,
          type: AlertType.error,
          title: "RFLUTTER ALERT",
          desc: "Flutter testing Testing testing.",
          buttons: [
            DialogButton(
              child: Text(
                "COOL",
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
              onPressed: () => Navigator.pop(context),
              width: 120,
            )
          ],
        ).show();
      }, child: Text("CLICK ME!!")),
    );
}