Flutter应用程序,演示如何使用moment_dart库在Flutter中操作和格式化日期和时间

blmhpbnm  于 2023-03-04  发布在  Flutter
关注(0)|答案(1)|浏览(160)

我正在开发一个Flutter应用程序,演示如何使用moment_dart库在Flutter中操作和格式化日期和时间。

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Moment Demo',
      home: MomentDemo(),
    );
  }
}

class MomentDemo extends StatefulWidget {
  @override
  _MomentDemoState createState() => _MomentDemoState();
}

class _MomentDemoState extends State<MomentDemo> {
  Moment _startDate = Moment.utc([2023, 1, 1]);
  Moment _endDate = Moment.utc([2023, 2, 28]);

  List<String> _dates = [    '2023-01-01',    '2023-01-08',    '2023-01-15',    '2023-01-22',    '2023-01-29',    '2023-02-05',    '2023-02-12',    '2023-02-19',    '2023-02-26',    '2023-03-05',  ];
  List<String> _filteredDates = [];

  void _filterDates() {
    _filteredDates = _dates.where((date) {
      Moment moment = Moment.parse(date);
      return moment.isAfter(_startDate) && moment.isBefore(_endDate);
    }).toList();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Moment Demo'),
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                ElevatedButton(
                  onPressed: () {
                    showDatePicker(
                      context: context,
                      initialDate: _startDate.toDateTime(),
                      firstDate: DateTime.utc(2023),
                      lastDate: _endDate.toDateTime(),
                    ).then((value) {
                      if (value != null) {
                        _startDate = Moment.fromDateTime(value.toUtc());
                        _filterDates();
                      }
                    });
                  },
                  child: Text('Başlangıç Tarihi Seç'),
                ),
                ElevatedButton(
                  onPressed: () {
                    showDatePicker(
                      context: context,
                      initialDate: _endDate.toDateTime(),
                      firstDate: DateTime.utc(2023),
                      lastDate: DateTime.utc(2023, 12, 31),
                    ).then((value) {
                      if (value != null) {
                        _endDate = Moment.fromDateTime(value.toUtc());
                        _filterDates();
                      }
                    });
                  },
                  child: Text('Bitiş Tarihi Seç'),
                ),
              ],
            ),
            SizedBox(height: 16.0),
            Text(
              'Başlangıç Tarihi: ${_startDate.format("dd/MM/yyyy")}',
              style: TextStyle(fontSize: 16.0),
            ),
            SizedBox(height: 8.0),
            Text(
              'Bitiş Tarihi: ${_endDate.format("dd/MM/yyyy")}',
              style: TextStyle(fontSize: 16.0),
            ),
            SizedBox(height: 16.0),
            Expanded(
              child: ListView.builder(
                itemCount: _filteredDates.length,
                itemBuilder: (context, index) {
                  return ListTile(
                    title: Text(_filteredDates[index]),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}

在这段代码中,我得到了我写在下面的错误,我请求帮助。谢谢大家。

  • 没有为类型“Moment”定义方法“utc”。
  • 没有为类型“Moment”定义方法“toDateTime”。
  • 没有为类型“Moment”定义方法“fromDateTime”。
3npbholx

3npbholx1#

确保你使用最新的versionmoment_dart
下面是修复代码的完整示例:

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Moment Demo',
      home: MomentDemo(),
    );
  }
}

class MomentDemo extends StatefulWidget {
  @override
  _MomentDemoState createState() => _MomentDemoState();
}

class _MomentDemoState extends State<MomentDemo> {
  Moment _startDate = DateTime(2023, 1, 1).toMoment();

  // Moment.utc([2023, 1, 1]);
  Moment _endDate = DateTime(2023, 1, 1).toMoment();

  // Moment.utc([2023, 2, 28]);

  List<String> _dates = [
    '2023-01-01',
    '2023-01-08',
    '2023-01-15',
    '2023-01-22',
    '2023-01-29',
    '2023-02-05',
    '2023-02-12',
    '2023-02-19',
    '2023-02-26',
    '2023-03-05',
  ];
  List<String> _filteredDates = [];

  void _filterDates() {
    _filteredDates = _dates.where((date) {
      Moment moment = Moment.parse(date);
      return moment.isAfter(_startDate) && moment.isBefore(_endDate);
    }).toList();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter Moment Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                ElevatedButton(
                  onPressed: () {
                    showDatePicker(
                      context: context,
                      initialDate: _startDate,
                      firstDate: DateTime.utc(2023),
                      lastDate: _endDate,
                    ).then((value) {
                      if (value != null) {
                        // _startDate = Moment.fromDateTime(value.toUtc());
                        _startDate = value.toMoment();
                        _filterDates();
                      }
                    });
                  },
                  child: const Text('Select Start Date'),
                ),
                ElevatedButton(
                  onPressed: () {
                    showDatePicker(
                      context: context,
                      initialDate: _endDate,
                      firstDate: DateTime.utc(2023),
                      lastDate: DateTime.utc(2023, 12, 31),
                    ).then((value) {
                      if (value != null) {
                        _endDate = value.toMoment();
                        _filterDates();
                      }
                    });
                  },
                  child: const Text('Choose End Date'),
                ),
              ],
            ),
            const SizedBox(height: 16.0),
            Text(
              'Starting date: ${_startDate.format("dd/MM/y")}',
              style: const TextStyle(fontSize: 16.0),
            ),
            const SizedBox(height: 8.0),
            Text(
              'End Date: ${_endDate.format("dd/MM/y")}',
              style: const TextStyle(fontSize: 16.0),
            ),
            const SizedBox(height: 16.0),
            Expanded(
              child: ListView.builder(
                itemCount: _filteredDates.length,
                itemBuilder: (context, index) {
                  return ListTile(
                    title: Text(_filteredDates[index]),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}

相关问题