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

huangapple go评论66阅读模式
英文:

Flutter app that demonstrates the use of the moment_dart library for manipulating and formatting dates and times in Flutter

问题

以下是您提供的代码的翻译部分:

我正在开发一个Flutter应用程序,用于演示在Flutter中操作和格式化日期和时间的moment_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 = 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_dart库时出现问题,可能是库版本不兼容或者缺少必要的导入。确保您的Flutter项目中已经正确导入了moment_dart库,并且您使用的是支持Moment类的版本。如果问题仍然存在,您可以尝试更新库或查阅库的文档以获取更多信息。

英文:

I'm working on a Flutter app that demonstrates the use of the moment_dart library for manipulating and formatting dates and times in Flutter.

import &#39;package:flutter/material.dart&#39;;
import &#39;package:moment_dart/moment_dart.dart&#39;;
void main() =&gt; runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: &#39;Flutter Moment Demo&#39;,
home: MomentDemo(),
);
}
}
class MomentDemo extends StatefulWidget {
@override
_MomentDemoState createState() =&gt; _MomentDemoState();
}
class _MomentDemoState extends State&lt;MomentDemo&gt; {
Moment _startDate = Moment.utc([2023, 1, 1]);
Moment _endDate = Moment.utc([2023, 2, 28]);
List&lt;String&gt; _dates = [    &#39;2023-01-01&#39;,    &#39;2023-01-08&#39;,    &#39;2023-01-15&#39;,    &#39;2023-01-22&#39;,    &#39;2023-01-29&#39;,    &#39;2023-02-05&#39;,    &#39;2023-02-12&#39;,    &#39;2023-02-19&#39;,    &#39;2023-02-26&#39;,    &#39;2023-03-05&#39;,  ];
List&lt;String&gt; _filteredDates = [];
void _filterDates() {
_filteredDates = _dates.where((date) {
Moment moment = Moment.parse(date);
return moment.isAfter(_startDate) &amp;&amp; moment.isBefore(_endDate);
}).toList();
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(&#39;Flutter Moment Demo&#39;),
),
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(&#39;Başlangı&#231; Tarihi Se&#231;&#39;),
),
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(&#39;Bitiş Tarihi Se&#231;&#39;),
),
],
),
SizedBox(height: 16.0),
Text(
&#39;Başlangı&#231; Tarihi: ${_startDate.format(&quot;dd/MM/yyyy&quot;)}&#39;,
style: TextStyle(fontSize: 16.0),
),
SizedBox(height: 8.0),
Text(
&#39;Bitiş Tarihi: ${_endDate.format(&quot;dd/MM/yyyy&quot;)}&#39;,
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]),
);
},
),
),
],
),
),
);
}
}

In this code, I am getting the errors I wrote below and i'm asking for help. Thank you all.

  • The method 'utc' isn't defined for the type 'Moment'.
  • The method 'toDateTime' isn't defined for the type 'Moment'.
  • The method 'fromDateTime' isn't defined for the type 'Moment'.

答案1

得分: 0

确保您使用最新的 moment_dartversion

下面是修复您的代码的完整示例:

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 _endDate = DateTime(2023, 1, 1).toMoment();

  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 = 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]),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}

如果您需要进一步的帮助,请告诉我。

英文:

make sure you use the latest version of moment_dart

Below is the full example of fixed your code:

import &#39;package:flutter/material.dart&#39;;
import &#39;package:moment_dart/moment_dart.dart&#39;;
void main() =&gt; runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: &#39;Flutter Moment Demo&#39;,
home: MomentDemo(),
);
}
}
class MomentDemo extends StatefulWidget {
@override
_MomentDemoState createState() =&gt; _MomentDemoState();
}
class _MomentDemoState extends State&lt;MomentDemo&gt; {
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&lt;String&gt; _dates = [
&#39;2023-01-01&#39;,
&#39;2023-01-08&#39;,
&#39;2023-01-15&#39;,
&#39;2023-01-22&#39;,
&#39;2023-01-29&#39;,
&#39;2023-02-05&#39;,
&#39;2023-02-12&#39;,
&#39;2023-02-19&#39;,
&#39;2023-02-26&#39;,
&#39;2023-03-05&#39;,
];
List&lt;String&gt; _filteredDates = [];
void _filterDates() {
_filteredDates = _dates.where((date) {
Moment moment = Moment.parse(date);
return moment.isAfter(_startDate) &amp;&amp; moment.isBefore(_endDate);
}).toList();
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(&#39;Flutter Moment Demo&#39;),
),
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(&#39;Select Start Date&#39;),
),
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(&#39;Choose End Date&#39;),
),
],
),
const SizedBox(height: 16.0),
Text(
&#39;Starting date: ${_startDate.format(&quot;dd/MM/y&quot;)}&#39;,
style: const TextStyle(fontSize: 16.0),
),
const SizedBox(height: 8.0),
Text(
&#39;End Date: ${_endDate.format(&quot;dd/MM/y&quot;)}&#39;,
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]),
);
},
),
),
],
),
),
);
}
}

huangapple
  • 本文由 发表于 2023年2月27日 16:47:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578353.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定