提问者:小点点

错误:尝试将静态成员放在卡片标题行18中时,只能在初始值设定项中访问静态成员


所以它不允许我在卡片的标题中使用name,我已经尝试过将name设置为静态变量,但是在构建时我仍然会被抛出一个错误。 我已经列出了所有相关的代码行//非常感谢您的帮助

我应该补充的是,当我尝试将name设置为static var name=“”,然后运行应用程序时,卡名仍然显示为空白,因此它没有更新静态变量。 为什么会这样? 有没有办法解决这个问题? 我怎么做?

import 'package:flutter/material.dart';
import 'package:rflutter_alert/rflutter_alert.dart';


class CardApp extends StatefulWidget {
  @override
  _CardAppState createState() => _CardAppState();
}

class _CardAppState extends State<CardApp> with AutomaticKeepAliveClientMixin<CardApp> {
  var _widgetsList  = [];
  final myController = new TextEditingController();
  var name ;
  var test =
  Card(
    child: new ListTile(
      leading: Icon(Icons.album),
      title: Text(name), //this line
      subtitle: Text('this is the subtitle'),
    ),
  );

  @override
  void dispose() {
    // Clean up the controller when the widget is disposed.
    myController.dispose();
    super.dispose();
  }


  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return MaterialApp(
        home: Scaffold(
          floatingActionButton: FloatingActionButton(
            child: Icon(Icons.add),
            onPressed: () {
              return Alert(
                context: context,
                title: 'Please Input your note',
                desc: 'this is where you will input your note',
                buttons: [
                  DialogButton(child: Text('Cancel'), onPressed:() {Navigator.pop(context);}),
                  DialogButton(child: Text('Confirm'), onPressed:() => setState(() //this line
                  {name = myController.text; // all of this
                  print(name);
                  _widgetsList.add(test);
                  Navigator.pop(context);}
                  )
//end
                  )
                ],
                content: Form(child: Column(
                  children: <Widget>[
                    TextFormField(
                      controller: myController,
                      decoration: InputDecoration(labelText: 'Input what you want to say'),
                    )
                  ],
                ))
              ).show();
            }
          ),
          appBar: AppBar(
            backgroundColor: Colors.purple,
            title: Text('The Note App', style: TextStyle(
                color: Colors.white
            ),),
          ),
          body: Container(
            child: ListView(
              children: <Widget>[
                Card(
                  child: new ListTile(
                    leading: Icon(Icons.album),
                    title: Text('hello'),
                    subtitle: Text('hi'),
                  ),
                ),
                ..._widgetsList, // cards get appended to _widgetlist so it appears up on the screen
              ],

            ),





          ),

        ),

    );
  }

}

共1个答案

匿名用户

我想出了一个解决方案,而不是使用一个变量来表示一张卡片,我制作了一个名为addCard的函数来追加到我的widgetlist中

addCard(){
    return Card(
      child: ListTile(
        leading: Icon(Icons.album),
        title: Text(name),
        subtitle: Text('hi'),
      ),
    );
  }