我正在努力打造我的第一个Flutter 翼项目.这是从下拉列表中 Select 用户的初始视图. 其 idea 是,当用户被选中时,文本应该显示该用户的一些信息,并根据信息显示登录按钮或注销按钮
但我在启动时收到一个意外的空值
有谁能帮帮我吗?
谢谢!
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
class UserScreen extends StatefulWidget {
@override
_UserState createState() => _UserState();
}
class _UserState extends State<UserScreen> {
String? dropdownvalue;
Position? position;
String? mvt;
List<Row> _widgetList = [];
@override
void initState() {
super.initState();
getAllUsers().then((value) => {dropdownvalue = value[0]});
getCurrentPosition().then((value) => position = value);
mvt = "";
_widgetList
.add(UserSelect(mvt!, dropdownvalue!, position!, _widgetList) as Row);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Center(
child: Padding(
padding: EdgeInsets.fromLTRB(0, 5, 0, 0),
child: Text(
'MY APP',
style: TextStyle(
color: Colors.orange,
fontSize: 16,
),
textAlign: TextAlign.center,
))),
backgroundColor: Color.fromARGB(255, 11, 11, 11),
automaticallyImplyLeading: false,
actions: [
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(padding: EdgeInsets.fromLTRB(0, 35, 50, 0)),
IconButton(
onPressed: () {
Navigator.pushNamed(context, '/admin');
},
icon: Icon(Icons.admin_panel_settings,
color: Colors.orange, size: 25))
],
),
],
),
backgroundColor: Color.fromARGB(255, 11, 11, 11),
body: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
decoration:
BoxDecoration(color: Color.fromARGB(255, 11, 11, 11)),
child: Column(children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
width: 200,
height: 30,
child: Text(
"SELECT YOUR USERNAME",
style: TextStyle(
color: Colors.orange,
),
),
)
]),
ListView(
children: _widgetList,
),
]))
],
),
),
);
}
}
class UserSelect extends StatefulWidget {
UserSelect(String move, String s, Position position, List<Row> widgetList);
@override
_UserSelect createState() => _UserSelect();
}
class _UserSelect extends State<UserSelect> {
late String move;
late String usu;
late Position pos;
late List<Row> _widgetList;
@override
void initState() {
super.initState();
getAllUsers().then((value) => {usu = value[0]});
getCurrentPosition().then((value) => pos = value);
move = "";
_widgetList = this._widgetList;
}
//UserSelect(this.move, this.usu, this.pos, this._widgetList);
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
width: 300,
child: Container(
decoration: BoxDecoration(
color: Colors.orange,
),
child: Column(children: [
FutureBuilder<List<String>>(
future: getAllUsers(),
builder: (context, snapshot) {
if (snapshot.hasData) {
var data = snapshot.data!;
return Container(
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton(
padding:
EdgeInsets.fromLTRB(40, 0, 40, 0),
isExpanded: true,
// Initial Value
value: usu,
// Down Arrow Icon
icon:
const Icon(Icons.keyboard_arrow_down),
// Array list of items
items: data.map((String items) {
return DropdownMenuItem(
value: items,
child: Text(items),
alignment: Alignment.center);
}).toList(),
// After selecting the desired option,it will
// change button value to selected value
onChanged: (String? newValue) {
setState(() {
usu = newValue!;
move = getNextMovement(usu) as String;
_widgetList.add(
UserNextMove(move, usu, pos)
as Row);
});
},
))));
} else {
return const CircularProgressIndicator();
}
},
),
]),
)),
]);
}
}
class UserNextMove extends State<UserSelect> {
final String move;
final String usu;
final Position pos;
UserNextMove(this.move, this.usu, this.pos);
@override
Widget build(BuildContext context) {
return Row(children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
width: 300,
child: Container(
decoration: BoxDecoration(
color: Color.fromARGB(255, 11, 11, 11),
),
child: Text("YOUR NEXT MOVEMENT SHOULD BE $move",
style: TextStyle(color: Colors.orange))))
]),
Row(children: [
move == "ENTRADA"
? ElevatedButton.icon(
label: Text("LOG IN",
style: TextStyle(
color: Colors.white,
)),
icon: Icon(Icons.login),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.greenAccent)),
onPressed: () {
if (usu != "") {
Navigator.pushNamed(context, '/cam',
arguments: ScreenArguments(usu, pos, move));
} else {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
behavior: SnackBarBehavior.floating,
width: 200,
backgroundColor: Colors.red,
content: new Text(
"SELECT A VALID USER",
textAlign: TextAlign.center,
),
duration: new Duration(seconds: 10),
));
}
},
)
: ElevatedButton.icon(
label: Text("LOG OUT",
style: TextStyle(
color: Colors.white,
)),
icon: Icon(Icons.login),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.redAccent)),
onPressed: () {
if (usu != "") {
Navigator.pushNamed(context, '/cam',
arguments: ScreenArguments(usu, pos, move));
} else {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
behavior: SnackBarBehavior.floating,
width: 200,
backgroundColor: Colors.red,
content: new Text(
"SELECT A VALID USER",
textAlign: TextAlign.center,
),
duration: new Duration(seconds: 10),
));
}
},
)
])
]);
}
}
Getalluser()是一个调用API并返回用户名列表(可以使用)的函数. GetCurrentPosition()是一个通用的地理定位器函数(可以工作) Getnextmoval()是一个函数,它根据用户名调用一个API并返回他应该执行哪个移动(这是可行的)
如果 Select 了用户,这if (usu != "") {Navigator.pushNamed(context, '/cam',arguments: ScreenArguments(usu, pos, move));
只会将用户重定向到摄像头(也可以)
提前谢谢您!