DropdownButton 在包含列表的容器内。

huangapple go评论71阅读模式
英文:

DropdownButton inside a container with list

问题

我有一个问题。我想创建一个下拉菜单,用户可以从列表中选择某项。但不幸的是,有些东西丢失和错误。当我悬停在 _DropdownButtonExampleState 上时,它显示 缺少 'State.build' 的具体实现。 (文档) 尝试实现缺失的方法,或将类设为抽象类。。出了什么问题,如何解决这个错误?

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:convert';
import 'dart:io';

import '/FirstRoute.dart';

const List<String> list = ['One', 'Two', 'Three', 'Four'];

class DropdownButtonExample extends StatefulWidget {
  const DropdownButtonExample({Key? key}) : super(key: key);

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

class _DropdownButtonExampleState extends State<DropdownButtonExample> {
  String dropdownValue = list.first;
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Colors.black,
        systemOverlayStyle:
        const SystemUiOverlayStyle(statusBarColor: Colors.black),
        leading: IconButton(
          icon: Image.asset('assets/logo.png'),
          onPressed: null,
        ),
        title: Text("Welcome my app",
            style: const TextStyle(color: Colors.white),
            textAlign: TextAlign.center)
      ),
      body: Center(
        child: Container(
          child: Column(
            children: [
              const Text("Your ID",
                  style: TextStyle(
                      fontSize: 50.0,
                      fontWeight: FontWeight.w300,
                      color: Color(0XFF3F3D56),
                      height: 2.0)),
              const Text("Please enter your ID.",
                style: TextStyle(
                    color: Colors.grey,
                    letterSpacing: 1.2,
                    fontSize: 16.0,
                    height: 1.3),
                textAlign: TextAlign.center,
              ),
              DropdownButton<String>(
                value: dropdownValue,
                icon: const Icon(Icons.arrow_downward),
                elevation: 16,
                style: const TextStyle(color: Colors.deepPurple),
                underline: Container(
                  height: 2,
                  color: Colors.deepPurpleAccent,
                ),
                onChanged: (String? value) {
                  // This is called when the user selects an item.
                  setState(() {
                    dropdownValue = value!;
                  });
                },
                items: list.map<DropdownMenuItem<String>>((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value),
                  );
                }).toList(),
              ),
              ElevatedButton(
                child: const Text('Open route'),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => const FirstRoute()),
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    // 这里是 IDRoute 的构建方法
    // 你可以继续编辑它
  }
}
英文:

I have a problem. I want to create a dropdown where the user can select something from a list. But unfourtanly there is something missing and wrong. When I hover over _DropdownButtonExampleState
it say Missing concrete implementation of &#39;State.build&#39;. (Documentation) Try implementing the missing method, or make the class abstract.
What is wrong and how can I solve the error ?

import &#39;package:flutter/material.dart&#39;;
import &#39;package:flutter/services.dart&#39;;
import &#39;dart:convert&#39;;
import &#39;dart:io&#39;;

import &#39;/FirstRoute.dart&#39;;




const List&lt;String&gt; list = &lt;String&gt;[&#39;One&#39;, &#39;Two&#39;, &#39;Three&#39;, &#39;Four&#39;];

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

  @override
  State&lt;DropdownButtonExample&gt; createState() =&gt; _DropdownButtonExampleState();
}

class _DropdownButtonExampleState extends State&lt;DropdownButtonExample&gt; {
  String dropdownValue = list.first;
  

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

  
  
  
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          centerTitle: true,
          backgroundColor: Colors.black,
          systemOverlayStyle:
          const SystemUiOverlayStyle(statusBarColor: Colors.black),
          leading: IconButton(
            icon: Image.asset(&#39;assets/logo.png&#39;),
            onPressed: null,
          ),
          title: Text(&quot;Welcome my app&quot;,
              style: const TextStyle(color: Colors.white),
              textAlign: TextAlign.center)
      ),
      body: Center(
        child: Container(
          child: Column(
            children: [
              const Text(&quot;YourID&quot;,
                  style: TextStyle(
                      fontSize: 50.0,
                      fontWeight: FontWeight.w300,
                      color: Color(0XFF3F3D56),
                      height: 2.0)),
            const Text(&quot;Please enter your ID.&quot;,
              style: TextStyle(
                  color: Colors.grey,
                  letterSpacing: 1.2,
                  fontSize: 16.0,
                  height: 1.3),
              textAlign: TextAlign.center,
            ),
  DropdownButton&lt;String&gt;(
  value: dropdownValue,
  icon: const Icon(Icons.arrow_downward),
  elevation: 16,
  style: const TextStyle(color: Colors.deepPurple),
  underline: Container(
  height: 2,
  color: Colors.deepPurpleAccent,
  ),
  onChanged: (String? value) {
  // This is called when the user selects an item.
  setState(() {
  dropdownValue = value!;
  });
  },
  items: list.map&lt;DropdownMenuItem&lt;String&gt;&gt;((String value) {
  return DropdownMenuItem&lt;String&gt;(
  value: value,
  child: Text(value),
  );
  }).toList(),
  ),
              
              
              ElevatedButton(
                child: const Text(&#39;Open route&#39;),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) =&gt; const FirstRoute()),
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
}

答案1

得分: 1

你不能在一个类内部创建另一个类!我已经为您移除了它。复制这段代码,您将不再遇到错误:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:convert';
import 'dart:io';

const List<String> list = <String>['One', 'Two', 'Three', 'Four'];

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

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

class _DropdownButtonExampleState extends State<DropdownButtonExample> {
  String dropdownValue = list.first;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          centerTitle: true,
          backgroundColor: Colors.black,
          systemOverlayStyle: const SystemUiOverlayStyle(statusBarColor: Colors.black),
          leading: IconButton(
            icon: Image.asset('assets/logo.png'),
            onPressed: null,
          ),
          title: Text("Welcome my app", style: const TextStyle(color: Colors.white), textAlign: TextAlign.center)),
      body: Center(
        child: Container(
          child: Column(
            children: [
              const Text("YourID", style: TextStyle(fontSize: 50.0, fontWeight: FontWeight.w300, color: Color(0XFF3F3D56), height: 2.0)),
              const Text(
                "Please enter your ID.",
                style: TextStyle(color: Colors.grey, letterSpacing: 1.2, fontSize: 16.0, height: 1.3),
                textAlign: TextAlign.center,
              ),
              DropdownButton<String>(
                value: dropdownValue,
                icon: const Icon(Icons.arrow_downward),
                elevation: 16,
                style: const TextStyle(color: Colors.deepPurple),
                underline: Container(
                  height: 2,
                  color: Colors.deepPurpleAccent,
                ),
                onChanged: (String? value) {
                  // This is called when the user selects an item.
                  setState(() {
                    dropdownValue = value!;
                  });
                },
                items: list.map<DropdownMenuItem<String>>((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value),
                  );
                }).toList(),
              ),
              ElevatedButton(
                child: const Text('Open route'),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => const FirstRoute()),
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
英文:

You CAN NOT create a class within a class! I removed it for you. Copy this and you will not get the Error anymore:

import &#39;package:flutter/material.dart&#39;;
import &#39;package:flutter/material.dart&#39;;
import &#39;package:flutter/services.dart&#39;;
import &#39;dart:convert&#39;;
import &#39;dart:io&#39;;
const List&lt;String&gt; list = &lt;String&gt;[&#39;One&#39;, &#39;Two&#39;, &#39;Three&#39;, &#39;Four&#39;];
class DropdownButtonExample extends StatefulWidget {
const DropdownButtonExample({super.key});
@override
State&lt;DropdownButtonExample&gt; createState() =&gt; _DropdownButtonExampleState();
}
class _DropdownButtonExampleState extends State&lt;DropdownButtonExample&gt; {
String dropdownValue = list.first;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
backgroundColor: Colors.black,
systemOverlayStyle: const SystemUiOverlayStyle(statusBarColor: Colors.black),
leading: IconButton(
icon: Image.asset(&#39;assets/logo.png&#39;),
onPressed: null,
),
title: Text(&quot;Welcome my app&quot;, style: const TextStyle(color: Colors.white), textAlign: TextAlign.center)),
body: Center(
child: Container(
child: Column(
children: [
const Text(&quot;YourID&quot;, style: TextStyle(fontSize: 50.0, fontWeight: FontWeight.w300, color: Color(0XFF3F3D56), height: 2.0)),
const Text(
&quot;Please enter your ID.&quot;,
style: TextStyle(color: Colors.grey, letterSpacing: 1.2, fontSize: 16.0, height: 1.3),
textAlign: TextAlign.center,
),
DropdownButton&lt;String&gt;(
value: dropdownValue,
icon: const Icon(Icons.arrow_downward),
elevation: 16,
style: const TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String? value) {
// This is called when the user selects an item.
setState(() {
dropdownValue = value!;
});
},
items: list.map&lt;DropdownMenuItem&lt;String&gt;&gt;((String value) {
return DropdownMenuItem&lt;String&gt;(
value: value,
child: Text(value),
);
}).toList(),
),
ElevatedButton(
child: const Text(&#39;Open route&#39;),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) =&gt; const FirstRoute()),
);
},
),
],
),
),
),
);
}
}

huangapple
  • 本文由 发表于 2023年6月6日 15:04:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定