Home > Software design >  How to send data without going to other screen in flutter pageview?
How to send data without going to other screen in flutter pageview?

Time:09-14

I want to send data from one page to other in pageview without going to other page I only want to save it somewhere and use this in other page.

Here is the HomeScreen Code :

class home extends StatefulWidget {


home({Key? key}) : super(key: key);



@override
  State<home> createState() => _homeState();
}

PageController controller = PageController();
int _index = 0;

class _homeState extends State<home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // extendBody: true,
      body: PageView(
        controller: controller,
        children: [
          page(
            controller: controller,
          ),
          codechef(),
          codeforce(sub: ""),
        ],
        onPageChanged: (page) {
          setState(() {
            _index = page;
          });
          controller.jumpToPage(_index);
        },
      ),
      bottomNavigationBar: Container(
        color: Color.fromARGB(255, 19, 63, 99),
        child: Padding(
          padding: const EdgeInsets.all(8),
          child: GNav(
            tabs: [
              GButton(
                icon: Icons.home,
                text: "Home",
              ),
              GButton(
                icon: Cp.codechef_svgrepo_com,
                text: "CodeChef",
              ),
              GButton(
                icon: Cp.codeforces_svgrepo_com,
                text: "CodeForces",
              )
            ],
            onTabChange: (index) {
              setState(() {
                _index = index;
              });
              controller.jumpToPage(index);
            },
            color: Colors.green,
            backgroundColor: Color.fromARGB(255, 22, 64, 98),
            activeColor: Colors.white,
            tabBackgroundColor: Color.fromARGB(255, 18, 44, 65),
            gap: 20,
            padding: EdgeInsets.all(20),
            rippleColor: Color.fromARGB(255, 51, 23, 23),
            selectedIndex: _index,
          ),
        ),
      ),
    );
  }
}

Here is the page() code:

import 'package:flutter/material.dart';

    class page extends StatefulWidget {
  PageController controller = PageController();
  page({Key? key, required this.controller}) : super(key: key);

  @override
  State<page> createState() => _pageState();
}

class _pageState extends State<page> {
  TextEditingController _textFieldController = TextEditingController();

  late String codeDialog;
  late String valueText;
  bool en = false;
  Future<void> _displayTextInputDialog(BuildContext context) {
    return showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            backgroundColor: Color.fromARGB(255, 55, 74, 90),
            title: Text(
              'UserName',
              style: TextStyle(color: Colors.white),
            ),
            content: TextFormField(
              onChanged: (value) {
                setState(() {
                  valueText = value;
                });
              },
              controller: _textFieldController,
              decoration: InputDecoration(
                hintText: "Enter a valid user name",
                hintStyle: TextStyle(color: Colors.white),
              ),
              style: TextStyle(
                color: Colors.white,
              ),
              validator: (Value) {
                if (Value!.isEmpty || Value == null) {
                  return "Enter User name";
                }
              },
            ),
            actions: <Widget>[
              ElevatedButton(
                child: Text(
                  'CANCEL',
                  style: TextStyle(color: Colors.white),
                ),
                onPressed: () {
                  setState(() {
                    Navigator.pop(context);
                  });
                },
                style: ElevatedButton.styleFrom(primary: Colors.red),
              ),
              ElevatedButton(
                child: Text(
                  'OK',
                  style: TextStyle(color: Colors.white),
                ),
                onPressed: () {
                  setState(() {
                    en = true;
                    codeDialog = valueText;
                    MaterialPageRoute(
                        builder: (context) => codeforce(sub: codeDialog));
                    controller.jumpToPage(2);

                    Navigator.pop(context);
                  });
                },
                style: ElevatedButton.styleFrom(primary: Colors.green),
              ),
            ],
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Container(
        child: Center(
          child: GridView(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2, crossAxisSpacing: 10),
            children: [
              Container(
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    color: Color.fromARGB(255, 97, 78, 78)),
                child: Column(
                  children: [
                    Icon(
                      Cp.codechef_svgrepo_com,
                      size: 100,
                    ),
                    Text("CodeChef",
                        style: TextStyle(
                          fontSize: 20,
                          fontWeight: FontWeight.bold,
                          color: Color.fromARGB(255, 166, 201, 229),
                        ))
                  ],
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  crossAxisAlignment: CrossAxisAlignment.center,
                ),
              ),
              InkWell(
                onTap: () async {
                  await _displayTextInputDialog(context);
                },
                child: Container(
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(10),
                      color: Color.fromARGB(255, 40, 83, 118)),
                  child: Column(
                    children: [
                      Icon(
                        Cp.codeforces_svgrepo_com,
                        size: 100,
                      ),
                      Text(
                        "CodeForces",
                        style: TextStyle(
                            fontSize: 20,
                            fontWeight: FontWeight.bold,
                            color: Color.fromARGB(255, 166, 201, 229)),
                      ),
                      en ? Text(codeDialog) : Text("")
                    ],
                    mainAxisAlignment: MainAxisAlignment.center,
                  ),
                ),
              )
            ],
            shrinkWrap: true,
          ),
        ),
      ),
    );
  }
}

Here I want when someone enter data and click on OK button then it should go to the codeforce page which take string. but when I am using Navigator.push() it will replace my screen but that I do not want. I want it to slide to codeforce page and write the data on it.

CodePudding user response:

Use Shared preferences plugin To save data in local device in the memory, you can see all details here: Shared preferences

CodePudding user response:

Use some state management in flutter, like: GetX...

CodePudding user response:

Use GetStoragehere Its very easy to use:

Writing values:

box.write('quote', 'GetX is the best');

Reading Values:

box.read('quote');

CodePudding user response:

Make the page widget take the callback function e.g. onSave:

final void Function(String data) onSave;

If the data you want to save is String. Otherwise use other type.

Then in the parent widget (home I think) use page like that:

page(
  onSave: (String data) {
    // Do your magic here (probably set some state)
  },
)

Also name classes from the big letter like this: Page. This is very important convention.

It's a quick fix but finally You want some state management like BLoC or GetX.

  • Related