英文:
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 '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]),
);
},
),
),
],
),
),
);
}
}
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_dart
的 version
。
下面是修复您的代码的完整示例:
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 '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]),
);
},
),
),
],
),
),
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论