繁体   English   中英

下拉菜单值不变

[英]dropdown menu value doesn't change

我的下拉菜单工作正常,但是当我添加 navigator.push 时,在 setstate() 方法中它在选择后没有更改值,它确实推送了数据。

我需要推送数据以及要更改的下拉菜单的值。 如果有一种方法可以根据下拉菜单的选择将数据传递给另一个 statfull class 请赐教,

这是我的完整代码

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,
      ),
    );
  }
}

我不确定我是否明白你的意思。 我能够运行你的代码,第一页上的下拉值被选中并显示,我能够导航到第二个屏幕(相同的屏幕布局),所以流程运行良好。 但如果您的问题是:我们如何才能在多个屏幕上拥有相同的下拉值? 那么答案是使用 state 管理技术和库,比如提供者或 cubit,你所要做的就是创建一个 controller 然后所有下拉小部件都应该听这个 controller,无论值有什么变化, controller 将通知所有小部件。 请注意,提供者应该在父窗口小部件中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM