我已经构建了DropDownmenuItem的代码,现在当我从DropDownmenuItem中单击一个项目时,它应该移动到另一个屏幕。下面是代码

class TimesScreen extends StatefulWidget {
  @override
  _TimesScreenState createState() => _TimesScreenState();
}

class _TimesScreenState extends State<TimesScreen> {
  var gender;
@override
  Widget build(BuildContext context) {
DropdownButton(
                     hint: Text("Select",
                     style: TextStyle(color: Colors.white),),
                     onChanged: (val){
                         setState(() {
                           this.gender=val;
                         });
                     },
                     value: this.gender,
                     items: [
                       DropdownMenuItem(
                         //onTap:
                         value: 'Earth',
                         child: Text('Earth'
                        ),
                       ),
                       DropdownMenuItem(
                        //onTap:
                         value: 'Mars',
                         child: Text('Mars'
                        ),
                       ),)]

最佳答案

您可以用具有可用于执行所需代码的Text函数的GestureDetector来包装您的onTap小部件。有关更多详细信息,请查看以下内容:https://api.flutter.dev/flutter/widgets/GestureDetector-class.html
这应该有效:

DropdownMenuItem(
    value: 'Earth',
    child: GestureDetector(
      onTap: () {
        // navigate code...
      },
      child: Text('Earth')
    ),
),

10-07 19:41
查看更多