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

时间:2019-03-12 23:08:33

标签: dart flutter

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

   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;

0 个答案:

没有答案