英文:
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 'State.build'. (Documentation) Try implementing the missing method, or make the class abstract.
What is wrong and how can I solve the error ?
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:convert';
import 'dart:io';
import '/FirstRoute.dart';
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;
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('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()),
);
},
),
],
),
),
),
);
}
}
}
答案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 'package:flutter/material.dart';
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()),
);
},
),
],
),
),
),
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论