颤振 - time_range_picker 包

flutter - time_range_picker package

我设法创建了一个函数(我将其放入 TextButton 中),您可以在其中看到 Time_range_picker 并选择范围。

我无法在文本小部件或按钮标签中显示结果。

class OpeningHours extends StatefulWidget {

  const OpeningHours({Key? key}) : super(key: key);
@override
  State<OpeningHours> createState() => _OpeningHoursState();
}

class _OpeningHoursState extends State<OpeningHours> {
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child: TextButton.icon(
            icon: const Icon(
              FontAwesomeIcons.penToSquare,
              color: Colors.white,
            ),
            label: Text(
              result.toString(),   // it does not work
              style: const TextStyle(
                color: Colors.white,
                fontSize: 22,
              ),
            ),
            style: TextButton.styleFrom(
                shadowColor: Colors.red,
                elevation: 18,
                backgroundColor: Colors.red),
            onPressed: () {
              pickTimeRange(context);
            },
          ),
      ),
    );
  }

  Future pickTimeRange(BuildContext context) async {
    final TimeRange? result = await showTimeRangePicker(context: context); 
      }
    }

首先你应该像这样初始化定时器值 TimeRange? resultForPrint; // add this line ,然后你应该使用 setSteate

更新这个值

请检查我的代码,它会工作

class Oeffnungszeiten extends StatefulWidget {

  const Oeffnungszeiten({Key? key}) : super(key: key);
@override
  State<Oeffnungszeiten> createState() => _OeffnungszeitenState();
}

class _OeffnungszeitenState extends State<Oeffnungszeiten> {

TimeRange? resultForPrint; // add this line

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child: TextButton.icon(
            icon: const Icon(
              FontAwesomeIcons.penToSquare,
              color: Colors.white,
            ),
            label: Text(

///////////////////////// add this code

              resultForPrint == null
              ? "Hi Friend"
             : "${resultForPrint!.startTime.format(context)} , ${resultForPrint!.endTime.format(context)}",

/////////////////////////

              style: const TextStyle(
                color: Colors.white,
                fontSize: 22,
              ),
            ),
            style: TextButton.styleFrom(
                shadowColor: Colors.red,
                elevation: 18,
                backgroundColor: Colors.red),
            onPressed: () {
              pickTimeRange(context);
            },
          ),
      ),
    );
  }

  Future pickTimeRange(BuildContext context) async {
    final TimeRange? result = await showTimeRangePicker(context: context); 
 
//////////////////////////// add this code

     setState(() {
              resultForPrint = result;
            });

/////////////////////////

      }
    }