Flutter 颤振-如何通过单击按钮添加行
我想在单击“+添加行”按钮时添加6球行(请参见此),但我有一个问题,那就是我不确定它是否有我编码的Listview。 请帮助我完成“void addRow()”以通过单击按钮添加行 你能给我一些关于如何得到一个与其他行完全不同的随机数的新行的提示吗Flutter 颤振-如何通过单击按钮添加行,flutter,flutter-layout,Flutter,Flutter Layout,我想在单击“+添加行”按钮时添加6球行(请参见此),但我有一个问题,那就是我不确定它是否有我编码的Listview。 请帮助我完成“void addRow()”以通过单击按钮添加行 你能给我一些关于如何得到一个与其他行完全不同的随机数的新行的提示吗 import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:material
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'dart:math';
import 'package:barcode_scan/barcode_scan.dart';
void main() => runApp(
MaterialApp(
home: BallPage()
),
);
class BallPage extends StatefulWidget {
@override
_BallPageState createState() => _BallPageState();
}
class _BallPageState extends State<BallPage>{
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
drawer: new Drawer(),
appBar: AppBar(
title: Text(
'Magic 6 balls'),
backgroundColor: Colors.black,
actions: <Widget>[
new IconButton(
icon: new Icon(MdiIcons.squareInc),
color: Colors.white,
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => SecondRoute())
);
},
),
],
),
body: Number(),
);
}
}
class SecondRoute extends StatefulWidget {
@override
_SecondRouteState createState() {
return new _SecondRouteState();
}
}
class _SecondRouteState extends State<SecondRoute>{
String result ="Hey there!";
Future _scanQR() async {
try{
String qrResult = await BarcodeScanner.scan();
setState(() {
result = qrResult;
});
} on PlatformException catch (ex) {
if (ex.code == BarcodeScanner.CameraAccessDenied){
setState(() {
result = "Camera permission was denied";
});
} else {
setState(() {
result = "Unknown Error $ex";
});
}
} on FormatException {
setState(() {
result = "You pressed the black button before scanning anything";
});
}catch (ex) {
setState(() {
result = "Unknown Error $ex";
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('QR Code Scan'),
),
body: Center(
child: Text(
result,
style: new TextStyle(fontSize: 20.0),
),
),
floatingActionButton: FloatingActionButton.extended(
onPressed: _scanQR,
label: Text('Scan'),
icon: Icon(Icons.camera_alt),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
);
}
}
class Number extends StatefulWidget {
@override
_NumberState createState() => _NumberState();
}
class _NumberState extends State<Number> {
int ball1 = 1;
int ball2 = 1;
int ball3 = 1;
int ball4 = 1;
int ball5 = 1;
int ball6 = 1;
void randomNumbers() {
setState(() {
ball1 = Random().nextInt(49) + 1;
ball2 = Random().nextInt(49) + 1;
ball3 = Random().nextInt(49) + 1;
ball4 = Random().nextInt(49) + 1;
ball5 = Random().nextInt(49) + 1;
ball6 = Random().nextInt(49) + 1;
});
}
void addRows(){
setState(() {
});
}
void removeRows(){
setState(() {
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: ListView(
children: <Widget>[
SizedBox(height: 100.0),
Center(
child: Text('Winners Number')
),
SizedBox(height: 16.0),
buildForm(),
SizedBox(height: 16.0),
RaisedButton.icon(
icon: Icon(Icons.add),
label: Text('Add Rows'),
onPressed: () {
addRows();
},
),
],
),
),
],
),
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () {
randomNumbers();
},
label: Text(
'Click Here!'),
icon: Icon(
Icons.loop),
backgroundColor: Colors.black,
),
);
}
Widget buildForm() {
List<Widget> list = new List();
for (var index = 0; index < 1; index++) {
list.add(Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
height: 60,
width: 60,
child: Center
(
child: Text(
'$ball1',
style: TextStyle(
fontSize: 20.0,
),
),
),
decoration: BoxDecoration(
color: Colors.yellow,
shape: BoxShape.circle,
),
),
Container(
height: 60,
width: 60,
child: Center
(
child: Text(
'$ball2',
style: TextStyle(
fontSize: 20.0,
),
),
),
decoration: BoxDecoration(
color: Colors.yellow,
shape: BoxShape.circle,
),
),
Container(
height: 60,
width: 60,
child: Center
(
child: Text(
'$ball3',
style: TextStyle(
fontSize: 20.0,
),
),
),
decoration: BoxDecoration(
color: Colors.yellow,
shape: BoxShape.circle,
),
),
Container(
height: 60,
width: 60,
child: Center
(
child: Text(
'$ball4',
style: TextStyle(
fontSize: 20.0,
),
),
),
decoration: BoxDecoration(
color: Colors.yellow,
shape: BoxShape.circle,
),
),
Container(
height: 60,
width: 60,
child: Center
(
child: Text(
'$ball5',
style: TextStyle(
fontSize: 20.0,
),
),
),
decoration: BoxDecoration(
color: Colors.yellow,
shape: BoxShape.circle,
),
),
Container(
height: 60,
width: 60,
child: Center
(
child: Text(
'$ball6',
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
),
decoration: BoxDecoration(
color: Colors.black,
shape: BoxShape.circle,
),
),
],
));
list.add(
SizedBox(
height: 12.0,
));
}
return Column(
children: list);
}
}
导入'dart:async';
进口“包装:颤振/材料.省道”;
导入“包:flifter/services.dart”;
导入“包装:材料\设计\图标\颤振/材料\设计\图标\颤振.dart”;
导入“dart:math”;
导入“包装:条形码扫描/条形码扫描.dart”;
void main()=>runApp(
材料聚丙烯(
主页:BallPage()
),
);
类BallPage扩展StatefulWidget{
@凌驾
_BallPageState createState()=>\u BallPageState();
}
类_BallPageState扩展了状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
背景颜色:Colors.white,
抽屉:新抽屉(),
appBar:appBar(
标题:正文(
‘魔法六球’,
背景颜色:Colors.black,
行动:[
新图标按钮(
图标:新图标(MDICONS.squareInc),
颜色:颜色,白色,
已按下:(){
Navigator.push(上下文,
MaterialPartnerRoute(生成器:(上下文)=>SecondRoute())
);
},
),
],
),
正文:Number(),
);
}
}
类SecondRoute扩展StatefulWidget{
@凌驾
_SecondRouteState createState(){
返回新的_SecondRouteState();
}
}
类_secondroutstate扩展状态{
String result=“你好!”;
Future\u scanQR()异步{
试一试{
String qrResult=Wait BarcodeScanner.scan();
设置状态(){
结果=结果;
});
}平台上异常捕获(ex){
if(ex.code==BarcodeScanner.CameraAccessDenied){
设置状态(){
结果=“摄像机权限被拒绝”;
});
}否则{
设置状态(){
结果=“未知错误$ex”;
});
}
}论格式例外{
设置状态(){
result=“您在扫描任何内容之前按下了黑色按钮”;
});
}捕获(ex){
设置状态(){
结果=“未知错误$ex”;
});
}
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“二维码扫描”),
),
正文:中(
子:文本(
结果,,
样式:新文本样式(fontSize:20.0),
),
),
floatingActionButton:floatingActionButton.extended(
按下按钮:_scanQR,
标签:文本(“扫描”),
图标:图标(Icons.camera\u alt),
),
浮动ActionButtonLocation:浮动ActionButtonLocation.centerFloat,
);
}
}
类编号扩展StatefulWidget{
@凌驾
_NumberState createState()=>_NumberState();
}
类_NumberState扩展状态{
int ball1=1;
int-ball2=1;
int ball3=1;
int=4=1;
int=5=1;
int=6=1;
无效随机数(){
设置状态(){
ball1=Random().nextInt(49)+1;
ball2=Random().nextInt(49)+1;
ball3=Random().nextInt(49)+1;
ball4=Random().nextInt(49)+1;
ball5=Random().nextInt(49)+1;
ball6=Random().nextInt(49)+1;
});
}
void addRows(){
设置状态(){
});
}
空洞清除器(){
设置状态(){
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
孩子:排(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
扩大(
子:ListView(
儿童:[
尺寸箱(高度:100.0),
居中(
子项:文本('获奖者编号')
),
尺寸箱(高度:16.0),
buildForm(),
尺寸箱(高度:16.0),
RaisedButton.icon(
图标:图标(Icons.add),
标签:文本(“添加行”),
已按下:(){
addRows();
},
),
],
),
),
],
),
),
floatingActionButton:floatingActionButton.extended(
已按下:(){
随机数();
},
标签:文本(
“点击这里!”),
图标:图标(
图标。循环),
背景颜色:Colors.black,
),
);
}
Widget buildForm(){
列表=新列表();
对于(var指数=0;指数<1;指数++){
列表。添加(第行)(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
容器(
身高:60,
宽度:60,
儿童:中心
(
子:文本(
“$1”,
样式:TextStyle(
字体大小:20.0,
),
),
),
装饰:盒子装饰(
颜色:颜色,黄色,
形状:BoxShape.circle,
),
),
容器(
身高:60,
宽度:60,
儿童:中心
(
子:文本(
“$2”,
样式:TextStyle(
字体大小:20.0,
),
),
),
装饰:盒子装饰(
颜色:颜色,黄色,
形状:BoxShape.circle,
),
),
容器(
身高:60,
宽度:60,
儿童:中心