[英]I have a variable and I want to display a dialog box when press the button flutter dart
[英]i WANT TO display the RESULT on DIALOG box flutter DART
我有一個名為'doaddition'
的SetState
function,它計算值的miduiom“ sum
”,我有一個按鈕,我想在單擊按鈕時顯示一個dialog box
,在此對話框中我想顯示'sum'
ps:我想創建多個對話框,並且按鈕顯示其中一個取決於'sum'
的結果
如果sum
==0 顯示第first dialog
else if sum
> 10 顯示second dialog
else 顯示third dialog
每個對話框都有自己的文本和“總和”的結果
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.