这个应用程序的目标是从第一个文本字段中获取输入,然后在第二个屏幕中对其进行打乱。然后提示用户解读并将其输入到第二个文本字段中。如果用户的 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!,
),
),
);
},
),
],
),
),
);
}
}
这是肘部分: SentenceState:
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;
}
}
}