【问题标题】:i WANT TO display the RESULT on DIALOG box flutter DART我想在对话框颤振 DART 上显示结果
【发布时间】:2021-05-24 08:10:32
【问题描述】:

我有一个名为 'doaddition'SetState 函数,用于计算 miduiom 'sum' 的值 我有一个按钮,我想在单击按钮时显示dialog box,在此对话框中我想显示'sum'的结果

ps:我想创建多个对话框,并且按钮显示其中一个取决于'sum'的结果

如果sum ==0 显示first dialog else if sum> 10 显示second dialog 否则显示third dialog

每个对话框都有自己的文本 和'sum'的结果

【问题讨论】:

  • 您好。您应该添加更多详细信息,说明到目前为止您已尝试过的内容以及遇到的问题。在帖子中添加更多代码。

标签: flutter dart dialog setstate


【解决方案1】:
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var sum = 0;
  void doAddition() {
    setState(() {
      sum = 15;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: FlatButton(
          child: Container(
              height: 30,
              width: 90,
              color: Colors.blue,
              child: Center(child: Text("Display"))),
          onPressed: () {
            doAddition();
            if (sum < 10) {
              showDialog(
                  context: context,
                  builder: (context) {
                    return Dialog(
                      child: Container(
                        height: 150,
                        child: Column(
                          children: <Widget>[
                            Text("Sum less than 10"),
                            Text("sum = " + sum.toString()),
                          ],
                        ),
                      ),
                    );
                  });
            } else if (sum > 10 && sum < 20) {
              showDialog(
                  context: context,
                  builder: (context) {
                    return Dialog(
                      child: Container(
                        height: 150,
                        child: Column(
                          children: <Widget>[
                            Text("Sum greater than 10 but less than 20"),
                            Text("sum = " + sum.toString()),
                          ],
                        ),
                      ),
                    );
                  });
            } else if (sum > 20 && sum < 30) {
              showDialog(
                  context: context,
                  builder: (context) {
                    return Dialog(
                      child: Container(
                        height: 150,
                        child: Column(
                          children: <Widget>[
                            Text("Sum greater than 20 but less than 30"),
                            Text("sum = " + sum.toString()),
                          ],
                        ),
                      ),
                    );
                  });
            } else {
              showDialog(
                  context: context,
                  builder: (context) {
                    return Dialog(
                      child: Container(
                        height: 150,
                        child: Column(
                          children: <Widget>[
                            Text("Sum greater than 30"),
                            Text("sum = " + sum.toString()),
                          ],
                        ),
                      ),
                    );
                  });
            }
          },
        ),
      ),
    );
  }
}

【讨论】:

    猜你喜欢
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 2019-07-03
    • 2021-02-27
    • 2021-12-25
    相关资源
    最近更新 更多