dart 在flutter中,我有2个屏幕,在第二个屏幕上,我使用回调函数返回索引并将此值保存在第一个屏幕上

plupiseo  于 2023-10-13  发布在  Flutter
关注(0)|答案(2)|浏览(91)

我有两个屏幕。在第一个屏幕上,我正在调用列表视图构建器中的第二个屏幕。第二个屏幕有3个墨水池,在按下每个墨水池时,我调用一个回调函数来打开endDrawer,它出现在第一个屏幕上。这个endDrawer将根据按下的墨水池呈现不同的CustomScreens。在第二个屏幕上,我有一个功能:

final Function(int) onTap;

在第二个屏幕上,我有一个墨水池,在它的按钮上,我把这个函数称为:

InkWell(
                        onTap:(){
                          widget.onTap(2);
                          },
                        child: FittedBox(
                          fit: BoxFit.scaleDown,
                          child: Text(
                            "Zee Lounge",
                            style: TextStyle(
                              fontSize: 18,
                              color: Color(0xff0101AA),
                              decoration: TextDecoration.underline,
                            ),
                          ),
                        ),
                      ),

现在在第一个屏幕上,我想保存这个值到一个int变量,并在此基础上,我将打开不同的抽屉。我如何保存int值从第二屏幕上的第一个屏幕.
我的第一个屏幕是:

import 'package:feyst_fe_operations/widgets/custom_app_bar.dart';
import 'package:feyst_fe_operations/widgets/custom_container.dart';
import 'package:feyst_fe_operations/screens/experience_custom_screen.dart';
import 'package:feyst_fe_operations/widgets/custom_drop_down_button.dart';
import 'package:feyst_fe_operations/widgets/custom_table_header.dart';
import 'package:feyst_fe_operations/widgets/custom_table_row.dart';
import 'package:flutter/material.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

   static List<Color> customContainerColor = const [
    Color(0xffFBECCB),
     Color(0xffB0C18B),
     Color(0xffD1D1D1),
     Color(0xffF1C452),
     Color(0xffF89F84),
  ];

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {

  int savedValue = 0;

  void updateValue(int newValue) {
    setState(() {
      savedValue = newValue;
    });
    print("saved value is $savedValue");
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SingleChildScrollView(
        child: Column(
          children: [
            const CustomAppBar(),
            Padding(
              padding: const EdgeInsets.all(10),
              child:
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  for(int i=0; i<HomeScreen.customContainerColor.length; i++)
                  CustomContainer(containerColor: HomeScreen.customContainerColor[i],),
                ],
              ),
            ),
            const SizedBox(
              width:20,
            ),

         const CustomTableHeader(),
            ListView.builder(
              itemCount: 50,
              shrinkWrap: true,
              //scrollDirection: Axis.horizontal,
              itemBuilder: (context, index) {
                return  CustomTableRow(
                  onTap: (int buttonPressed){
                    Scaffold.of(context).openEndDrawer();
                  }
                );
              },
            )

          ],
        ),
      ),

      endDrawer: ExperienceCustomDrawer(),
    );
  }
}

我的第二个屏幕是:

import 'package:feyst_fe_operations/screens/experience_custom_screen.dart';
import 'package:feyst_fe_operations/widgets/my_custom_bottom_sheet_content.dart';
import 'package:flutter/material.dart';

class CustomTableRow extends StatefulWidget {
  CustomTableRow({super.key, required this.onTap});

  //final VoidCallback onTap;
  final Function(int) onTap;

  @override
  State<CustomTableRow> createState() => _CustomTableRowState();
}

