![](/img/trans.png)
[英]How to have different color for dropdown label and dropdown list text in Flutter?
[英]I have an issue saving a different list on multiple dropdown button in Flutter
我是 Flutter 的菜鳥,畢竟是編程。 我正在完成我作為土木工程師畢業的最后一個項目,但我決定編寫一個適用於 Android 的應用程序,用於計算用於微樁設計的各種方程。
我已經完成了模擬披薩訂單的代碼(這對解釋變得更加友好),但是我在保存不同列表中的值時遇到問題,這些列表會根據披薩素食或肉類而變化,我仍在努力。
我可以達到這樣做的方法或表格,請有人解釋或幫助我。
到目前為止的代碼:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
//Provider
class Datos with ChangeNotifier {
List _datos = [' '];
List get datos => _datos;
set datos(List value){
_datos = value;
notifyListeners();
}
void goVegan () {
_datos = [' ','Vegan 1', 'Vegan 2', 'Vegan 3'];
notifyListeners();
}
void goMeat () {
_datos = [' ', 'Meat 1', 'Meat 2', 'Meat 3'];
notifyListeners();
}
}
class lechada extends StatelessWidget {
// const lechada({Key? key}) : super(key: key);
int nes = 0;
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<Datos>(
create: (context) => Datos(),
child: Scaffold(
appBar: AppBar(
title: const Text('definitivo'),
),
body:Column(
children: [
entrada('cantidad de Pizzas'),
cuerpo(),//nes),
],
),
),
);
}
//inputs de formatos
entrada(String hint){
return TextFormField(
validator: (String? value) { //Esta valida que no este vacio
if (value == null || value.isEmpty) {
return 'Campo requerido'; //campo en caso de que este vacio
}
return null;
},
onSaved: (value) => nes = int.parse(value!),//eje.add(double.parse(value!)), //Aca guarda el valor
decoration: InputDecoration(
hintText: hint,
// icon: Icon(Icons.calendar_view_day_sharp, color: Color(0xFFB14246))
),
keyboardType: TextInputType.number,
);
}
}
class cuerpo extends StatefulWidget {
// const cuerpo({Key? key}) : super(key: key);
int nes = 3;
// cuerpo (int nes);
@override
State<cuerpo> createState() => _cuerpoState();
}
class _cuerpoState extends State<cuerpo> {
// List selec = [' ','Pizza Vegan','Pizza Meat'];
String initial = ' ';
String initial2 = ' ';
@override
Widget build(BuildContext context) {
final datos = Provider.of<Datos>(context,listen: false);
return Column(
children: [
ListView.builder(
shrinkWrap: true,
itemCount: widget.nes ,
itemBuilder: (BuildContext context, int index) => datosPizza(),
// {
// return datosPizza();
// },
),
SizedBox(height: 20),
Container(
padding: EdgeInsets.all(8),
child: Text('selec.toString()'),
),
SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(onPressed: (){
setState(() {
initial = ' ';
datos.goVegan();
});
}, child: Text('Go Vegan')),
ElevatedButton(onPressed: (){
setState(() {
initial = ' ';
datos.goMeat();
});
}, child: Text('Go Meat')),
],
)
],
);
}
}
class datosPizza extends StatefulWidget {
// const datosPizza({Key? key}) : super(key: key);
@override
_datosPizzaState createState() => _datosPizzaState();
}
class _datosPizzaState extends State<datosPizza> {
List selec = [' ','Pizza Vegan','Pizza Meat'];
String initial = ' ';
String initial2 = ' ';
@override
Widget build(BuildContext context) {
final datos = Provider.of<Datos>(context,listen: false);
return Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Container(
padding: EdgeInsets.all(8),
child: DropdownButton(
value: initial,
//Aca recibe los valores y los pasa al map
items: selec.map((value){ //datos se mete al list
return DropdownMenuItem(
child: new Text(value),
value:value,
);
}).toList(),
onChanged: (value) {
value.toString() == 'Pizza Vegan' ?datos.goVegan() :null;
value.toString() == 'Pizza Meat' ?datos.goMeat() :null;
initial2 = ' ';
setState(() {
initial = value.toString();
});
})
),
Container(
padding: EdgeInsets.all(8),
child: DropdownButton(
value: initial2,
//Aca recibe los valores y los pasa al map
items: datos.datos.map((value){ //datos se mete al list
return DropdownMenuItem(
child: new Text(value),
value:value,
);
}).toList(),
onChanged: (value) {
setState(() {
initial2 = value.toString();
});
})
),
],
);
}
}
請不要責怪我的英語,我是西班牙裔母語人士
更新:我找到了問題的解決方案,簡化是為主要樹小部件創建更多的孩子,並繼承這些值,所以小部件將保存我們需要的這些值,我將分享代碼示例:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
//Provider
class Datos with ChangeNotifier {
List _datos = [' '];
List guardado = [];
List gg = [];
List<String> esgua = [];
List<double> algua = [];
List<double> zgua = [];
// List guardado = [' '];
int p = 0;
List get datos => _datos;
set datos(List value){
_datos = value;
notifyListeners();
}
void goVegan () {
_datos = [' ','Vegan 1', 'Vegan 2', 'Vegan 3'];
// p = 0;
notifyListeners();
}
void goMeat () {
_datos = [' ', 'Meat 1', 'Meat 2', 'Meat 3'];
// p = 1;
notifyListeners();
}
void plus () {
p++;
notifyListeners();
}
}
class lechada extends StatelessWidget {
// const lechada({Key? key}) : super(key: key);
// int nes = 0;
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<Datos>(
create: (context) => Datos(),
child: Scaffold(
appBar: AppBar(
title: const Text('definitivo'),
),
body:Column(
children: [
Text('Toma pedido'),
cuerpo(),
],
),
),
);
}
}
class cuerpo extends StatefulWidget {
// const cuerpo({Key? key}) : super(key: key);
@override
State<cuerpo> createState() => _cuerpoState();
}
class _cuerpoState extends State<cuerpo> {
// List selec = [' ','Pizza Vegan','Pizza Meat'];
String initial = ' ';
String initial2 = ' ';
// GlobalKey<FormState> dkey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
final datos = Provider.of<Datos>(context);
return Column(
children: [
ListView.builder(
shrinkWrap: true,
itemCount: datos.p,
itemBuilder: (BuildContext context, int index) => datosPizza(),
// {
// return datosPizza();
// },
),
SizedBox(height: 20),
Container(
padding: EdgeInsets.all(8),
child: Text('selec.toString()'),
),
SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
onPressed: (){
setState(() {
print (datos.gg);
datos.p!=datos.gg.length
?print ('error')
:null;
});
}, child: Text('Print')
),
ElevatedButton(
onPressed: (){
datos.p = 0;
datos.gg.clear();
},
child: Text('Reinicia')),
ElevatedButton(
onPressed: (){
datos.plus();
},
child: Text('Add estrato')
),
],
)
],
);
}
}
class datosPizza extends StatefulWidget {
const datosPizza({Key? key}) : super(key: key);
@override
_datosPizzaState createState() => _datosPizzaState();
}
class _datosPizzaState extends State<datosPizza> {
List selec = [' ','Pizza Vegan','Pizza Meat'];
String initial = ' ';
String initial2 = ' ';
List temp = [];
List<double> eje = [];
@override
Widget build(BuildContext context) {
final datos = Provider.of<Datos>(context,listen: false);
return Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Container(
width: 150,
padding: EdgeInsets.all(8),
child: DropdownButton(
value: initial,
//Aca recibe los valores y los pasa al map
items: selec.map((value){ //datos se mete al list
return DropdownMenuItem(
child: new Text(value),
value:value,
);
}).toList(),
onChanged: (value) {
value.toString() == 'Pizza Vegan' ?datos.goVegan() :null;
value.toString() == 'Pizza Meat' ?datos.goMeat() :null;
initial2 = ' ';
temp.addAll(datos._datos); //Añade los datos de entrada y los pasa al widget para que los conserve
setState(() {
initial = value.toString();
});
})
),
Container(
width: 100,
padding: EdgeInsets.all(8),
child: dropdowBut(temp),
),
Container(
width: 70,
padding: EdgeInsets.all(8),
child: entrada('mts'),
)
],
);
}
//inputs de formatos
entrada(String hint){
return TextFormField(
validator: (String? value) { //Esta valida que no este vacio
if (value == null || value.isEmpty) {
return 'Campo requerido'; //campo en caso de que este vacio
}
return null;
},
onSaved: (value) => eje.add(double.parse(value!)),//eje.add(double.parse(value!)), //Aca guarda el valor
onFieldSubmitted: (value) => print(value),
// onChanged: (value)=> print(value),
decoration: InputDecoration(
hintText: hint,
// icon: Icon(Icons.calendar_view_day_sharp, color: Color(0xFFB14246))
),
keyboardType: TextInputType.number,
);
}
}
class dropdowBut extends StatefulWidget {
// const dropdowBut({Key? key}) : super(key: key);
String initial2 =' ';
List temp ;
dropdowBut(this.temp);
@override
_dropdowButState createState() => _dropdowButState();
}
class _dropdowButState extends State<dropdowBut> {
String _value = ' ';
@override
Widget build(BuildContext context) {
final datos = Provider.of<Datos>(context,listen: false);
return DropdownButton(
// value: widget.initial2,
value: _value,
// hint: Text('hi'),
//Aca recibe los valores y los pasa al map
items:
widget.temp.map((value){ //datos se mete al list
return DropdownMenuItem(
child: new Text(value),
value:value,
);
}).toList(),
onChanged: (value) {
datos.gg.add(value);
setState(() {
_value = value.toString();
});
});
}
}
對不起,如果我糾結了代碼,但我還在學習 Flutter。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.