即使我离开屏幕或关闭应用程序,我如何在一段时间后显示小部件

发布于 2025-01-21 21:22:38 字数 9870 浏览 0 评论 0原文

import 'dart:async';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:e_sante/variables.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';

class Cures extends StatefulWidget {

  @override
  State<Cures> createState() => _CuresState();
}

class _CuresState extends State<Cures> {
  bool visible=false;
  File ?image;
  Future pickimage() async{
    try{
      final image = await ImagePicker().getImage(source: ImageSource.gallery);
      if(image==null) return;
      final imageTemp = File(image.path);
      setState(() {
        this.image = imageTemp;
      });
    }on PlatformException catch(e){
      print('failed to pick image:$e');

    }

  }
  Future pickimagecam() async{
    try{
      final image = await ImagePicker().getImage(source: ImageSource.camera);
      if(image==null) return;
      final imageTemp = File(image.path);
      setState(() {
        this.image = imageTemp;
      });
    }on PlatformException catch(e){
      print('failed to pick image:$e');

    }

  }

  @override
  Widget build(BuildContext context) {
    double WidthScreen=MediaQuery.of(context).size.width;
    double HeightScreen=MediaQuery.of(context).size.height;
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.cyan[900],
        title: Text(
          'Cure',
          style: TextStyle(
            fontSize: 30,
          ),
        ),
        centerTitle: true,
        bottom: PreferredSize(
          preferredSize: Size.fromHeight(50),
          child: Padding(
            padding:  EdgeInsets.fromLTRB(0, 0, 40, 25),
            child: Container(

            ),
          ),

        ),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Padding(
              padding:  EdgeInsets.only(top: HeightScreen/20),
              child: Text(
                'Confirmer si vous avez passer votre cure',
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Card(
                  margin: EdgeInsets.only(top: HeightScreen/15),
                  shape: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.black,)
                  ),
                  child: Row(
                    children: [
                      Padding(
                        padding:  EdgeInsets.only(left: WidthScreen/30),
                        child: Text(
                          'Oui',
                          style: TextStyle(
                            fontSize: 16,
                          ),
                        ),
                      ),
                      SizedBox(width: 10,),
                      Radio<String>(
                        value: 'Oui',
                        groupValue: cure_val,
                        onChanged: (value){
                          setState(() {
                            cure_val=value!;
                          });

                        },
                      ),

                    ],
                  ),
                ),
                SizedBox(width: 30,),
                Card(
                  shape: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.black,)
                  ),
                  margin: EdgeInsets.only(top: HeightScreen/15),
                  child: Row(
                    children: [
                      Padding(
                        padding:  EdgeInsets.only(left: WidthScreen/30),
                        child: Text(
                          'Non',
                          style: TextStyle(
                            fontSize: 16,
                          ),

                        ),
                      ),
                      Radio<String>(
                        value: 'Non',
                        groupValue: cure_val,
                        onChanged: (value){
                          setState(() {
                            cure_val=value!;
                          });

                        },
                      ),
                    ],
                  ),

                ),
              ],
            ),
            SizedBox(height: 40,),
            ElevatedButton(
                onPressed: (){
                  if(cure_val=='Oui'){
                    Future.delayed(Duration(seconds: 5),(){
                      setState(() {
                        visible= !visible;
                      });

                    });
                    
                }else {
                   setState(() {
                     visible = false;
                   });
                  }
            },
                child: Text(
                  'Confirmer',
                  style: TextStyle(
                    fontSize: 20,
                  ),
                ),
            ),
            SizedBox(height: 30,),

      Visibility(
        visible:visible ,
        child:  Column(
          children: [
            Card(
              shape: OutlineInputBorder(
                  borderSide: BorderSide(color: Colors.black,),
                  borderRadius: BorderRadius.circular(10)
              ),
              child: Column(
                children: [
                  TextFormField(
                    keyboardType: TextInputType.text,
                    decoration: InputDecoration(
                        hintText: "Insérer votre bilan",
                        enabledBorder: InputBorder.none,
                        focusedBorder: InputBorder.none,
                        icon: IconButton(
                          icon:Icon(
                            Icons.camera_alt,
                            size: 18,
                          ),
                          onPressed: () {
                            showDialog(context: context,
                                builder: (context)=>AlertDialog(
                                  actions: [
                                    Column(
                                      children: [
                                        Row(
                                          children: [
                                            TextButton(
                                              child: Text(
                                                'Prendre une photo',
                                                style:TextStyle(
                                                  fontSize: 18,
                                                ),
                                              ),

                                              onPressed: () {
                                                pickimagecam();
                                              } ,
                                            ),
                                            SizedBox(width: 20,),
                                            Icon(
                                                Icons.camera_alt,
                                                size: 30,
                                            ),
                                          ],
                                        ),
                                        SizedBox(height: 10,),
                                        Row(
                                          children: [
                                            TextButton(
                                              onPressed: () {
                                                pickimage();
                                              },
                                              child: Text(
                                                'Telecharger une photo',
                                                style:TextStyle(
                                                  fontSize: 18,
                                                ) ,
                                              ),

                                            ),
                                            SizedBox(width: 20,),
                                            Icon(
                                                Icons.upload_rounded,
                                                size: 30,
                                            ),
                                          ],
                                        ),
                                      ],
                                    )
                                  ],

                                ),
                              barrierDismissible: true,
                            );
                          },

                        ),

                    ),
                  ),
      image !=null ?Image.file(image!):Text("Aucne image n'a été selectionné")
                ],
              ),



            ),
            SizedBox(height: 20,),
            ElevatedButton(
                onPressed: (){},
                child: Text(
                  'Envoyer',
                  style: TextStyle(
                    fontSize: 18,
                  ),
                ),
            )
          ],
        ),

      )
          ],

        ),
      ),
      

    );
  }
}

