我正在構建一個語言應用程式。該應用程式的目的是向英語教授土耳其語。首先,讓我給你影像:



有一個滾動系統。textFormField 上方所寫單詞的土耳其語將被輸入到textFormField.
我將英語單詞及其土耳其語含義保存在一個串列中。
串列:
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),
wordAndMeaning("How's it going?", "Nas?l gidiyor", false),
wordAndMeaning("How's your day?", "Günün nas?ld?", false),
wordAndMeaning("How's your day going?", "Günün nas?l gidiyor", false),
wordAndMeaning("Nice to see you", "Seni g?rmek güzel", false),
wordAndMeaning("It's been a while", "G?rü?meyeli uzun zaman oluyor", false),
];
螢屏滾動系統中的順序與串列中的順序完全相同。我希望它隨機排序。我怎樣才能做到這一點?
代碼:
import 'package:flutter/material.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:getwidget/getwidget.dart';
class selamlasma_test1 extends StatefulWidget {
@override
State<selamlasma_test1> createState() => _selamlasma_test1State();
}
class _selamlasma_test1State extends State<selamlasma_test1> {
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),
wordAndMeaning("How's it going?", "Nas?l gidiyor", false),
wordAndMeaning("How's your day?", "Günün nas?ld?", false),
wordAndMeaning("How's your day going?", "Günün nas?l gidiyor", false),
wordAndMeaning("Nice to see you", "Seni g?rmek güzel", false),
wordAndMeaning("It's been a while", "G?rü?meyeli uzun zaman oluyor", false),
];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.amber[500],
appBar: AppBar(
backgroundColor: Colors.amber[500],
bottomOpacity: 0,
leading: IconButton(
icon: Icon(Icons.arrow_back_ios),
onPressed: () {
Navigator.pop(context);
},
),
title: Text("Selamla?ma Testi 1", style: TextStyle(fontSize: 25),),
),
body: Builder(builder: (context) {
final double height = MediaQuery.of(context).size.height - 160;
return Column(
children: [
CarouselSlider(
carouselController: _controller,
options: CarouselOptions(
reverse: false,
height: height,
viewportFraction: 1.0,
onPageChanged: (index, reason) => setState(() {
myController.clear();
}),
),
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: [
Text(
word.word,
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold, color: Colors.black),
),
SizedBox(height: 20,),
Padding(
padding: EdgeInsets.all(15),
child: TextFormField(
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
width: 3
),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
// border kal?nl???:
width: 3,
),
),
border: OutlineInputBorder(),
labelText: '"' word.word '"' " Türk?esi", floatingLabelStyle: TextStyle(fontSize: 23, color: Colors.white),
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),
// kenar ovalla?tirme:
shape: GFButtonShape.pills,
onPressed: () {
//e?er bir de?er girilmemi?se:
if (myController.text == "") {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text("Lütfen bir de?er giriniz!", style: TextStyle(fontSize: 20),),
duration: Duration(seconds: 2),
),
);
}
else if (myController.text.toLowerCase() != word.meaning.toLowerCase()) {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text("Yanl?? cevap!", style: TextStyle(fontSize: 20),),
duration: Duration(seconds: 2),
),
);
}
if (myController.text.toLowerCase() == word.meaning.toLowerCase()) {
print("Do?ru");
myController.clear();
AlertDialog dogru = AlertDialog(
content: Text("Tebrikler! Do?ru bildiniz.", style: TextStyle(fontSize: 22),),
actions: [
GFButton(
padding: EdgeInsets.only(left: 20, right: 20),
size: 35,
text: "Sonraki soru", textStyle: TextStyle(fontSize: 25),
onPressed: () {
_controller.nextPage(
duration: Duration(milliseconds: 500),
curve: Curves.ease,
);
// alert dialog u kapatma:
Navigator.pop(context);
},
),
],
);
showDialog(
context: context,
builder: (BuildContext context) {
return dogru;
},
);
}
},
),
],
),
),
const SizedBox(
width: 10,
),
],
),
);
},
);
}).toList(),
),
Container(
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20)),
),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
),
width: 55,
height: 55,
child: RaisedButton(
color: Colors.blue,
child: Icon(Icons.arrow_back_ios, size: 30, color: Colors.white,),
onPressed: () {
_controller.previousPage(
duration: const Duration(milliseconds: 100),
curve: Curves.easeInCirc,
);
},
// k??eyi yuvarla?t?rma:
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25),
),
)
),
SizedBox(
width: 10,
),
SizedBox(
width: 10,
),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
),
width: 55,
height: 55,
child: Container(
child: RaisedButton(
color: Colors.blue,
child: Icon(Icons.arrow_forward_ios, size: 30, color: Colors.white,),
onPressed: () {
_controller.nextPage(
duration: const Duration(milliseconds: 100),
curve: Curves.easeInCirc,
);
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25),
),
)
),
),
],
),
],
),
)
],
);
}) ,
);
}
}
class wordAndMeaning {
String word;
String meaning;
bool showMeaning;
wordAndMeaning(this.word, this.meaning, this.showMeaning);
}
在此先感謝您的幫助。
uj5u.com熱心網友回復:
您可以創建初始狀態并使用該shuffle方法。請執行下列操作。
@override
initState() {
wordsList.shuffle();
super.initState();
}
uj5u.com熱心網友回復:
這將隨機洗牌你的物品:
wordsList.shuffle();
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/433462.html
