我正在看我的代碼,想了2個小時都沒有結果,所以我想在這里尋求幫助。
我有一個按鈕,當我按下它時,它會從串列視圖中隨機顯示一個專案。問題是我在串列視圖中的每個專案都有一個復選框。我不希望它(在勾選了復選框的專案中洗牌)只洗牌串列視圖中未勾選/未選中/未完成的任務。
以下是我的代碼
class TaskData extends ChangeNotifier {
List<Task> _tasks = [
Task(name: "item1")。
任務(name: "item2")。
任務(name: "item3")。
];
UnmodifiableListView<Task> get tasks {
return UnmodifiableListView(_tasks)。
}
int get taskCount {
return _tasks.length。
}
// <<這里是洗刷串列的代碼。
Future<String> rann() async {
return (_tasks.toList().shuffle()).first.name。
}
void addTask(String newTaskTitle) {
final task = Task(name: newTaskTitle)。
_tasks.add(task);
notifyListeners()。
}
void updateTask(Task task) {
task.toggleDone()。
notifyListeners()。
}
在另一個腳本中,我有這樣一個腳本
class Task {
final String name;
bool isDone。
Task({required this.name, this.isDone = false}) 。
void toggleDone() {
isDone = !isDone;
}
在另一個腳本檔案中,我有這樣的代碼
Padding(
padding:
const EdgeInsets.symmetric( horizontal: 20, vertical: 0)。)
孩子。FutureBuilder(
未來。Provider.of<TaskData>(context).rann()。
builder: (context, snapshot) {
return Align(
對齊。Alignment.center,
孩子。文本(
"${snapshot.data}"。
//softWrap: true,.
textAlign: TextAlign.center,
//textWidthBasis: TextWidthBasis.longestLine,/span>
風格。TextStyle(
color: Colors.white,
字體大小。30。
fontWeight: FontWeight.w700)。)
),
);
},
),
),
在另一個腳本中,我有這樣一個腳本
class 任務串列 extends StatelessWidget {
Widget build(BuildContext context) {
return Consumer<TaskData> (
builder: (context, taskData, child) {
return ListView.builder(
itemBuilder。(context, index) {
final task = taskData.tasks[index];
return TaskTile(
taskTitle: task.name,
isChecked: task.isDone,
checkboxCallback: (checkboxState) {
taskData.updateTask(task)。
},
);
},
itemCount: taskData.taskCount,
);
},
);
}
}
請提供任何幫助!
編輯:我還忘了包括這部分的代碼
class TaskTile title">TaskTile extends StatelessWidget{
final bool isChecked;
final String taskTitle;
final Function(bool?) checkboxCallback;
final VoidCallback longPressCallback;
任務瓦片(
{required this.isChecked,
required this.taskTitle,
required this.checkboxCallback,
required this.longPressCallback})。)
Widget build(BuildContext context) {
return ListTile(
onLongPress: longPressCallback,
標題。文本(
taskTitle,
//在底部,如果isChecked為真,它將設定文本的裝飾,如果它不是,則為空。
風格。TextStyle(
decoration: isChecked ? TextDecoration.lineThrough : null)。)
),
尾部。Checkbox(
activeColor: Colors.blue,
value: isChecked,
onChanged: checkboxCallback,
),
);
}
}
uj5u.com熱心網友回復:
通過簡單的檢查,你傳遞給它task.isDone== false你顯示listTile否則回傳空container
class 任務串列 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<TaskData> (
builder: (context, taskData, child) {
return ListView.builder(
itemBuilder。(context, index) {
final task = taskData.tasks[index];
return task.isDone== false? TaskTile( // here and the condition)
taskTitle: task.name,
isChecked: task.isDone,
checkboxCallback: (checkboxState) {
taskData.updateTask(task)。
},
): 容器()。
},
itemCount: taskData.taskCount,
);
},
);
}
}
uj5u.com熱心網友回復:
我認為你只需要在得到一個隨機元素之前過濾結果。你需要修改你的rann方法,比如
//你并不真正需要一個future方法,因為你沒有async代碼。
String rann() {
final r = Random();
final undoneTasks = _tasks.where((e)=> !e.isDone).toList()。
//this is for avoid RangeException on list. You can return any other thing
if(undoneTasks.isEmpty) return ' ';
// I think that you don't really need to shuffle whole list, you only need a random element.
return undoneTasks[r.nextInt(undoneTasks.length - 1)].name;
}
我希望這能解決你的問題
。轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/329150.html
標籤:
上一篇:如何用typescript撰寫一個能夠從一個物件陣列中添加或替換一個元素的函式
下一篇:膨脹瓦得到自動崩潰的飄帶
