flutter -time_range_picker软件包

发布于 2025-01-17 15:02:08 字数 1240 浏览 3 评论 0原文

我设法创建了一个函数(我放入了文本键),您可以在其中看到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); 
      }
    }

I managed to create a function (which I put in a TextButton) where you can see the Time_range_picker and choose the range.

I just can't show the result either in a text widget or in the button label.

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); 
      }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单 2025-01-24 15:02:09

首先,您应该像这样初始化计时器值 TimeRange?结果打印; // 添加这一行,然后你应该使用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;
            });

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

      }
    }

first you should initialize the timer value like this TimeRange? resultForPrint; // add this line ,then you should update this value using setSteate

please check my code , it will work

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;
            });

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

      }
    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文