gpt4 book ai didi

dart - Flutter 将对象传递给有状态的小部件

转载 作者:IT王子 更新时间:2023-10-29 06:53:39 24 4
gpt4 key购买 nike

编辑——错误是用户未定义。

   class _CheckInState extends State<CheckIn> {

double _value = 0;
String _emotionalStatus = 'Happy';
bool didStartDrag = false;
double updatedDragDelta;
bool didEndDrag = false;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new LBAppBar().getAppBar(),
drawer: new LBDrawer().getDrawer(),
body:
SwipeDetector(

child: Container(
decoration: BoxDecoration(
gradient: new LinearGradient(
colors: [Color.fromRGBO(1,89,99, 1.0), Colors.grey],
begin: Alignment.bottomLeft,
end: Alignment.topRight
)
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[

Container(
margin: EdgeInsets.only(left: 30.0,top: 30.0, bottom: 70.0, right:30.0),
child :
Text(

'How are you feeling today Sam?', style: new TextStyle( color: Colors.white, fontWeight: FontWeight.bold, fontSize: 21.0 ))
,
),
Text(
'${_emojis[_value.toInt()]}',
style: Theme.of(context).textTheme.display3,
),

Container(

margin: EdgeInsets.only(left: 30.0,top: 20.0, bottom: 20.0, right:30.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget> [
Container(
margin: EdgeInsets.all(10.0),
child: Text(

'$_emotionalStatus', style: new TextStyle( color: Colors.white, fontWeight: FontWeight.bold, fontSize: 21.0 ))
,
)

]
)

),


Padding(
padding: const EdgeInsets.symmetric(horizontal: 15.0),
child:

Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(_emojis[0], softWrap: true),
Expanded(
child: Slider(
value: _value,
// label: _emojis[_value.toInt()],
min: 0.0,
max: 4.0,
divisions: 4,
onChangeStart: (double value) {
print('Start value is ' + value.toString());
},
onChangeEnd: (double value) {
print('Finish value is ' + value.toString());
if (value.toString() == '0.0') {
_emotionalStatus = 'Happy';
}
if (value.toString() == '1.0') {
_emotionalStatus = 'Optimistic';
}
if (value.toString() == '2.0') {
_emotionalStatus = 'Neutral';
}
if (value.toString() == '3.0') {
_emotionalStatus = 'Pessimistic';
}
if (value.toString() == '4.0') {
_emotionalStatus = 'Sad';
}
setState(() {

});
},
onChanged: (double value) {
setState(() {
_value = value;
});
},
activeColor: Colors.white,
inactiveColor: Colors.white,
),
),
Text(_emojis[4], softWrap: true,)
],
),
),
),




Container(
margin: EdgeInsets.all(20.0),
child:OutlineButton(
child: Text('Tap to Continue'), textColor: Colors.white,
shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0)),
onPressed: (){
Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new CheckInQ(user: user, emoji: _emojis[_value.toInt()], mood: _emotionalStatus))
);
}
)
)

我们有一个 flutter 应用程序,它在按下按钮时将用户对象传递到不同的屏幕。这在传递给 Stateless Widget 时效果很好。我尝试在有状态小部件中实现相同的模式,但它不起作用。我假设因为我是 Flutter 的新手,所以我做错了,有状态的小部件必须以不同的方式处理这个对象。

这是无状态小部件的代码:

class HomeMember extends StatelessWidget {
User user;

HomeMember({Key key, @required this.user}) : super(key: key);

@override
Widget build(BuildContext context){
bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;
return new Scaffold(
appBar : LBAppBar().getAppBar(),
//drawer: new LBDrawer().getDrawer(),
body: Container(
decoration: BoxDecoration(
gradient: new LinearGradient(
colors: [Color.fromRGBO(1,89,99, 1.0), Colors.grey],
begin: Alignment.bottomLeft,
end: Alignment.topRight
)
),

child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children:[
Row(
children: [
Container(
margin: EdgeInsets.only(left: 20.0,top: 10.0, bottom: 10.0, right:30.0),
child: Column(
children: <Widget>[



Text("Hi ${user.firstName}, Today is " + formatDate(), style: new TextStyle( color: Colors.white70, fontWeight: FontWeight.bold, fontSize: 19.0 )),




],

我尝试使用这种方法在有状态的小部件中传递用户对象,但它不起作用。

class CheckIn extends StatefulWidget {

@override
_CheckInState createState() => _CheckInState();
User user;
CheckIn({Key key, @required this.user}) : super(key: key);
}



class _CheckInState extends State<CheckIn> {

double _value = 0;
String _emotionalStatus = 'Happy';
bool didStartDrag = false;
double updatedDragDelta;
bool didEndDrag = false;

最佳答案

假设我们有一个无状态的小部件:

class HomeMember extends StatelessWidget {
final User user;

HomeMember({Key key, @required this.user}) : super(key: key);

@override
Widget build(BuildContext context) {
return new Scaffold(
body: Center(
child: Text("Hi ${user.username}",
),
);
}
}

等效的有状态小部件如下所示:


class HomeMemberStateful extends StatefulWidget {
final User user;
HomeMemberStateful({Key key, @required this.user}) : super(key: key);

@override
_HomeMemberStatefulState createState() => _HomeMemberStatefulState();
}

class _HomeMemberStatefulState extends State<HomeMemberStateful> {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: Center(
child: Text("Hi ${widget.user.username}, this is a stateful widget",
),
);
}
}

您可以通过在状态类中调用 widget.user 来访问对象“user”。

关于dart - Flutter 将对象传递给有状态的小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55131992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com