Ad

Dart/Flutter: How To Create Loop Of 600 Days, With DateTime Variable And Return In Text Widget?

- 1 answer

User writes a date for example 19/11/2021 and flutter print in Text Widget a List of days that finish after 600 days (19/11/2021 + 600).

Ad

Answer

Here is the whole code which will print all day for next 600 days.

class MyApp extends StatelessWidget {
  final DateTime userDate = DateTime.now();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: darkBlue,
      ),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: ListView.builder(
            itemCount: 600,
            itemBuilder: (_, index) {
                return Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Text('${userDate.add(Duration(days: index)).day}'),
                );
            },
          )
        ),
      ),
    );
  }
}

For GridView:

Center(
      child: GridView.count(
        crossAxisCount: 2,
        children: List.generate(600, (index) {
              return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text('${userDate.add(Duration(days: index)).day}'),
              );
          }
        )
      )
    ),

You can directly check on https://dartpad.dartlang.org/

Ad
source: stackoverflow.com
Ad