flutter 下拉菜单值不变

1l5u6lss  于 2023-01-18  发布在  Flutter
关注(0)|答案(1)|浏览(198)

我的下拉菜单工作正常,但当我添加navigator.push时,在setstate()方法中,它在选择后没有改变值,位它确实推送了数据。
我需要推数据以及下拉菜单的值来改变.如果有一种方法来传递数据到另一个statfull类取决于下拉菜单的选择请启发我,
下面是我完整代码

import 'package:flutter/src/widgets/container.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

import '../widgets/My_wedgits.dart';

// shmalyh 3  LatLng(21.489191114466923, 39.24285294444855)
//shmalyh 1   LatLng( 21.490190928284374, 39.24029335794148)
//west 2   LatLng(21.489312801215913, 39.239637004938416)

//double lat = 21.48880614639443;
//double leng = 39.24159501940586;
 String dropdownvalue = "";

const List<String> gatelist = <String>[
  'NorthGate 1',
  'NorthGate 2',
  'WestGate 2'
];

class homepage extends StatefulWidget {
  //const homepage({super.key});
  double lat;
  double lan;

  homepage({Key? mykey, required this.lan, required this.lat})
      : super(key: mykey);

  @override
  State<homepage> createState() => _homepageState();
}

class _homepageState extends State<homepage> {
  DropdownButtonExample mydropdown = DropdownButtonExample();

  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          toolbarHeight: 60,
          centerTitle: true,
          title: const Text(
            'Wajeeh',
            style: TextStyle(
              fontSize: 28,
            ),
          ),
          elevation: 10,
          backgroundColor: const Color.fromARGB(255, 4, 105, 55),
          leading: Padding(
            padding: const EdgeInsets.all(6.0),
            child: Image.asset(
              'images/logo2.png',
              height: 30,
            ),
          ),
        ),
        backgroundColor: Colors.white,
        body: Column(
          children: [
            const SizedBox(
              height: 70,
            ),
            Row(
              // ignore: prefer_const_literals_to_create_immutables
              children: [
                Expanded(
                    child: Align(
                  alignment: Alignment.topCenter,
                  child: SizedBox(
                    width: 250,
                    child: mydropdown,
                  ),
                )),
              ],
            ),

            const SizedBox(
              height: 45,
            ),
            Row(
              children: [
                Expanded(
                    child: Column(
                  children: [
                    Image.asset('images/full.jpg'),
                    const Text(
                      'Total Parking',
                      style: TextStyle(
                          fontSize: 22.0,
                          fontWeight: FontWeight.bold,
                          color: Color.fromARGB(255, 0, 0, 0)),
                    ),
                    const Text(
                      '33',
                      style: TextStyle(
                          fontSize: 22.0,
                          fontWeight: FontWeight.bold,
                          color: Color.fromARGB(255, 4, 105, 55)),
                    ),
                  ],
                )),
                Expanded(
                    child: Column(
                  children: [
                    Image.asset('images/remain.jpg'),
                    const Text(
                      'Available Parking',
                      style: TextStyle(
                          fontSize: 22.0,
                          fontWeight: FontWeight.bold,
                          color: Color.fromARGB(255, 0, 0, 0)),
                    ),
                    const Text(
                      "100",
                      style: TextStyle(
                          fontSize: 22.0,
                          fontWeight: FontWeight.bold,
                          color: Color.fromARGB(255, 4, 105, 55)),
                    ),
                  ],
                ))
              ],
            ),
            //here where the map should be
            const SizedBox(
              height: 70,
            ),

            Row(
              // ignore: prefer_const_literals_to_create_immutables
              children: [
                Expanded(
                    child: Align(
                  alignment: Alignment.topCenter,
                  child: SizedBox(
                    width: 355,
                    height: 315,
                    child: Stack(
                      children: [
                        GoogleMap(
                          initialCameraPosition: CameraPosition(
                            target: LatLng(widget.lan, widget.lat),
                            zoom: 16,
                          ),
                        ),
                        Container(
                          alignment: Alignment.topLeft,
                          child: const Icon(
                            Icons.star,
                            color: Color.fromARGB(255, 231, 210, 23),
                            size: 40,
                          ),
                        ),
                      ],
                    ),
                  ),
                )),
              ],
            ),
          ],
        ));
  }

  // ignore: unused_element

}

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

  @override
  State<DropdownButtonExample> createState() => _DropdownButtonExampleState();

  /*Widget goodglemap() {
    return GoogleMap(
      initialCameraPosition: CameraPosition(
        target: LatLng(lat, leng),
        zoom: 16,
      ),
    );
  }*/
}

class _DropdownButtonExampleState extends State<DropdownButtonExample> {
  double lat = 21.48880614639443;
  double leng = 39.24159501940586;

  String dropdownValue = gatelist.first;
  // ignore: avoid_print

  @override
  Widget build(BuildContext context) {
    return DropdownButton<String>(
      hint: const Text("Choose a Gate"),
      isExpanded: true,
      value: dropdownValue,
      icon: const Icon(Icons.arrow_drop_down_sharp),
      style: const TextStyle(color: const Color.fromARGB(255, 4, 105, 55)),
      underline: Container(
        height: 2,
        color: const Color.fromARGB(255, 4, 105, 55),
      ),
      onChanged: (String? value) {
        // This is called when the user selects an item.
        setState(() {
          dropdownValue = value!;

//see if i can reach the other classs from here
          lat = 37.43296265331129;
          leng = -122.08832357078792;
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => homepage(lat: lat, lan: leng)));
        });
      },
      items: gatelist.map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(
            value,
          ),
        );
      }).toList(),
    );



  }



  Widget goodglemap() {
    return GoogleMap(
      initialCameraPosition: CameraPosition(
        target: LatLng(lat, leng),
        zoom: 16,
      ),
    );
  }
}
rdlzhqv9

rdlzhqv91#

我不确定我是否明白你的意思。我可以运行你的代码,第一页的下拉值被选中并显示,我可以导航到第二个屏幕(相同的屏幕布局),所以流程工作正常。但如果你的问题是:我们怎么能在多个屏幕上有相同的下拉值?那么答案是使用一个状态管理技术与一个库,像一个提供者或一个肘,所有你有是创建一个控制器然后所有的下拉窗口小部件应该听这个控制器,无论哪里有一个值的变化,控制器将通知所有的窗口小部件.请注意,提供者应该在一个父窗口小部件.

相关问题