線上書籍

Home

Dart 及 flutter 初體驗

父 Widget

class _MyHomePageState extends State<MyHomePage> { String displayCate = '蔬菜類'; // 改變狀態中分類的值 void setDisplayCate(value) { setState(() { displayCate = value; print('displayCate:$displayCate'); }); } @override Widget build(BuildContext context) { return Scaffold( body: FoodCateList( changeCate: setDisplayCate, ), ); } }

子 Widget

class FoodCateList extends StatelessWidget { final ValueChanged<String> changeCate; FoodCateList({ @required this.changeCate, Key key}): assert(changeCate != null), super(key: key); @override Widget build(BuildContext context) { return ListView.separated( itemBuilder: (context, index) { FoodCate foodCate = snap.data[index]; return FoodKindButton( changeCate: changeCate, ); }, separatorBuilder: (context, index) {}, itemCount: snap.data.length ), } }

底下某個按鈕

//單一個類別按鈕 class FoodKindButton extends StatelessWidget { final ValueChanged<String> changeCate; FoodKindButton( {@required this.changeCate, Key key}) assert(changeCate != null), super(key: key); @override Widget build(BuildContext context) { return UnicornOutlineButton( onPressed: () { changeCate(title); return changeCate; }, ); } }