Flutter NoSuchMethodError:类“bool”没有示例方法“[]”

mlmc2os5  于 2022-11-17  发布在  Flutter
关注(0)|答案(1)|浏览(125)

我做了一个日历日期选择器todoList。所以我想如果我选择了一天。返回一个当天的todolist。但是ListView不工作。问题在哪里??我想如果我点击日历中的一天。我想从所有的待办事项列表中提取相应日期的待办事项列表。
Main.dart

import 'package:calendar_app/todo_list.dart';
import 'package:flutter/material.dart';
import 'package:table_calendar/table_calendar.dart';
import 'package:intl/intl.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'calendar',
      debugShowCheckedModeBanner: false,
      home: MyHome(),
    );
  }
}

class MyHome extends StatefulWidget {
  const MyHome({super.key});

  @override
  State<MyHome> createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {

  CalendarFormat _calendarFormat =CalendarFormat.month;
  DateTime _focusedDay = DateTime.now();
  DateTime? _selectedDay;
  DateTime dateTime = DateTime(2022,11,11);

  //todo List - all days
  List todoList = [
    ['Nov 22, 2022','todo22',false],
    ['Nov 23, 2022','todo23',false],
    ['Nov 24, 2022','todo24',false],
    ['Nov 25, 2022','todo25',false],
    ['Nov 26, 2022','todo26',false],
  ];  

  //todo List - a day ex)2022-11-15 todo
  List dayTodo =[];
  void onChanged(bool? value, int index){
    setState(() {
      dayTodo[index][2]=!dayTodo[index][2];
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'calendar',
          style: TextStyle(
            color: Colors.black,
          ),
        ),
        backgroundColor: Colors.grey[200],
        elevation: 0,
      ),
      body: Column(
        children: [
          TableCalendar(
            focusedDay:_focusedDay,
            firstDay: DateTime.utc(1900,1,1),
            lastDay: DateTime.utc(3000,1,1),
            calendarFormat: _calendarFormat,
            selectedDayPredicate: (day){
              return isSameDay(_selectedDay,day);
            },
            onDaySelected: (selectedDay, focusedDay){
              if(!isSameDay(_selectedDay, selectedDay)){
                setState((){  
                  _selectedDay = selectedDay;
                  _focusedDay = focusedDay;
                  dateTime = focusedDay;
                  for(int i = 0; i<todoList.length; i++){
                     if(todoList[i][0]==DateFormat.yMMMd().format(dateTime)){
                       dayTodo.clear();
                       dayTodo.addAll(todoList[i]);
                     }
                }
                });
              };
            },
            onFormatChanged: (format){
              if(_calendarFormat != format){
                setState(() {
                  _calendarFormat = format;
                });
              }
            },
            onPageChanged: (focusedDay){
              _focusedDay = focusedDay;
            },
          ),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 25),
            child: Row(
              children: [
                Text(
                  '${dateTime.year} - ${dateTime.month} - ${dateTime.day}',
                  style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.bold
                  ),
                )
              ],
            ),
          ),
          SizedBox(
            height:400,
            child: ListView.builder(
              itemCount: dayTodo.length,
              itemBuilder: (context, index) {
                return TodoList(
                  todo:dayTodo[index][1],
                  onChanged:(value)=>onChanged(value, index),
                );
              },
            ),
          )
        ],
      ),
    );
  }
}

待办事项列表.dart

import 'package:flutter/material.dart';

class TodoList extends StatelessWidget {
  final String todo;
  final Function(bool?)? onChanged;
  const TodoList({
    super.key,
    required this.todo,
    required this.onChanged,
  });

  @override
  Widget build(BuildContext context) {
    return  Container(
       margin: EdgeInsets.all(20),
       padding: EdgeInsets.all(20),
       color: Colors.red,
       child: Column(
         children: [
           Row(
             children: [
               Checkbox(
                value:false ,
                onChanged:onChanged
              ),
               Text(todo),
               Text('2022'),
             ],
           ),
         ],
       ),
     );
  }
}

我想把当天的待办事项列表打印在日历下面。请帮助我!!

4nkexdtk

4nkexdtk1#

您正在添加单个项目,请使用dayTodo.add(todoList[i]);而不是.addAll

onDaySelected: (selectedDay, focusedDay) {
  if (!isSameDay(_selectedDay, selectedDay)) {
    _selectedDay = selectedDay;
    _focusedDay = focusedDay;
    dateTime = focusedDay;
    for (int i = 0; i < todoList.length; i++) {
      if (todoList[i][0] == DateFormat.yMMMd().format(dateTime)) {
        dayTodo.clear();    
        dayTodo.add(todoList[i]);
      }
    }
    setState(() {});
  }
},

相关问题

微信公众号

最新问答

更多