Last active
April 4, 2021 18:30
-
-
Save IsmailAlamKhan/55e985c1e3f09f3e37d100e7355ef24a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'dart:convert'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart' show rootBundle; | |
class MyStatefulWidget extends StatefulWidget { | |
const MyStatefulWidget({Key? key}) : super(key: key); | |
@override | |
_MyStatefulWidgetState createState() => _MyStatefulWidgetState(); | |
} | |
class _MyStatefulWidgetState extends State<MyStatefulWidget> { | |
String? _mySelection = "Atmosphere"; | |
Future<String> getJson() { | |
return rootBundle.loadString('data.json'); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
body: new Center( | |
child: FutureBuilder<String>( | |
future: getJson(), | |
builder: (context, snapshot) { | |
if (!snapshot.hasData) return CircularProgressIndicator(); | |
if (snapshot.hasError) { | |
return Center(child: Text(snapshot.error?.toString() ?? '')); | |
} | |
final Map<String, dynamic> _data = jsonDecode(snapshot.data!); | |
return DropdownButton<String>( | |
isDense: true, | |
hint: Text("Select"), | |
value: _mySelection, | |
onChanged: (String? newValue) { | |
setState(() { | |
_mySelection = newValue; | |
}); | |
print(_mySelection); | |
}, | |
items: _data | |
.map((key, map) { | |
return MapEntry( | |
key, | |
DropdownMenuItem<String>( | |
value: map["link"].toString(), | |
child: new Text( | |
map["name"], | |
), | |
), | |
); | |
}) | |
.values | |
.toList(), | |
); | |
}), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment