flutter 尝试打印日期

s8vozzvw  于 2023-01-21  发布在  Flutter
关注(0)|答案(1)|浏览(145)

我试图在选择按钮后打印按钮内的日期,但每次我将DatatTime名称放入onpressed方法时,它都会给我一个错误。因此,我希望如果我单击按钮选择一个日期,它在按钮中可见。通过使用flutter:

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

class MyAppThree extends StatefulWidget {
  const MyAppThree({super.key});

  @override
  State<StatefulWidget> createState() => _MyAppThreeState();
}
class _MyAppThreeState extends State<StatefulWidget> {
  // ignore: unused_field
  late DateTime _selectedDate;
  void _dataPicker() {
    showDateRangePicker(
            context: context,
            firstDate: DateTime(2022),
            lastDate: DateTime.now())
        .then((value) {
      if (value == null) {
        return;
      }
      setState(() {
        _selectedDate = value as DateTime;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.black,
        title: const Text(''),
      ),
      body: Container(
          color: Colors.black,
          height: double.infinity,
          child: Column(
            children: <Widget>[
 child: Text(
                  "${DateFormat.yMMMd().format(_selectedDate)}",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 20,
                  ),
                ),
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(
                      Color.fromARGB(255, 55, 55, 55)),
                ),
              )
            ],
          )),
    );
  }
}
nimxete2

nimxete21#

MyAppThree的构造函数缺少参数名称。请尝试如下操作:

MyAppThree({Key key}) : super(key: key);

您还需要将Text小部件 Package 到容器中。

相关问题