我想在一段时间后显示文本字段输入,但是问题是如果我离开屏幕或关闭应用程序,倒数停止计数时间,并且应用程序会生成错误。它只有在我留在同一屏幕上时起作用。 如果我选择选项“ OUI”并按时间后出现文本字段输入的按钮,但是如果我算出我按下按钮并离开屏幕或关闭应用程序倒数停止计数时间并生成错误

在我离开屏幕之前出现的错误

import 'dart:async';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:e_sante/variables.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';

class Cures extends StatefulWidget {

  @override
  State<Cures> createState() => _CuresState();
}

class _CuresState extends State<Cures> {
  bool visible=false;
  File ?image;
  Future pickimage() async{
    try{
      final image = await ImagePicker().getImage(source: ImageSource.gallery);
      if(image==null) return;
      final imageTemp = File(image.path);
      setState(() {
        this.image = imageTemp;
      });
    }on PlatformException catch(e){
      print('failed to pick image:$e');

    }

  }
  Future pickimagecam() async{
    try{
      final image = await ImagePicker().getImage(source: ImageSource.camera);
      if(image==null) return;
      final imageTemp = File(image.path);
      setState(() {
        this.image = imageTemp;
      });
    }on PlatformException catch(e){
      print('failed to pick image:$e');

    }

  }

