簡體   English   中英

go如何在Flutter返回刷新上一頁?

[英]How to go back and refresh the previous page in Flutter?

我有一個主頁,單擊該主頁時會通過導航將我帶到另一個頁面,在其中進行一些操作然后按后退按鈕將我帶回主頁。 但問題是主頁沒有得到刷新。

當我按下后退按鈕並刷新主頁時,有沒有辦法重新加載頁面?

當您像此偽代碼一樣導航回第一頁時,您可以觸發 API 調用

class PageOne extends StatefulWidget {
  @override
  _PageOneState createState() => new _PageOneState();
}

class _PageOneState extends State<PageOne> {
  _getRequests()async{

  }
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Center(
        child: new RaisedButton(onPressed: ()=>
        Navigator.of(context).push(new MaterialPageRoute(builder: (_)=>new PageTwo()),)
        .then((val)=>val?_getRequests():null),
      ),
    ));
  }
}

class PageTwo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //somewhere
    Navigator.pop(context,true);
  }
}

或者,如果 API 更新頻繁,您可以只使用流,新數據將在您的ListView內自動更新

例如,使用 firebase 我們可以做到這一點

stream: FirebaseDatabase.instance.reference().child(
      "profiles").onValue

並且無論何時您更改數據庫中的某些內容(例如從編輯個人資料頁面),它都會反映在您的個人資料頁面上。 在這種情況下,這只是可能的,因為我正在使用onValue它將繼續偵聽任何更改並代表您進行更新。

(在您的第一頁中):使用此代碼導航到第二頁。

Navigator.pushNamed(context, '/page2').then((_) {
  // This block runs when you have returned back to the 1st Page from 2nd.
  setState(() {
    // Call setState to refresh the page.
  });
});

(在您的第 2 頁):使用此代碼返回到第 1 頁。

Navigator.pop(context);

如果您使用 GetX :從 nextScreen 導航返回時使用結果,如下所示:

Get.back(result: 'hello');

並重新加載上一頁使用此功能:

void _navigateAndRefresh(BuildContext context) async {
    final result = await Get.to(()=>NextScreen());
    if(result != null){
      model.getEMR(''); // call your own function here to refresh screen
    }
  }

調用此函數而不是直接導航到 nextScreen

我只是使用這個:

onPressed: () {
          Navigator.pop(context,
              MaterialPageRoute(builder: (context) => SecondPage()));
        },

關閉當前頁面:

Navigator.pop

瀏覽上一頁:

MaterialPageRoute(builder: (context) => SecondPage())

在 FirtsPage 中,我添加了這個以在 startUpPage 上刷新:

 @override
  void initState() {
    //refresh the page here
    super.initState();
  }

我發現的最佳解決方案只是導航到上一頁:在getx

 return WillPopScope(
      onWillPop: () {
        Get.off(() => const PreviousPage());

        return Future.value(true);
      },
child: YourChildWidget(),

或者如果你想使用簡單的導航,那么:

 Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) =>PreviousPage() ,));

對於更細粒度、與頁面無關的解決方案,我想出了這個 Android Single LiveEvent模仿行為。

我在Provider類中創建了這樣的字段,例如:

SingleLiveEvent<int> currentYearConsumable = SingleLiveEvent<int>();

它有一個公共設置器來設置值。 公共consume允許您僅讀取一次值(如果存在)(請求 UI 刷新)。 在你需要的地方調用consume (比如在build方法中)。

您不需要Provider ,您可以使用其他解決方案來傳遞它。

執行:

/// Useful for page to page communication
/// Mimics Android SingleLiveEvent behaviour
/// https://stackoverflow.com/questions/51781176/is-singleliveevent-actually-part-of-the-android-architecture-components-library
class SingleLiveEvent<T> {
  late T _value;
  bool _consumed = true;

  set(T val) {
    _value = val;
    _consumed = false;
  }

  T? consume() {
    if (_consumed) {
      return null;
    } else {
      _consumed = true;
      return _value;
    }
  }
}

等待導航,然后調用 api 函數。

等待 Navigator.of(context).pop(); 等待 api 調用

您可以使用彈出路由時調用的簡單回調來執行此操作。 在下面的代碼示例中,它會在您彈出路由時調用。

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => new _HomePageState();
}

class _HomePageState extends State<HomePage> {
  _someFunction()async{
      Navigator.of(context).push(MaterialPageRoute(builder: (_)=> PageTwo(
         onClose():(){
            // Call setState here to rebuild this widget
            // or some function to refresh data on this page.
         }
     )));
   }

 @override
  Widget build(BuildContext context) {
     return SomeWidget();
 }
...
} // end of widget
class PageTwo extends StatelessWidget {
  final VoidCallback? onClose;
  PageTwo({Key? key, this.onClose}) : super(key: key);

  @override
  Widget build(BuildContext context) {
   
   return SomeWidget(
    onEvent():{ 
     Navigate.of(context).pop();
     onClose(); // call this wherever you are popping the route
   );
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM