简体   繁体   English

我想在对话框 flutter DART 上显示结果

[英]i WANT TO display the RESULT on DIALOG box flutter DART

i have a SetState function with the name 'doaddition' that calculate the miduiom ' sum ' of values and i have a button i want to dispaly a dialog box when i click the button and inside this dialog box i want to display the result of 'sum'我有一个名为'doaddition'SetState function,它计算值的miduiom“ sum ”,我有一个按钮,我想在单击按钮时显示一个dialog box ,在此对话框中我想显示'sum'

ps: i want to create more than one dialog box and the Button display one of them Depending on the result of 'sum' ps:我想创建多个对话框,并且按钮显示其中一个取决于'sum'的结果

if the sum ==0 display the first dialog else if sum > 10 display the second dialog else display the third dialog如果sum ==0 显示第first dialog else if sum > 10 显示second dialog else 显示third dialog

and each of dialog box have a Text of its own and the result of 'sum'每个对话框都有自己的文本和“总和”的结果

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()),
                          ],
                        ),
                      ),
                    );
                  });
            }
          },
        ),
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM