如何在警报对话框按钮上重新加载屏幕按下?

发布于 2025-01-22 05:21:40 字数 2422 浏览 0 评论 0原文

我正在制作一个应用程序,该应用显示来自子reddit的图像,我想根据通过警报对话框输入的子reddit名称更改这些图像。我已经实施了如下所有内容,但是当我设置状态时,图像没有显示。如何解决这个问题?这是我的代码。

import 'package:flutter/material.dart';
import 'package:memes/screens/memes_screen.dart';

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    )
  );
}

class HomePage extends StatefulWidget {
  const HomePage({ Key? key }) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  final TextEditingController textEditingController = TextEditingController();
  String subs = "memes";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromARGB(255, 255, 254, 241), //Color(0xffdedede),

      appBar: AppBar(title: Text("Memes"), backgroundColor: Color(0xff008b00)),

      body: MemesScreen(subreddits: subs),

      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),

        backgroundColor: Color(0xff008b00),

        onPressed: (){
          _showDialog();
        },
      ),
    );
  }


  void _showDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        // return object of type Dialog
        return AlertDialog(

          backgroundColor: Color.fromARGB(255, 255, 254, 241),

          title: Text("Enter subreddits"),

          content: TextField(
            controller: textEditingController,
          ),
          
          actions: <Widget>[
            // usually buttons at the bottom of the dialog
            TextButton(
              child: new Text("Close", style: TextStyle(color: Color(0xff008b00))),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),

            TextButton(
              child: new Text("Get", style: TextStyle(color: Color(0xff008b00))),
              onPressed: () {
                String newSubs = textEditingController.text;
                setState(() {
                  subs = newSubs;
                });
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }
}

字符串subs传递给显示图像的semesscreen。如果我更改此subs在按钮上的值按_showdialog方法中的方法并设置状态,则屏幕没有重新加载。如何解决这个问题?任何帮助将不胜感激。

I'm making an app that shows images from a subreddit and I want to change these images based on the subreddit names entered through the alert dialog. I have implemented everything as follows but when I set the state, the images are not showing. How to fix this? Here is my code.

import 'package:flutter/material.dart';
import 'package:memes/screens/memes_screen.dart';

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    )
  );
}

class HomePage extends StatefulWidget {
  const HomePage({ Key? key }) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  final TextEditingController textEditingController = TextEditingController();
  String subs = "memes";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromARGB(255, 255, 254, 241), //Color(0xffdedede),

      appBar: AppBar(title: Text("Memes"), backgroundColor: Color(0xff008b00)),

