dart Text()中的文本不会更新

iqxoj9l9  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(99)

我有一个小部件列表,当我试图更新它们里面的文本时,它只是由于某种原因没有得到更新。即使当我添加了一个新的小部件,重建并看到这个小部件行(所以这意味着屏幕得到重建)我看不到更新的文本。是否可以更新它,或者它是静态的?

class NoteInfo {
  String _title = ""; // private backing variable

  String get Title => _title;
  set Title(String value) => _title = value;

  String _note = ""; // private backing variable

  String get Note => _note;
  set Note(String value) => _note = value;

  Icon icon = Icon(Icons.label);
  //DateTime date = DateTime(year);

  //List<Widget> active_notes = <Widget>[];

  String _shortNote = ""; // private backing variable

  String get shortNote => _shortNote;
  set shortNote(String value) => _shortNote = value;

  String _shortTitle = ""; // private backing variable

  String get shortTitle => _shortTitle;
  set shortTitle(String value) => _shortTitle = value;
}

List<NoteInfo> note_infos = [];
List<String> Titles = [];

lass HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

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

class HomeScreenState extends State<HomeScreen> {
Widget W1(BuildContext context) {
    final key = ValueKey(DateTime.now().toString());
    int index = -1;
    int ind = Titles.length - 1;
    Titles[ind] = "title" + ind.toString();
    return TextButton(
      key: key,
      onPressed: () async {
        await Navigator.push(context,
            MaterialPageRoute(builder: (BuildContext context) {
          index = active_notes.indexWhere((e) => e.key == key);
          Titles[index] = "title" + index.toString();
          return noteScreenState.note_page(
              context, index); //calling the editing menu
        }));
        Titles[ind] = "Rebuilded! " +
            index
                .toString(); //i tried to switch it to Titles[0] = "Rebuilded! " + index.toString(); and text didnt changed anyway
        setState(() {
          print("setstate"); //printed
        });
      },
      child: Container(
        height: 50,
        child: Row(
          children: [
            Icon(
              Icons.label,
              color: Colors.white,
            ), ],
        ),
      ),
    );
}

先谢谢你的帮助。
编辑:我认为它不会重建Widget

mqkwyuun

mqkwyuun1#

首先,在这段代码中没有设置Text(),但问题是需要在SetState中重新分配标题。更改了下面的W1小部件。

class HomeScreenState extends State<HomeScreen> {

@override    
Widget W1(BuildContext context) {
    final key = ValueKey(DateTime.now().toString());
    int index = -1;
    int ind = Titles.length - 1;
    Titles[ind] = "title" + ind.toString();
    return TextButton(
      key: key,
      onPressed: () async {
        await Navigator.push(context,
            MaterialPageRoute(builder: (BuildContext context) {
          index = active_notes.indexWhere((e) => e.key == key);
          Titles[index] = "title" + index.toString();
          return noteScreenState.note_page(
              context, index); //calling the editing menu
        }));
        setState(() {
            Titles[ind] = "Rebuilded! ${index.toString()}" 
          print("setstate"); 
        },);
      },
      child: Container(
        height: 50,
        child: Row(
          children: [
            Icon(
              Icons.label,
              color: Colors.white,
            ), ],
        ),
      ),
    );
}

相关问题