  @override
  Widget build(BuildContext context) {
    double WidthScreen=MediaQuery.of(context).size.width;
    double HeightScreen=MediaQuery.of(context).size.height;
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.cyan[900],
        title: Text(
          'Cure',
          style: TextStyle(
            fontSize: 30,
          ),
        ),
        centerTitle: true,
        bottom: PreferredSize(
          preferredSize: Size.fromHeight(50),
          child: Padding(
            padding:  EdgeInsets.fromLTRB(0, 0, 40, 25),
            child: Container(

            ),
          ),

        ),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Padding(
              padding:  EdgeInsets.only(top: HeightScreen/20),
              child: Text(
                'Confirmer si vous avez passer votre cure',
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Card(
                  margin: EdgeInsets.only(top: HeightScreen/15),
                  shape: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.black,)
                  ),
                  child: Row(
                    children: [
                      Padding(
                        padding:  EdgeInsets.only(left: WidthScreen/30),
                        child: Text(
                          'Oui',
                          style: TextStyle(
                            fontSize: 16,
                          ),
                        ),
                      ),
                      SizedBox(width: 10,),
                      Radio<String>(
                        value: 'Oui',
                        groupValue: cure_val,
                        onChanged: (value){
                          setState(() {
                            cure_val=value!;
                          });

                        },
                      ),

                    ],
                  ),
                ),
                SizedBox(width: 30,),
                Card(
                  shape: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.black,)
                  ),
                  margin: EdgeInsets.only(top: HeightScreen/15),
                  child: Row(
                    children: [
                      Padding(
                        padding:  EdgeInsets.only(left: WidthScreen/30),
                        child: Text(
                          'Non',
                          style: TextStyle(
                            fontSize: 16,
                          ),

                        ),
                      ),
                      Radio<String>(
                        value: 'Non',
                        groupValue: cure_val,
                        onChanged: (value){
                          setState(() {
                            cure_val=value!;
                          });

                        },
                      ),
                    ],
                  ),

                ),
              ],
            ),
            SizedBox(height: 40,),
            ElevatedButton(
                onPressed: (){
                  if(cure_val=='Oui'){
                    Future.delayed(Duration(seconds: 5),(){
                      setState(() {
                        visible= !visible;
                      });

                    });
                    
                }else {
                   setState(() {
                     visible = false;
                   });
                  }
            },
                child: Text(
                  'Confirmer',
                  style: TextStyle(
                    fontSize: 20,
                  ),
                ),
            ),
            SizedBox(height: 30,),

      Visibility(
        visible:visible ,
        child:  Column(
          children: [
            Card(
              shape: OutlineInputBorder(
                  borderSide: BorderSide(color: Colors.black,),
                  borderRadius: BorderRadius.circular(10)
              ),
              child: Column(
                children: [
                  TextFormField(
                    keyboardType: TextInputType.text,
                    decoration: InputDecoration(
                        hintText: "Insérer votre bilan",
                        enabledBorder: InputBorder.none,
                        focusedBorder: InputBorder.none,
                        icon: IconButton(
                          icon:Icon(
                            Icons.camera_alt,
                            size: 18,
                          ),
                          onPressed: () {
                            showDialog(context: context,
                                builder: (context)=>AlertDialog(
                                  actions: [
                                    Column(
                                      children: [
                                        Row(
                                          children: [
                                            TextButton(
                                              child: Text(
                                                'Prendre une photo',
                                                style:TextStyle(
                                                  fontSize: 18,
                                                ),
                                              ),

                                              onPressed: () {
                                                pickimagecam();
                                              } ,
                                            ),
                                            SizedBox(width: 20,),
                                            Icon(
                                                Icons.camera_alt,
                                                size: 30,
                                            ),
                                          ],
                                        ),
                                        SizedBox(height: 10,),
                                        Row(
                                          children: [
                                            TextButton(
                                              onPressed: () {
                                                pickimage();
                                              },
                                              child: Text(
                                                'Telecharger une photo',
                                                style:TextStyle(
                                                  fontSize: 18,
                                                ) ,
                                              ),

                                            ),
                                            SizedBox(width: 20,),
                                            Icon(
                                                Icons.upload_rounded,
                                                size: 30,
                                            ),
                                          ],
                                        ),
                                      ],
                                    )
                                  ],

                                ),
                              barrierDismissible: true,
                            );
                          },

                        ),

                    ),
                  ),
      image !=null ?Image.file(image!):Text("Aucne image n'a été selectionné")
                ],
              ),



            ),
            SizedBox(height: 20,),
            ElevatedButton(
                onPressed: (){},
                child: Text(
                  'Envoyer',
                  style: TextStyle(
                    fontSize: 18,
                  ),
                ),
            )
          ],
        ),

      )
          ],

        ),
      ),
      

    );
  }
}

I want to display a textfield input after an amount of time but the problem is if I leave the screen or I close the app the countdown stop counting the time and the app generate an error . it works only if I stay in the same screen.
if I choose the option "Oui" and press the button a textfield input appears after an amount of time but if I press the button and leave the screen or close the app the countdown stop counting the time and generate an error

Error generated after I leave the screen before the time finishes

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文