从无状态小部件类调用有状态小部件的问题..我创建了无状态小部件类 MyApp,其中绘制了图像滑块(有 4 个图像),单独使用此图像滑块可以正常工作,但是当我尝试连接有状态小部件类 DropDown 时出现问题。 ..如何从 MyApp 类调用 DropDown 类。有人帮忙.pl.
import 'package:flutter/material.dart';
import "dart:convert";
import "package:flutter_linkify/flutter_linkify.dart";
import "package:url_launcher/url_launcher.dart";
import 'package:flutter_image_slideshow/flutter_image_slideshow.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body:
ImageSlideshow(
width: double.infinity,
height: 200,
initialPage: 0,
indicatorColor: Colors.blue,
indicatorBackgroundColor: Colors.grey,
children: [
Image.asset(
'assets/muktagiri.jpg',
fit: BoxFit.cover,
),
Image.asset(
'assets/maatapti.jpg',
fit: BoxFit.cover,
),
Image.asset(
'assets/kukru.jpg',
fit: BoxFit.cover,
),
Image.asset(
'assets/gupteshwar.jpg',
fit: BoxFit.cover,
),
],
onPageChanged: (value) {
print('Page changed: $value');
},
),
),
);
}
}
class DropDown extends StatefulWidget{
@override
DropDownState createState() => DropDownState();
}
class Item {
int id;
String name;
Item(this.id, this.name);
static List<Item> getDeparment() {
return <Item>[
Item(1, 'Social Justice'),
Item(2, 'NIC'),
Item(3, 'Education'),
Item(4, 'Food'),
];
}
}
//class _HomeState extends State<State> {
class DropDownState extends State<DropDown> {
int _value = 1;
int index=0;
List<Item> _dept = Item.getDeparment();
List<DropdownMenuItem<Item>> _dropdownMenuItems;
Item _selectedDept;
// Company _selectedCompany;
@override
void initState() {
_dropdownMenuItems = buildDropdownMenuItems(_dept);
_selectedDept = _dropdownMenuItems[0].value;
super.initState();
}
List<DropdownMenuItem<Item>> buildDropdownMenuItems(List companies) {
List<DropdownMenuItem<Item>> items = List();
for (Item department in _dept) {
items.add(
DropdownMenuItem(
value: department,
child: Text(department.name),
),
);
}
return items;
}
onChangeDropdownItem(Item selectedItem) {
setState(() {
_selectedDept = selectedItem;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: new Container(
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Select Department"),
DropdownButton(
value: _selectedDept,
items: _dropdownMenuItems,
onChanged: onChangeDropdownItem,
),
SizedBox(
height: 80.0,
),
FutureBuilder(
future:
DefaultAssetBundle.of(context).loadString('assets/data.json'),
builder: (context, snapshot) {
if(!snapshot.hasData) {
return CircularProgressIndicator();
}
var myData = json.decode(snapshot.data);
print(myData);
return ListView.builder(
shrinkWrap: true,
itemBuilder: (BuildContext context, index) {
print(_selectedDept.name);
print(myData[index]['Department']);
print(index);
String URL= myData[index]['URL'];
if (_selectedDept.name ==
myData[index]['Department']) {
return Card(
child: Column(
// crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(myData[index]['Department']),
Text("HOD : " + myData[index]['HOD']),
Text("Contact :" + myData[index]['CONTACT']),
Text("Vision :" + myData[index]['VISION']),
new InkWell(
child: Text("Websites :" +URL),
onTap : ()=> launch(URL),
),
],
),
);
} //if block
else {
return new Container();
}
}, // itembuilder
itemCount: myData == null ? 0 : myData.length,
);
},
),
// Text('Selected Department is : ${_selectedDept.name} '),
],
),
),
),
);
}
}