【问题标题】:Why does cubit result show before using the second Textfield?为什么在使用第二个 Textfield 之前显示cubit 结果?
【发布时间】:2021-12-27 17:38:49
【问题描述】:

此应用程序的目标是从第一个文本字段中获取输入,然后在第二个屏幕中对其进行打乱。然后提示用户解密并将其输入到第二个文本字段中。如果用户的 second_text_field 输入与 firs_text_field 中的输入匹配,则 UI 应显示“OKAY”;否则,“不好”。 在我看来,变量 mycontroller 在到达 BlockBuilder 下的文本小部件时什么都不存储。这是快照:

这里是相关的屏幕代码:

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

  import 'package:rafia_unscramble_demo/cubit/sentence_cubit.dart';

  class ScrambledUnscrambled extends StatelessWidget {
  final String? userInputFromFirstTextField;
  //final String? secondSTring;

  ScrambledUnscrambled({
  required this.userInputFromFirstTextField,
  //this.secondSTring,
  });

  @override
  Widget build(BuildContext context) {
  String str = "";
  final mycontroller = TextEditingController();

  List<String> inputs = userInputFromFirstTextField!.split(" ");
  String shuffledWords(List<String> input) {
    input.shuffle();
    // print(input);
    return input.join(" ");
  }

  return BlocProvider<SentenceCubit>(
    create: (context) => SentenceCubit(),
    child: Scaffold(
      appBar: AppBar(
        title: const Text('Unscramble'),
      ),
      body: Column(
        children: [
          Card(
            margin: const EdgeInsets.only(top: 50),
            child: Container(
              alignment: Alignment.center,
              width: double.infinity,
              height: 100,
              color: Colors.blueAccent,
              padding: const EdgeInsets.all(15),
              child: Text(
                shuffledWords(inputs),
                style: const TextStyle(
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                  color: Colors.white,
                ),
              ),
            ),
          ),
          const SizedBox(
            width: 20,
          ),
          Container(
            padding: const EdgeInsets.all(20),
            alignment: Alignment.center,
            child: TextField(
              controller: mycontroller,
              onChanged: (String text) {},
              decoration: InputDecoration(
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(10),
                ),
                hintText: 'Please, enter a grammatically correct sentence.',
                labelText: 'Second Input',
                labelStyle: TextStyle(
                  fontSize: 24,
                  color: Colors.blue[900],
                ),
              ),
            ),
          ),
          ElevatedButton(
            onPressed: () {
              str = mycontroller.text;
            },
            child: const Text("Submit"),
          ),
          if (str == mycontroller.text)
            BlocBuilder<SentenceCubit, SentenceState>(
              builder: (context, state) {
                return Container(
                  color: Colors.blueAccent,
                  padding: const EdgeInsets.all(20),
                  width: double.infinity,
                  height: 100,
                  child: Text(
                    BlocProvider.of<SentenceCubit>(context).checkForError(
                      str,
                      userInputFromFirstTextField!,
                    ),
                  ),
                );
              },
            ),
        ],
      ),
    ),
  );
  }
  }

这是肘部分: 句子状态:

    part of 'sentence_cubit.dart';

@immutable
abstract class SentenceState {}

class SentenceInitial extends SentenceState {
  final String textInput;
  SentenceInitial({
    this.textInput = "",
  });
}

class Success extends SentenceState {
  final String okay;
  Success({
    required this.okay,
  });
}

class Failed extends SentenceState {
  final String notOkay;
  Failed({
    required this.notOkay,
  });
}

句肘:

    import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:meta/meta.dart';

part 'sentence_state.dart';

class SentenceCubit extends Cubit<SentenceState> {
  //final String firstScreenText;
  // final String secondString;
  SentenceCubit(
      // this.firstScreenText,
      //this.secondString,
      )
      : super(SentenceInitial());

  String checkForError(String getSecondText, String getFirstString) {
    // getSecondText = secondString;
    final okay = Success(okay: "Okay");
    final notOkay = Failed(notOkay: "Not Okay");

    if (getSecondText == getFirstString) {
      return okay.okay;
    } else if (getSecondText == " ") {
      return " ";
    } else {
      return notOkay.notOkay;
    }
  }
}

【问题讨论】:

    标签: flutter dart textfield bloc flutter-cubit


    【解决方案1】:
    } else if (getSecondText == " ") {
    

    只有当第二个文本是空格时才会出现这种情况,我猜这是导致您的错误的错字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多