Created
March 14, 2020 18:25
-
-
Save TetsuFe/6bc83a7cc801762821b484482017224c to your computer and use it in GitHub Desktop.
This file contains 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 'package:flutter/material.dart'; | |
import 'package:provider/provider.dart'; | |
class Parent extends StatelessWidget{ | |
EmailDomainChangeNotifier emailDomainChangeNotifier = | |
EmailDomainChangeNotifier(); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
child: ChangeNotifierProvider<EmailDomainChangeNotifier>( | |
builder: (_) => emailDomainChangeNotifier, | |
child: HokudaiEmailPicker(), | |
), | |
); | |
} | |
} | |
class EmailDomainChangeNotifier with ChangeNotifier { | |
String _emailDomain = '@hokudai.ac.jp'; | |
String get emailDomain => _emailDomain; | |
void changeEmailDomain(String newEmailDomain) { | |
_emailDomain = newEmailDomain; | |
notifyListeners(); | |
} | |
} | |
class HokudaiEmailPicker extends StatefulWidget { | |
@override | |
_HokudaiEmailPickerState createState() => _HokudaiEmailPickerState(); | |
} | |
class _HokudaiEmailPickerState extends State<HokudaiEmailPicker> { | |
EmailDomainChangeNotifier emailDomainChangeNotifier; | |
@override | |
void didChangeDependencies() { | |
super.didChangeDependencies(); | |
emailDomainChangeNotifier = Provider.of<EmailDomainChangeNotifier>(context); | |
} | |
@override | |
Widget build(BuildContext context) { | |
var emailDomainList = [ | |
'@eis.hokudai.ac.jp', | |
'@hokudai.ac.jp', | |
'@ist.hokudai.ac.jp' | |
]; | |
return Container( | |
child: Row(crossAxisAlignment: CrossAxisAlignment.start, children: [ | |
PopupMenuButton<String>( | |
padding: EdgeInsets.all(0), | |
icon: Icon( | |
Icons.arrow_drop_down, | |
size: 24, | |
), | |
onSelected: (String value) { | |
setState(() { | |
emailDomainChangeNotifier.changeEmailDomain(value); | |
}); | |
}, | |
initialValue: emailDomainChangeNotifier.emailDomain, | |
itemBuilder: (context) => emailDomainList | |
.map((v) => PopupMenuItem<String>(value: v, child: Text(v))) | |
.toList(), | |
), | |
Text(emailDomainChangeNotifier.emailDomain) | |
]), | |
); | |
} | |
} |
なぜかStatefulWidgetを使ってしまってる・・
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考
https://qiita.com/kabochapo/items/a90d8438243c27e2f6d9#changenotifierprovider
https://flutter.keicode.com/basics/popupmenubutton.php
スクショ