alibaba / fish-redux

An assembled flutter application framework.
https://github.com/alibaba/fish-redux
Apache License 2.0
7.33k stars 843 forks source link

PreferredSizeWidgetWrapper 在 release 下失效了 #439

Closed genox-wang closed 5 years ago

genox-wang commented 5 years ago

我使用 WidgetWrapper 包裹了 PreferredSizeWidget 实现让 Component 可以在 appBar 内使用

以下代码在 debug 的时候都没问题,不过我 flutter run --release 只有 appBar 不见了。

Widget appBarWrapper(Widget widget) {
  return AppBarWrapper(child: widget,);
}

class AppBarWrapper extends StatefulWidget implements PreferredSizeWidget{
  ...
}
lass TopTabComponent extends Component<TopTabState> {
  TopTabComponent()
      : super(
          // 包裹 PreferredSizeWidget 让模块可以直接用于 appBar
          wrapper: appBarWrapper,
Widget buildView(HomeState state, Dispatch dispatch, ViewService viewService) {
  return Scaffold(
      appBar: viewService.buildComponent("top_tab"),

Additional context

  1. The version of fish-redux which you are using. fish_redux: ^0.2.5
  2. The information from flutter doctor.
    
    [✓] Flutter (Channel stable, v1.7.8+hotfix.4, on Mac OS X 10.14.6 18G84, locale en-CN)

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3) [✓] Xcode - develop for iOS and macOS (Xcode 10.3) [✓] iOS tools - develop for iOS devices [✓] Android Studio (version 3.4) [!] IntelliJ IDEA Ultimate Edition (version 2018.3.5) ✗ Flutter plugin not installed; this adds Flutter specific functionality. ✗ Dart plugin not installed; this adds Dart specific functionality. [✓] VS Code (version 1.37.1) [✓] Connected device (1 available)

genox-wang commented 5 years ago
class RootAppBar extends StatefulWidget implements PreferredSizeWidget {
  RootAppBar({
    @required this.tabs,
    @required this.index,
    @required this.tabChanged,
    @required this.onSearch,
  });

  final List<Widget> tabs;
  final int index;
  final Function(int) tabChanged;
  final VoidCallback onSearch;

  @override
  _RootAppBarState createState() => _RootAppBarState();

   @override
  Size get preferredSize => new Size.fromHeight(Screen.setWidth(160) + Screen.topSafeHeight);
}

class _RootAppBarState extends State<RootAppBar>
    with SingleTickerProviderStateMixin {
  TabController controller;

  @override
  void initState() {
    controller = TabController(
        initialIndex: widget.index, length: widget.tabs.length, vsync: this);
    super.initState();
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context)  {
    return Column(
      children: <Widget>[
        SizedBox(
          height: Screen.topSafeHeight,
        ),
        Container(
          color: Color(0xFFF9F9F9),
          height: Screen.setWidth(160),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              SizedBox(
                width: Screen.setWidth(50),
              ),
              GestureDetector(
                child: Image.asset('img/bookstore_search.png',
                    width: Screen.setWidth(50)),
                onTap: () {
                  widget.onSearch();
                },
              ),
              SizedBox(
                width: Screen.setWidth(200),
              ),
              Container(
                width: Screen.setWidth(500),
                child: TabBar(
                  indicator: BoxDecoration(
                    image: DecorationImage(
                        image: ExactAssetImage("img/tab_bg.png"),
                        fit: BoxFit.fitHeight),
                  ),
                  labelPadding:
                      EdgeInsets.symmetric(vertical: Screen.setWidth(40)),
                  controller: controller,
                  tabs: widget.tabs,
                  labelColor: AppColors.darkGray,
                  unselectedLabelColor: AppColors.gray,
                  onTap: (index) {
                    widget.tabChanged(index);
                  },
                ),
              )
            ],
          ),
        ),
      ],
    );
  }
}

以上是我 AppBar 代码

Widget buildView(HomeState state, Dispatch dispatch, ViewService viewService) {
  return Scaffold(
      // appBar: viewService.buildComponent("top_tab"),
      appBar: RootAppBar(
        index: 0,
        tabs: ["书单", "书城", "我的"]
            .map((name) => Text(
                  name,
                  style: TextStyle(
                      fontSize: 20, fontWeight: FontWeight.bold),
                ))
            .toList(),
      ),

我这样直接用是可以的

Widget buildView(
    TopTabState state, Dispatch dispatch, ViewService viewService) {
  return RootAppBar(
    index: state.selectedIndex,
    tabs: state.tabList
        .map((name) => Text(
              name,
              style: TextStyle(
                  fontSize: Screen.setSp(50), fontWeight: FontWeight.bold),
            ))
        .toList(),
    tabChanged: (index) {
      if (state.selectedIndex == index) {
        return;
      }
      dispatch(TopTabActionCreator.onChanged(index));
    },
    onSearch: () {
      dispatch(TopTabActionCreator.onSearch());
    },
  );

包装成 Component 就不行了,release 下无法显示,也不跑错

zjuwjf commented 5 years ago

后面有进展么? 如果还有问题,方便提供一个最小化的demo么?

genox-wang commented 5 years ago

@zjuwjf 目前没什么进展,我晚点出个最小化 demo 试试,我目前直接用 StatefulWidget 替换 component 来解决

zjuwjf commented 5 years ago

后来有demo么?可以看下是哪里的问题?

zjuwjf commented 5 years ago

没有更一步的问题,先关闭。如有需要可以随时reopen。