如何在GridView中编辑特定项目?

时间:2019-09-27 22:17:42

标签: flutter dart flutter-gridview

我将如何在GridView中编辑/定位特定项目。 此Gridview列出了每个容器中2020年1月的日子。如何更改特定日期的容器边框? 例... 我想将第一行中第二个项目的边框更改为红色,而不是黑色(01-02-2020)

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final title = 'Grid List';

    return MaterialApp(
      title: title,
      home: Scaffold(
        appBar: AppBar(
          title: Text(title),
        ),
        body: GridView.count(
          // Create a grid with 2 columns. If you change the scrollDirection to
          // horizontal, this produces 2 rows.
          crossAxisCount: 4,
          // Generate 100 widgets that display their index in the List.
          children: getDayInWeek(),
        ),
      ),
    );
  }
}

List<Widget> getDayInWeek() {
  var list = <DateTime>[];
  DateTime start = DateTime(2020, 01, 01);
  final end = DateTime(2020, 01, 31);

  while (start.isBefore(end)) {
    list.add(start);
    start = start.add(const Duration(days: 1));
  }

  return list.map((DateTime time) {
    return Container(
      margin: const EdgeInsets.all(2.0),
      padding: const EdgeInsets.all(.0),
      decoration: myBoxDecoration(),
      child: Column(
        children: <Widget>[
          Text(
            new DateFormat("MM-dd-yyyy").format(time),
            style: TextStyle(fontSize: 15.0, backgroundColor: Colors.white),
            textAlign: TextAlign.center,
          ),
        ],
      ),
    );
  }).toList();
}

BoxDecoration myBoxDecoration() {
  return BoxDecoration(
    border: Border.all(width: .1),
  );
}

2 个答案:

答案 0 :(得分:1)

您可以使用index并使用三元条件来更改BoxDecoration()中的边框颜色。就像我在我的应用中使用的一样。

Container(
     height: 30,
     width: 155,
     decoration: ShapeDecoration(
    shape: RoundedRectangleBorder(
    side: BorderSide(
    color: index == 7
    ? dateDifference
       ? Colors.red
       : lightGrey
       : lightGrey),
       borderRadius:
       BorderRadius.all(Radius.circular(3.0)),
     ),
  ),

答案 1 :(得分:0)

您可以使用difference

int diffDays = date1.difference(date2).inDays;