跳至主內容區

XOOPS輕鬆架

:::

主內容區域

選擇後會自動跳轉頁面

6-13 父 Widget管理子 Widget的狀態

父 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;
      },
    );
  }
}

 

選擇後會自動跳轉頁面

:::

左邊區域內容

書籍目錄

展開 | 闔起

快速登入


右邊區域內容

請掃此 QR Code 可連至此頁面

計數器

今天: 1502150215021502
總計: 9677950967795096779509677950967795096779509677950

頁尾區域