class _CustomTableRowState extends State<CustomTableRow> {


  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            padding: const EdgeInsets.symmetric(vertical: 8.0),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(5),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Expanded(
                  flex: 1,
                  child: FittedBox(
                    fit: BoxFit.scaleDown,
                    child: Text(
                      "0160",
                      style: TextStyle(
                        color: Color(0xff393C46),
                        fontSize: 16,
                      ),
                    ),
                  ),
                ),
                Expanded(
                  flex: 3,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Flexible(
                        flex: 1,
                        child: Container(
                          padding: EdgeInsets.all(8.0),
                          decoration: BoxDecoration(
                            color:  Color(0xffFFEEE8),
                            borderRadius: BorderRadius.circular(5),
                          ),
                          child: SizedBox(
                            child: Image(
                              image:
                                  AssetImage("assets/images/dinnerTable.png"),
                            ),
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 4,
                        child: Padding(
                          padding: const EdgeInsets.only(left: 8.0),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              InkWell(
                                onTap: (){
                                  widget.onTap(1);
                                },

                                child: FittedBox(
                                  fit: BoxFit.scaleDown,
                                  child: Text(
                                    "Sea Food Experience",
                                    style: TextStyle(
                                      fontSize: 18,
                                      color: Color(0xff0101AA),
                                      decoration: TextDecoration.underline,
                                    ),
                                  ),
                                ),
                              ),
                              FittedBox(
                                fit: BoxFit.scaleDown,
                                child: Text(
                                  "Rawat, Islamabad",
                                  style: TextStyle(
                                    color: Color(0xff797E90),
                                    fontSize: 16,
                                    //fontWeight: FontWeight.bold,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  flex: 2,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      InkWell(
                        onTap:(){
                          widget.onTap(2);
                          },
                        child: FittedBox(
                          fit: BoxFit.scaleDown,
                          child: Text(
                            "Zee Lounge",
                            style: TextStyle(
                              fontSize: 18,
                              color: Color(0xff0101AA),
                              decoration: TextDecoration.underline,
                            ),
                          ),
                        ),
                      ),
                      FittedBox(
                        fit: BoxFit.scaleDown,
                        child: Text(
                          "0300 500 9000",
                          style: TextStyle(
                            color: Color(0xff797E90),
                            fontSize: 16,
                            //fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  flex: 2,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    //crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      InkWell(
                        onTap: () {
                          print("Foodie clicked");
                        },
                        child: FittedBox(
                          fit: BoxFit.scaleDown,
                          child: Text(
                            "Umer Mehmood",
                            style: TextStyle(
                              fontSize: 18,
                              color: Color(0xff0101AA),
                              decoration: TextDecoration.underline,
                            ),
                          ),
                        ),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Expanded(
                            flex: 1,
                            child: FittedBox(
                              fit: BoxFit.scaleDown,
                              child: Text(
                                "persons:",
                                style: TextStyle(
                                  color: Color(0xff797E90),
                                  fontSize: 16,
                                  //fontWeight: FontWeight.bold,
                                ),
                              ),
                            ),
                          ),
                          Expanded(
                            flex: 1,
                            child: FittedBox(
                              fit: BoxFit.scaleDown,
                              child: Text(
                                "04",
                                style: TextStyle(
                                  color: Color(0xff797E90),
                                  fontSize: 12,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
                Expanded(
                  flex: 2,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    //crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      InkWell(
                        onTap: () {},
                        child: FittedBox(
                          fit: BoxFit.scaleDown,
                          child: Text(
                            "Jazz Cash",
                            style: TextStyle(
                              fontSize: 16,
                              color: Color(0xff393C46),
                              //decoration: TextDecoration.underline,
                            ),
                          ),
                        ),
                      ),
                      FittedBox(
                        fit: BoxFit.scaleDown,
                        child: Text(
                          "3000",
                          style: TextStyle(
                            color: Color(0xff797E90),
                            fontSize: 16,
                            //fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  flex: 2,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    //crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      InkWell(
                        onTap: () {},
                        child: FittedBox(
                          fit: BoxFit.scaleDown,
                          child: Text(
                            "Jazz Cash",
                            style: TextStyle(
                              fontSize: 16,
                              color: Color(0xff393C46),
                              //decoration: TextDecoration.underline,
                            ),
                          ),
                        ),
                      ),
                      FittedBox(
                        fit: BoxFit.scaleDown,
                        child: Text(
                          "3000",
                          style: TextStyle(
                            color: Color(0xff797E90),
                            fontSize: 16,
                            //fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  flex: 2,
                  child: FittedBox(
                    fit: BoxFit.scaleDown,
                    child: Text(
                      "6000",
                      style: TextStyle(
                        color: Color(0xff393C46),
                        fontSize: 18,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ),
                Expanded(
                  flex: 2,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      InkWell(
                        onTap: () {},
                        child: FittedBox(
                          fit: BoxFit.scaleDown,
                          child: Text(
                            "28-01-23",
                            style: TextStyle(
                              fontSize: 16,
                              color: Color(0xff393C46),
                              //decoration: TextDecoration.underline,
                            ),
                          ),
                        ),
                      ),
                      FittedBox(
                        fit: BoxFit.scaleDown,
                        child: Text(
                          "02:30 PM",
                          style: TextStyle(
                            color: Color(0xff797E90),
                            fontSize: 16,
                            //fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  flex: 2,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      InkWell(
                        onTap: () {},
                        child: FittedBox(
                          fit: BoxFit.scaleDown,
                          child: Text(
                            "28-01-23",
                            style: TextStyle(
                              fontSize: 16,
                              color: Color(0xff393C46),
                              //decoration: TextDecoration.underline,
                            ),
                          ),
                        ),
                      ),
                      FittedBox(
                        fit: BoxFit.scaleDown,
                        child: Text(
                          "02:30 PM",
                          style: TextStyle(
                            color: Color(0xff797E90),
                            fontSize: 16,
                            //fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  flex: 2,
                  child: InkWell(
                    onTap: () {},
                    child: Container(
                      // height: 100,
                      padding: const EdgeInsets.symmetric(
                          horizontal: 12, vertical: 8),
                      decoration: BoxDecoration(
                        color: Color(0xffD1D1D1),
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: FittedBox(
                        fit: BoxFit.scaleDown,
                        child: Text(
                          "DECLINED",
                          style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          const Divider(
            color: Color(0xff707070),
            thickness: 0.5,
          ),
        ],
      ),
    );
  }
}
6ojccjat

6ojccjat1#

下面是从第二个屏幕到第一个屏幕获取索引的最简单方法。

class FirstScreen extends StatelessWidget {
  const FirstScreen({Key? key}) : super(key: key);

  void onClick(int index){
    print(index);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ElevatedButton(child:const Text('Go to Second Screen'),onPressed: (){
        Navigator.push(context, MaterialPageRoute(builder: (context) => SecondScreen(onClick: onClick,)));
      },),
    );
  }
}

class SecondScreen extends StatelessWidget {
  final Function onClick;
  const SecondScreen({Key? key, required this.onClick}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    int index=0;
    return Scaffold(
      body: ElevatedButton(child: const Text('Click to Send index to first screen'),onPressed: (){
        onClick(index++);
      },),
    );
  }
}
rqenqsqc

rqenqsqc2#

您已经可以从第二个屏幕上访问索引

onTap: (int buttonPressed){
      Scaffold.of(context).openEndDrawer();
    }

onTap是传递给第二个屏幕的回调函数。在onTap上,buttonPressed是您在第二个屏幕上调用的widget.onTap(2);提供的索引。这里,您将常量2传递给buttonPressed。根据buttonPressed的值,您可以打开抽屉或使用所获得的值做任何事情。每当在第二个屏幕上调用widget.onTap时,就会调用在第一个屏幕中定义的onTap函数。如果我理解正确的话,我认为没有其他问题。
我认为你应该花一些时间来掌握回调函数,因为它们很酷。

相关问题