      body: MemesScreen(subreddits: subs),

      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),

        backgroundColor: Color(0xff008b00),

        onPressed: (){
          _showDialog();
        },
      ),
    );
  }


  void _showDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        // return object of type Dialog
        return AlertDialog(

          backgroundColor: Color.fromARGB(255, 255, 254, 241),

          title: Text("Enter subreddits"),

          content: TextField(
            controller: textEditingController,
          ),
          
          actions: <Widget>[
            // usually buttons at the bottom of the dialog
            TextButton(
              child: new Text("Close", style: TextStyle(color: Color(0xff008b00))),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),

            TextButton(
              child: new Text("Get", style: TextStyle(color: Color(0xff008b00))),
              onPressed: () {
                String newSubs = textEditingController.text;
                setState(() {
                  subs = newSubs;
                });
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }
}

The string subs is passed to the MemesScreen that shows an image. If I change this subs value on a button press in _showDialog method and set the state, the screen is not reloading. How to fix this? Any help would be highly appreciated.

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

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

发布评论

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

评论(1

甚是思念 2025-01-29 05:21:40

将semesscreen(subreddits:subs)包装到valuelistenablebuilder()中。然后将字符串subs变量放入Globals.dart页面,以便可以在SetState上更改它。
编辑您的代码如下

body: ValueListenableBuilder(
      valueListenable: refreshPage,
      builder: (context, value, child) {
        return MemesScreen(subreddits: subs);
      }),
     ......

对话框页面SETSTATE将是

  setState(() {
              subs = newSubs;
              refreshPage.value == 0
                  ? refreshPage.value = 1
                  : refreshPage.value = 0;
            });

Globals

import 'package:flutter/material.dart';

String subs = 'meme';
final refreshPage = ValueNotifier<int>(0);


main.dart

import 'package:answer_project/globals.dart';
import 'package:answer_project/meme_screen.dart';
import 'package:flutter/material.dart';

void main() {
 runApp(const MaterialApp(
 debugShowCheckedModeBanner: false,
 home: HomePage(),
 ));
 }

 class HomePage extends StatefulWidget {
 const HomePage({Key? key}) : super(key: key);

 @override
   State<HomePage> createState() => _HomePageState();
  }

  class _HomePageState extends State<HomePage> {
  final TextEditingController textEditingController = 
  TextEditingController();
  //String subs = "memes";

 @override
  Widget build(BuildContext context) {
  return Scaffold(
    backgroundColor:
      const Color.fromARGB(255, 255, 254, 241), //Color(0xffdedede),

  appBar: AppBar(
      title: const Text("Memes"), backgroundColor: const 
  Color(0xff008b00)),

  body: ValueListenableBuilder(
      valueListenable: refreshPage,
      builder: (context, value, child) {
        return MemesScreen(subreddits: subs);
      }),

  floatingActionButton: FloatingActionButton(
    child: const Icon(Icons.add),
    backgroundColor: const Color(0xff008b00),
    onPressed: () {
      _showDialog();
    },
  ),
  );
  }

  void _showDialog() {
  showDialog(
  context: context,
  builder: (BuildContext context) {
    // return object of type Dialog
    return AlertDialog(
      backgroundColor: const Color.fromARGB(255, 255, 254, 241),
      title: const Text("Enter subreddits"),
      content: TextField(
        controller: textEditingController,
      ),
      actions: <Widget>[
        // usually buttons at the bottom of the dialog
        TextButton(
          child: const Text("Close",
              style: TextStyle(color: Color(0xff008b00))),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),

        TextButton(
          child:
              const Text("Get", style: TextStyle(color: 
         Color(0xff008b00))),
          onPressed: () {
            String newSubs = textEditingController.text;
            setState(() {
              subs = newSubs;
              refreshPage.value == 0
                  ? refreshPage.value = 1
                  : refreshPage.value = 0;
            });
            Navigator.of(context).pop();
          },
        ),
      ],
    );
  },
  );
   }
  }

meme_screen.dart

  import 'package:flutter/material.dart';

class MemesScreen extends StatelessWidget {
const MemesScreen({Key? key, required this.subreddits}) : super(key: 
key);
final String subreddits;

@override
Widget build(BuildContext context) {
return Card(
  child: Text(
    subreddits,
  ),
);
}
}

globals.dart

  import 'package:flutter/material.dart';

  String subs = 'meme';

  final refreshPage = ValueNotifier<int>(0);

另一种刷新页面的方法是:
使用navigator.pushreplacement()代替navigator.of(context).pop()。

这是_showdialog()的代码:

void _showDialog() {
showDialog(
 context: context,
 builder: (BuildContext context) {
 // return object of type Dialog
 return AlertDialog(
  backgroundColor: const Color.fromARGB(255, 255, 254, 241),
  title: const Text("Enter subreddits"),
  content: TextField(
    controller: textEditingController,
  ),
  actions: <Widget>[
    // usually buttons at the bottom of the dialog
    TextButton(
      child: const Text("Close",
          style: TextStyle(color: Color(0xff008b00))),
      onPressed: () {
        Navigator.of(context).pop();
      },
    ),

    TextButton(
      child:
          const Text("Get", style: TextStyle(color: 
     Color(0xff008b00))),
      onPressed: () {
        String newSubs = textEditingController.text;
        setState(() {
          subs = newSubs;
          refreshPage.value == 0
              ? refreshPage.value = 1
              : refreshPage.value = 0;
        });
        //Navigator.of(context).pop();
        Navigator.pushReplacement(context, MaterialPageRoute(builder: 
        (BuildContext context) => HomePage()));
      },
    ),
  ],
  );
 },
 );
  }

它将重新加载主页。但是最后一个堆栈丢失意味着您不能回到最后一页。

Wrap MemesScreen(subreddits: subs) INTO ValueListenableBuilder(). Then Put String subs variable in globals.dart page so that it can be changed on setState.
Edit your Code as below

body: ValueListenableBuilder(
      valueListenable: refreshPage,
      builder: (context, value, child) {
        return MemesScreen(subreddits: subs);
      }),
     ......

Dialog page setState will be

  setState(() {
              subs = newSubs;
              refreshPage.value == 0
                  ? refreshPage.value = 1
                  : refreshPage.value = 0;
            });

globals.dart will be

import 'package:flutter/material.dart';

String subs = 'meme';
final refreshPage = ValueNotifier<int>(0);

Check this full code working and tested:
main.dart

import 'package:answer_project/globals.dart';
import 'package:answer_project/meme_screen.dart';
import 'package:flutter/material.dart';

void main() {
 runApp(const MaterialApp(
 debugShowCheckedModeBanner: false,
 home: HomePage(),
 ));
 }

 class HomePage extends StatefulWidget {
 const HomePage({Key? key}) : super(key: key);

 @override
   State<HomePage> createState() => _HomePageState();
  }

  class _HomePageState extends State<HomePage> {
  final TextEditingController textEditingController = 
  TextEditingController();
  //String subs = "memes";

 @override
  Widget build(BuildContext context) {
  return Scaffold(
    backgroundColor:
      const Color.fromARGB(255, 255, 254, 241), //Color(0xffdedede),

  appBar: AppBar(
      title: const Text("Memes"), backgroundColor: const 
  Color(0xff008b00)),

  body: ValueListenableBuilder(
      valueListenable: refreshPage,
      builder: (context, value, child) {
        return MemesScreen(subreddits: subs);
      }),

  floatingActionButton: FloatingActionButton(
    child: const Icon(Icons.add),
    backgroundColor: const Color(0xff008b00),
    onPressed: () {
      _showDialog();
    },
  ),
  );
  }

  void _showDialog() {
  showDialog(
  context: context,
  builder: (BuildContext context) {
    // return object of type Dialog
    return AlertDialog(
      backgroundColor: const Color.fromARGB(255, 255, 254, 241),
      title: const Text("Enter subreddits"),
      content: TextField(
        controller: textEditingController,
      ),
      actions: <Widget>[
        // usually buttons at the bottom of the dialog
        TextButton(
          child: const Text("Close",
              style: TextStyle(color: Color(0xff008b00))),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),

        TextButton(
          child:
              const Text("Get", style: TextStyle(color: 
         Color(0xff008b00))),
          onPressed: () {
            String newSubs = textEditingController.text;
            setState(() {
              subs = newSubs;
              refreshPage.value == 0
                  ? refreshPage.value = 1
                  : refreshPage.value = 0;
            });
            Navigator.of(context).pop();
          },
        ),
      ],
    );
  },
  );
   }
  }

meme_screen.dart

  import 'package:flutter/material.dart';

class MemesScreen extends StatelessWidget {
const MemesScreen({Key? key, required this.subreddits}) : super(key: 
key);
final String subreddits;

@override
Widget build(BuildContext context) {
return Card(
  child: Text(
    subreddits,
  ),
);
}
}

globals.dart

  import 'package:flutter/material.dart';

  String subs = 'meme';

  final refreshPage = ValueNotifier<int>(0);

Another method of refreshing page is:
use Navigator.pushReplacement() instead of Navigator.of(context).pop().

This is the code of _showDialog():

void _showDialog() {
showDialog(
 context: context,
 builder: (BuildContext context) {
 // return object of type Dialog
 return AlertDialog(
  backgroundColor: const Color.fromARGB(255, 255, 254, 241),
  title: const Text("Enter subreddits"),
  content: TextField(
    controller: textEditingController,
  ),
  actions: <Widget>[
    // usually buttons at the bottom of the dialog
    TextButton(
      child: const Text("Close",
          style: TextStyle(color: Color(0xff008b00))),
      onPressed: () {
        Navigator.of(context).pop();
      },
    ),

    TextButton(
      child:
          const Text("Get", style: TextStyle(color: 
     Color(0xff008b00))),
      onPressed: () {
        String newSubs = textEditingController.text;
        setState(() {
          subs = newSubs;
          refreshPage.value == 0
              ? refreshPage.value = 1
              : refreshPage.value = 0;
        });
        //Navigator.of(context).pop();
        Navigator.pushReplacement(context, MaterialPageRoute(builder: 
        (BuildContext context) => HomePage()));
      },
    ),
  ],
  );
 },
 );
  }

It will reload the HomePage. But the last stack was lost means you cannot go back on last page.

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