简体   繁体   English

Dart Flutter 每个页面上的 sliderShow textFormField 值保持不变

[英]Dart Flutter sliderShow textFormField values ​on each page stay the same

I have such an application:我有这样一个应用程序:

在此处输入图像描述

The application purpose is to query whether the word entered in the textFormField corresponds to the meaning I keep in the list.应用目的是查询在textFormField中输入的单词是否对应我在列表中保留的意思。 This is a language app.这是一个语言应用程序。 The above screen is the test part.以上画面为测试部分。

I give the person a word and ask him to write it in Turkish.我给那个人一个词,让他用土耳其语写。

But there is a problem here.但是这里有一个问题。 I am using CarouselSlider for testing.我正在使用 CarouselSlider 进行测试。 Each slider screen has a textFormField.每个 slider 屏幕都有一个 textFormField。 But every time I change the page, the value in the textFormField from the previous page remains.但是每次我更改页面时,上一页的 textFormField 中的值仍然存在。

Example:例子:

Slider page 1: Slider 第 1 页:

在此处输入图像描述

Slider page 2: Slider 第 2 页:

在此处输入图像描述


The " Kontrol et " button is querying the correctness of the entered word. Kontrol et ”按钮查询输入的单词是否正确。

I want the textFormField to be reset and empty on every page change.我希望在每次页面更改时重置 textFormField 并清空。 How can I do that?我怎样才能做到这一点?


Codes:代码:

import 'package:flutter/material.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:getwidget/getwidget.dart';

class selamlasma_test1 extends StatelessWidget {
  final CarouselController _controller = CarouselController();
  final myController = TextEditingController();
  List<wordAndMeaning> wordsList = [
    wordAndMeaning("Hello", "Merhaba", false),
    wordAndMeaning("What's up?", "Naber?", false),
    wordAndMeaning("How are you?", "Nasılsın?", false),
    wordAndMeaning("Good morning", "Günaydın", false),
    wordAndMeaning("Good night", "İyi geceler", false),

  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.amber[500],
        bottomOpacity: 0,
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        title: Text("Selamlama Testi 1", style: TextStyle(fontSize: 25),),
      ),

      body: Builder(builder: (context) {
        final double height = MediaQuery.of(context).size.height;
        return Column(
          children: [
            CarouselSlider(
              carouselController: _controller,
              options: CarouselOptions(
                height: height - 86.8,
                viewportFraction: 1.0,
                enlargeCenterPage: false,
              ),
              items: wordsList.map((wordAndMeaning word) {
                return Builder(
                  builder: (BuildContext context) {
                    return Container(
                      width: MediaQuery.of(context).size.width,
                      decoration: BoxDecoration(color: Colors.amber),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Expanded(
                            child: Column(
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                Padding(
                                  padding: EdgeInsets.all(15),
                                  child: TextFormField( // <<<<!!!!!!!!
                                    decoration: InputDecoration(
                                      border: OutlineInputBorder(),
                                      labelText: '"' + word.word + '"' + " Türkçesi", floatingLabelStyle: TextStyle(fontSize: 23),
                                      prefix: Padding(
                                        padding: EdgeInsets.only(left: 5, right: 10),
                                        child: Icon(Icons.translate),
                                      ),
                                      labelStyle: TextStyle(
                                        fontSize: 20,
                                        fontWeight: FontWeight.bold,
                                        
                                      ),
                                    ),
                                    style: TextStyle(
                                      fontSize: 20,
                                      fontWeight: FontWeight.bold,
                                    ),
                                    controller: myController,
                                    onChanged: (value) {
                                      
                                    },
                                  ),
                                ),
                                GFButton(
                                  padding: EdgeInsets.only(left: 20, right: 20),
                                  size: 45,
                                  text: "Kontrol et", textStyle: TextStyle(fontSize: 25),
                                  onPressed: () {
                                     //eğer bir değer girilmemişse:
                                    if (myController.text == "") {
                                      Scaffold.of(context).showSnackBar(SnackBar(
                                        content: Text("Lütfen bir değer giriniz!"),
                                        duration: Duration(seconds: 2),
                                      ),
                                      );
                                    }
                                    if (myController.text.toLowerCase() == word.meaning.toLowerCase()) {
                                      print("Doğru");
                                    }
                                  },
                                ),
                              ],
                            ),
                          ),
                          const SizedBox(
                            width: 10,
                          ),
                          
                        ],
                      ),
                    );
                  },
                );
              }).toList(),
            ),
          ],
        );
      }) ,
    );
  }
}

class wordAndMeaning {
  String word;
  String meaning;
  bool showMeaning;

  wordAndMeaning(this.word, this.meaning, this.showMeaning);
}

Thanks in advance for the help.先谢谢您的帮助。

You are facing this issue, because your current TextEditingController is same for all your TextFormField .您正面临这个问题,因为您当前的TextEditingController对于所有TextFormField都是相同的。 Try providing for each TextFormField its own controller.尝试为每个TextFormField提供它自己的 controller。

return Builder(builder: (BuildContext context) {
  final TextEditingController myController = TextEditingController();
  return Container(
    child: TextFormField(
      controller: myController,
    ),
  );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM