Home > Enterprise >  how do i make get_measurements_page.dart as lading to show_measurements_page.dart?
how do i make get_measurements_page.dart as lading to show_measurements_page.dart?

Time:06-02

i want from user_detailo_page, when pressing the button. show get_measurements_page as loading to show_measurements_page.

here sample code UserDetailPage.

    import 'package:flutter/material.dart';
    import 'package:bodymeasure/raised_button_gradient.dart';
    import 'package:image_picker/image_picker.dart';
    import 'package:bodymeasure/get_measurements_page.dart';
    import 'package:bodymeasure/show_measurements_page.dart';
    import 'package:tflite_flutter/tflite_flutter.dart';
    
    class UserDetailPage extends StatefulWidget {
      static String tag = 'user-detail-page';
    
      const UserDetailPage({Key? key}) : super(key: key);
      @override
      _UserDetailPageState createState() => _UserDetailPageState();
      }

code get_measurements_page as loading.

import 'package:flutter/material.dart';

class GetMeasurementsPage extends StatefulWidget {
  static String tag = 'get-measurement-page';

  const GetMeasurementsPage({
    Key? key }) : super(key: key);
  @override
  _GetMeasurementsPageState createState() => _GetMeasurementsPageState();
}

code show_measurements_page.

import 'dart:convert';
import 'package:bodymeasure/history_page.dart';
import 'package:flutter/material.dart';

class ShowMeasurementsPage extends StatefulWidget {
  static String tag = 'show-measurements-page';
  final String? name;
  final String? nohap;
  final String? tinggi;
  const ShowMeasurementsPage({
    Key? key,
    this.name,
    this.nohap,
    this.tinggi,
  }) : super(key: key);
  @override
  _ShowMeasurementsPageState createState() => _ShowMeasurementsPageState();
}

I tried this code it worked but when I press the back button it's loading again.

onPressed: () async {
                setState(() => isLoading = true);
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => 
GetMeasurementsPage()));
                await Future.delayed(const Duration(seconds: 3));
                setState(() => isLoading = false);
                Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => ShowMeasurementsPage(
                              name: name.text,
                              nohap: nohap.text,
                              tinggi: tinggi.text),
                        ))
                    .whenComplete(() =>
                        {name.clear(), nohap.clear(), 
tinggi.clear()});
}

CodePudding user response:

Your navigation stack looks like this after your code runs:

UserDetailsPage --> GetMeasurementsPage --> ShowMeasurementsPage

So when you go back from the ShowMeasurementsPage, you will of course see the GetMeasurementsPage again. The simplest way to resolve this, is to push the ShowMeasurementsPage as a replacement:

Navigator.pushReplacement(
  context,
  MaterialPageRoute(...),
);

Also checkout the reference: https://api.flutter.dev/flutter/widgets/Navigator/pushReplacement.html

This should replace the GetMeasurementsPage with the ShowMeasurementsPage.

  • Related