颤动将对象传递给有状态的小部件

萨姆·克罗默

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

   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应用,可在按下按钮时将用户对象传递到不同的屏幕。传递给无状态窗口小部件时,这非常有用。我尝试在有状态小组件中实现相同的模式,但无法正常工作。我假设自从我刚开始振作起来以来,我就做错了,有状态的小部件必须以不同的方式处理此对象。

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

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”。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章