为什么Flutter显示变量初始化错误?

问题描述 投票:0回答:1

使用Flutter v1.12.13 + hotfix.8,

在调试模式下使用Flutter-App,我不断收到以下错误消息:

════════ Exception caught by rendering library ═══════════════════════════

The method '_greaterThanFromInteger' was called on null.

Receiver: null

Tried calling: _greaterThanFromInteger(7)

The relevant error-causing widget was
    ListView 

lib/drawers/drawer_left.dart:96
════════════════════════════════════════════════════════════

此外,错误消息显示以下内容:

[38;5;244mThe relevant error-causing widget was[39;49m
    [38;5;248mListView[39;49m
[38;5;244mWhen the exception was thrown, this was the stack[39;49m
[38;5;244m#0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:53:5)[39;49m
[38;5;244m#1      int.>  (dart:core-patch/integers.dart:103:18)[39;49m
[38;5;244m#2      RenderSliverFixedExtentBoxAdaptor._calculateTrailingGarbage[39;49m
[38;5;244m#3      RenderSliverFixedExtentBoxAdaptor.performLayout[39;49m
[38;5;244m#4      RenderObject.layout[39;49m
[38;5;244m...[39;49m
[38;5;244mThe following RenderObject was being processed when the exception was fired: RenderSliverFixedExtentList#bbb75 relayoutBoundary=up16 NEEDS-LAYOUT[39;49m
[38;5;244mRenderObject: RenderSliverFixedExtentList#bbb75 relayoutBoundary=up16 NEEDS-LAYOUT[39;49m
    [38;5;244mneeds compositing[39;49m
    [38;5;244mparentData: paintOffset=Offset(0.0, 0.0) (can use size)[39;49m
    [38;5;244mconstraints: SliverConstraints(AxisDirection.down, GrowthDirection.forward, ScrollDirection.idle, scrollOffset: 0.0, remainingPaintExtent: Infinity, crossAxisExtent: 304.0, crossAxisDirection: AxisDirection.right, viewportMainAxisExtent: Infinity, remainingCacheExtent: Infinity cacheOrigin: 0.0 )[39;49m
    [38;5;244mgeometry: SliverGeometry(scrollExtent: 480.0, paintExtent: 480.0, maxPaintExtent: 480.0, cacheExtent: 480.0)[39;49m
        [38;5;244mscrollExtent: 480.0[39;49m
        [38;5;244mpaintExtent: 480.0[39;49m
        [38;5;244mmaxPaintExtent: 480.0[39;49m
        [38;5;244mcacheExtent: 480.0[39;49m
    [38;5;244mcurrently live children: 0 to 7[39;49m
    [38;5;244mchild with index 0: RenderIndexedSemantics#f15ec[39;49m
        [38;5;244mneeds compositing[39;49m
        [38;5;244mparentData: index=0; layoutOffset=0.0[39;49m
        [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
        [38;5;244msemantic boundary[39;49m
        [38;5;244msize: Size(304.0, 60.0)[39;49m
        [38;5;244mindex: 0[39;49m
        [38;5;244mchild: RenderRepaintBoundary#da636[39;49m
            [38;5;244mneeds compositing[39;49m
            [38;5;244mparentData: <none> (can use size)[39;49m
            [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
            [38;5;244mlayer: OffsetLayer#3f6f3[39;49m
                [38;5;244moffset: Offset(0.0, 0.0)[39;49m
            [38;5;244msize: Size(304.0, 60.0)[39;49m
            [38;5;244mmetrics: 0.0% useful (1 bad vs 0 good)[39;49m
            [38;5;244mdiagnosis: insufficient data to draw conclusion (less than five repaints)[39;49m
            [38;5;244mchild: RenderConstrainedBox#1b8cb[39;49m
                [38;5;244mparentData: <none> (can use size)[39;49m
                [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
                [38;5;244msize: Size(304.0, 60.0)[39;49m
                [38;5;244madditionalConstraints: BoxConstraints(0.0<=w<=Infinity, h=60.0)[39;49m
                [38;5;244mchild: RenderPadding#7a95f[39;49m
                    [38;5;244mparentData: <none> (can use size)[39;49m
                    [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
                    [38;5;244msize: Size(304.0, 60.0)[39;49m
                    [38;5;244mpadding: EdgeInsets.zero[39;49m
                    [38;5;244mtextDirection: ltr[39;49m
    [38;5;244mchild with index 1: RenderIndexedSemantics#e3942[39;49m
        [38;5;244mneeds compositing[39;49m
        [38;5;244mparentData: index=1; layoutOffset=60.0[39;49m
        [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
        [38;5;244msemantic boundary[39;49m
        [38;5;244msize: Size(304.0, 60.0)[39;49m
        [38;5;244mindex: 1[39;49m
        [38;5;244mchild: RenderRepaintBoundary#1ca15[39;49m
            [38;5;244mneeds compositing[39;49m
            [38;5;244mparentData: <none> (can use size)[39;49m
            [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
            [38;5;244mlayer: OffsetLayer#7b0c3[39;49m
                [38;5;244moffset: Offset(0.0, 60.0)[39;49m
            [38;5;244msize: Size(304.0, 60.0)[39;49m
            [38;5;244mmetrics: 0.0% useful (1 bad vs 0 good)[39;49m
            [38;5;244mdiagnosis: insufficient data to draw conclusion (less than five repaints)[39;49m
            [38;5;244mchild: RenderConstrainedBox#5fa56[39;49m
                [38;5;244mparentData: <none> (can use size)[39;49m
                [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
                [38;5;244msize: Size(304.0, 60.0)[39;49m
                [38;5;244madditionalConstraints: BoxConstraints(0.0<=w<=Infinity, h=60.0)[39;49m
                [38;5;244mchild: RenderPadding#5a6e7[39;49m
                    [38;5;244mparentData: <none> (can use size)[39;49m
                    [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
                    [38;5;244msize: Size(304.0, 60.0)[39;49m
                    [38;5;244mpadding: EdgeInsets.zero[39;49m
                    [38;5;244mtextDirection: ltr[39;49m
    [38;5;244mchild with index 2: RenderIndexedSemantics#3a923[39;49m
        [38;5;244mneeds compositing[39;49m
        [38;5;244mparentData: index=2; layoutOffset=120.0[39;49m
        [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
        [38;5;244msemantic boundary[39;49m
        [38;5;244msize: Size(304.0, 60.0)[39;49m
        [38;5;244mindex: 2[39;49m
        [38;5;244mchild: RenderRepaintBoundary#61670[39;49m
            [38;5;244mneeds compositing[39;49m
            [38;5;244mparentData: <none> (can use size)[39;49m
            [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
            [38;5;244mlayer: OffsetLayer#7db6e[39;49m
                [38;5;244moffset: Offset(0.0, 120.0)[39;49m
            [38;5;244msize: Size(304.0, 60.0)[39;49m
            [38;5;244mmetrics: 0.0% useful (1 bad vs 0 good)[39;49m
            [38;5;244mdiagnosis: insufficient data to draw conclusion (less than five repaints)[39;49m
            [38;5;244mchild: RenderConstrainedBox#cdca9[39;49m
                [38;5;244mparentData: <none> (can use size)[39;49m
                [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
                [38;5;244msize: Size(304.0, 60.0)[39;49m
                [38;5;244madditionalConstraints: BoxConstraints(0.0<=w<=Infinity, h=60.0)[39;49m
                [38;5;244mchild: RenderPadding#9709e[39;49m
                    [38;5;244mparentData: <none> (can use size)[39;49m
                    [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
                    [38;5;244msize: Size(304.0, 60.0)[39;49m
                    [38;5;244mpadding: EdgeInsets.zero[39;49m
                    [38;5;244mtextDirection: ltr[39;49m
    [38;5;244mchild with index 3: RenderIndexedSemantics#f64fe[39;49m
        [38;5;244mneeds compositing[39;49m
        [38;5;244mparentData: index=3; layoutOffset=180.0[39;49m
        [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
        [38;5;244msemantic boundary[39;49m
        [38;5;244msize: Size(304.0, 60.0)[39;49m
        [38;5;244mindex: 3[39;49m
        [38;5;244mchild: RenderRepaintBoundary#7aa60[39;49m
            [38;5;244mneeds compositing[39;49m
            [38;5;244mparentData: <none> (can use size)[39;49m
            [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
            [38;5;244mlayer: OffsetLayer#e4b81[39;49m
                [38;5;244moffset: Offset(0.0, 180.0)[39;49m
            [38;5;244msize: Size(304.0, 60.0)[39;49m
            [38;5;244mmetrics: 0.0% useful (1 bad vs 0 good)[39;49m
            [38;5;244mdiagnosis: insufficient data to draw conclusion (less than five repaints)[39;49m
            [38;5;244mchild: RenderConstrainedBox#22bc3[39;49m
                [38;5;244mparentData: <none> (can use size)[39;49m
                [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
                [38;5;244msize: Size(304.0, 60.0)[39;49m
                [38;5;244madditionalConstraints: BoxConstraints(0.0<=w<=Infinity, h=60.0)[39;49m
                [38;5;244mchild: RenderPadding#50d9b[39;49m
                    [38;5;244mparentData: <none> (can use size)[39;49m
                    [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
                    [38;5;244msize: Size(304.0, 60.0)[39;49m
                    [38;5;244mpadding: EdgeInsets.zero[39;49m
                    [38;5;244mtextDirection: ltr[39;49m
    [38;5;244mchild with index 4: RenderIndexedSemantics#0ece6[39;49m
        [38;5;244mneeds compositing[39;49m
        [38;5;244mparentData: index=4; layoutOffset=240.0[39;49m
        [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
        [38;5;244msemantic boundary[39;49m
        [38;5;244msize: Size(304.0, 60.0)[39;49m
        [38;5;244mindex: 4[39;49m
        [38;5;244mchild: RenderRepaintBoundary#4891a[39;49m
            [38;5;244mneeds compositing[39;49m
            [38;5;244mparentData: <none> (can use size)[39;49m
            [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
            [38;5;244mlayer: OffsetLayer#6f936[39;49m
                [38;5;244moffset: Offset(0.0, 240.0)[39;49m
            [38;5;244msize: Size(304.0, 60.0)[39;49m
            [38;5;244mmetrics: 0.0% useful (1 bad vs 0 good)[39;49m
            [38;5;244mdiagnosis: insufficient data to draw conclusion (less than five repaints)[39;49m
            [38;5;244mchild: RenderConstrainedBox#b06e6[39;49m
                [38;5;244mparentData: <none> (can use size)[39;49m
                [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
                [38;5;244msize: Size(304.0, 60.0)[39;49m
                [38;5;244madditionalConstraints: BoxConstraints(0.0<=w<=Infinity, h=60.0)[39;49m
                [38;5;244mchild: RenderPadding#44b12[39;49m
                    [38;5;244mparentData: <none> (can use size)[39;49m
                    [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
                    [38;5;244msize: Size(304.0, 60.0)[39;49m
                    [38;5;244mpadding: EdgeInsets.zero[39;49m
                    [38;5;244mtextDirection: ltr[39;49m
    [38;5;244mchild with index 5: RenderIndexedSemantics#97bd7[39;49m
        [38;5;244mneeds compositing[39;49m
        [38;5;244mparentData: index=5; layoutOffset=300.0[39;49m
        [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
        [38;5;244msemantic boundary[39;49m
        [38;5;244msize: Size(304.0, 60.0)[39;49m
        [38;5;244mindex: 5[39;49m
        [38;5;244mchild: RenderRepaintBoundary#db1ac[39;49m
            [38;5;244mneeds compositing[39;49m

            [38;5;244mparentData: <none> (can use size)[39;49m
            [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
            [38;5;244mlayer: OffsetLayer#7f4b8[39;49m
                [38;5;244moffset: Offset(0.0, 360.0)[39;49m
            [38;5;244msize: Size(304.0, 60.0)[39;49m
            [38;5;244mmetrics: 0.0% useful (1 bad vs 0 good)[39;49m
            [38;5;244mdiagnosis: insufficient data to draw conclusion (less than five repaints)[39;49m
            [38;5;244mchild: RenderConstrainedBox#053e3[39;49m
                [38;5;244mparentData: <none> (can use size)[39;49m
                [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
                [38;5;244msize: Size(304.0, 60.0)[39;49m
                [38;5;244madditionalConstraints: BoxConstraints(0.0<=w<=Infinity, h=60.0)[39;49m
                [38;5;244mchild: RenderPadding#cb6ed[39;49m
                    [38;5;244mparentData: <none> (can use size)[39;49m
                    [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
                    [38;5;244msize: Size(304.0, 60.0)[39;49m
                    [38;5;244mpadding: EdgeInsets.zero[39;49m
                    [38;5;244mtextDirection: ltr[39;49m
    [38;5;244mchild with index 7: RenderIndexedSemantics#393e7[39;49m
        [38;5;244mneeds compositing[39;49m
        [38;5;244mparentData: index=7; layoutOffset=420.0[39;49m
        [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
        [38;5;244msemantic boundary[39;49m
        [38;5;244msize: Size(304.0, 60.0)[39;49m
        [38;5;244mindex: 7[39;49m
        [38;5;244mchild: RenderRepaintBoundary#300dc[39;49m
            [38;5;244mneeds compositing[39;49m
            [38;5;244mparentData: <none> (can use size)[39;49m
            [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
            [38;5;244mlayer: OffsetLayer#442ed[39;49m
                [38;5;244moffset: Offset(0.0, 420.0)[39;49m
            [38;5;244msize: Size(304.0, 60.0)[39;49m
            [38;5;244mmetrics: 0.0% useful (1 bad vs 0 good)[39;49m
            [38;5;244mdiagnosis: insufficient data to draw conclusion (less than five repaints)[39;49m
            [38;5;244mchild: RenderConstrainedBox#5e085[39;49m
                [38;5;244mparentData: <none> (can use size)[39;49m
                [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
                [38;5;244msize: Size(304.0, 60.0)[39;49m
                [38;5;244madditionalConstraints: BoxConstraints(0.0<=w<=Infinity, h=60.0)[39;49m
                [38;5;244mchild: RenderPadding#feb7c[39;49m
                    [38;5;244mparentData: <none> (can use size)[39;49m
                    [38;5;244mconstraints: BoxConstraints(w=304.0, h=60.0)[39;49m
                    [38;5;244msize: Size(304.0, 60.0)[39;49m
                    [38;5;244mpadding: EdgeInsets.zero[39;49m
                    [38;5;244mtextDirection: ltr[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m

我读过here,这在未初始化变量时发生。

但是我看不出我的代码在哪里。

下面是我的代码-有人可以帮我指出一下吗?我从2个小时以来一直在调查此事,但没有成功。...

这里是整个小部件

主要在ListView下发生-但在哪里????


    import 'package:corona_test/screens/user_state.dart';
    import 'package:flutter/material.dart';
    import 'package:corona_test/app_locations.dart';
    import 'package:url_launcher/url_launcher.dart';

    class DrawerLeft extends StatefulWidget {
      DrawerLeft({Key key}) : super(key: key);

      @override
      _DrawerLeftState createState() => _DrawerLeftState();
    }

    class _DrawerLeftState extends State<DrawerLeft> {
      final int nrOfSettingsBullets = 8;
      int screenSizeType = 0;

      @override
      Widget build(BuildContext context) {
        // first define settings titles (order matters)
        List<String> settings = [];
        settings
            .add(AppLocalizations.of(context).translate('My Personal Situation'));
        settings.add(
            'E-Mail'); // AppLocalizations.of(context).translate('Revert to Original Icons'));
        settings.add(
            'Test Dialog'); // AppLocalizations.of(context).translate('App help videos 1'));
        settings.add(
            't.b.d'); // AppLocalizations.of(context).translate('Facebook Like'));
        settings.add(
            't.b.d'); // AppLocalizations.of(context).translate('Our website'));
        settings
            .add('t.b.d'); // AppLocalizations.of(context).translate('Contact'));
        settings
            .add('t.b.d'); // AppLocalizations.of(context).translate('Rate App'));
        settings
            .add('t.b.d'); //AppLocalizations.of(context).translate('Impressum'));

        final double screenHeight = MediaQuery.of(context).size.longestSide;
        if (screenHeight >= 1000) {
          screenSizeType = 8;
        } else if (screenHeight >= 896) {
          // iPhone XSmax/XR
          screenSizeType = 7;
        } else if (screenHeight >= 812) {
          // iPhone XS
          screenSizeType = 6;
        } else if (screenHeight >= 800) {
          // Android Samsung Galaxy S7 5.1"
          screenSizeType = 5;
        } else if (screenHeight >= 736) {
          // iPhone 6S Plus
          screenSizeType = 4;
        } else if (screenHeight >= 690) {
          // Android Samsung Galaxy S9 5.8"
          screenSizeType = 3;
        } else if (screenHeight >= 683) {
          // Android Nexus 5X, Pixel 2
          screenSizeType = 2;
        } else if (screenHeight >= 667) {
          // iPhone 6S
          screenSizeType = 1;
        } else if (screenHeight >= 568) {
          // iPhone 5S
          screenSizeType = 0;
        } else {
          screenSizeType = 0;
        }

        return ListView(
          physics: const NeverScrollableScrollPhysics(),
          padding: EdgeInsets.zero,
          children: <Widget>[
            Container(
              height: _drawerHeaderHeight() ?? 80.0,
              child: DrawerHeader(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    SizedBox(
                      width: _spacerXTitle() ?? 12.0,
                    ),
                    Text(AppLocalizations.of(context).translate('Settings'),
                      style: TextStyle(
                          fontSize: 21.0,
                          color: Colors.white,
                          fontWeight: FontWeight.w600),
                    ),
                  ],
                ),
                decoration: BoxDecoration(
                  color: Colors.blue,
                ),
              ),
            ),
            ListView.builder(
              physics: const NeverScrollableScrollPhysics(),
              shrinkWrap: true, // this way you don't need Expanded()
              itemCount: this.nrOfSettingsBullets,
              itemExtent: _tileHeightSettings() ?? 60.0,
              itemBuilder: (BuildContext ctxt, int index) {
                return Container(
                  padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                  height: _tileHeightSettings() ?? 60.0,
                  child: ListTile(
                    title: Container(
                      alignment: AlignmentDirectional.centerStart,
                      height: _itemHeightSetting() ?? 50.0,
                      decoration: BoxDecoration(
                        color: Color.fromRGBO(0x00, 0x99, 0xCC, 0.2),
                        borderRadius: BorderRadius.all(Radius.circular(14.0)),
                        border: Border.all(
                            color: Color.fromRGBO(0x2d, 0x32, 0x7d, 0.2)),
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          SizedBox(
                            width: 12.0,
                          ),
                          Text(
                            settings[index],
                            style: TextStyle(
                                fontSize: 20.0,
                                color: Color.fromRGBO(0x2d, 0x32, 0x7d, 1.0),
                                fontWeight: FontWeight.w600),
                          ),
                        ],
                      ),
                    ),
                    onTap: () async {
                      switch (index) {
                        case 0:                      
                          Navigator.of(context).pop();
                          _navigateToUserState(context);                      
                          break;
                        case 1:
                          _sendMail();
                          break;
                        case 2:
                          _showDialog(
                            'Wollen Sie wirklich TEST TEST ?',
                            'Alle Einstellungen werden TEST TEST',
                            // AppLocalizations.of(context).translate(
                            //     'Do you really want to revert to Original Icons ?'),
                            // AppLocalizations.of(context).translate(
                            //     'All privately added Image Icons will be deleted.'),
                          );
                          break;
                        case 3:
                          //_launchFacebookURL();
                          break;
                        case 4:
                          //_launchChunderURL();
                          break;
                        case 5:
                          break;
                        case 6:
                          //_sendReview();
                          break;
                        case 7:
                          break;
                        default:
                          break;
                      }
                    },
                  ),
                );
              },
            ),
            SizedBox(
              height: _aboveCancelSpaceLeft() ?? 16.0,
            ),
            MaterialButton(
              height: 70.0,
              minWidth: 70.0,
              color: Colors.blue,
              textColor: Colors.white,
              child: Text(
                'Cancel', // AppLocalizations.of(context).translate('Cancel'),
                style: TextStyle(
                    fontSize: 21.0,
                    color: Colors.white,
                    fontWeight: FontWeight.w600),
              ),
              onPressed: () {
                Navigator.of(context).pop();
              },
              splashColor: Color.fromRGBO(0x00, 0x99, 0xCC, 1.0),
            )
          ],
        );
      }

      // user defined function
      void _showDialog(String title, String content) {
        // flutter defined function
        showDialog(
          context: context,
          builder: (BuildContext context) {
            // return object of type Dialog
            return AlertDialog(
              title: Text(title),
              content: Text(content),
              actions: <Widget>[
                FlatButton(
                  child: Text(
                    'No', //AppLocalizations.of(context).translate('No'),
                    style: TextStyle(
                        fontSize: 21.0,
                        color: Color.fromRGBO(0x00, 0x99, 0xCC, 1.0),
                        fontWeight: FontWeight.w600),
                  ),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
                FlatButton(
                  child: Text(
                    'Yes', // AppLocalizations.of(context).translate('Yes'),
                    style: TextStyle(
                        fontSize: 21.0,
                        color: Color.fromRGBO(0x00, 0x99, 0xCC, 1.0),
                        fontWeight: FontWeight.w600),
                  ),
                  onPressed: () async {
                    // revert to Original Icons
                    // Directory directory = await LocationiKK.dbDirectory();
                    // DBHelperLocations databaseHelperLocations =
                    //     DBHelperLocations();
                    // databaseHelperLocations.resetLocationDB(directory);
                    // await _loadLocationsFromSQLDBIntoContainer();
                    Future.delayed(
                      const Duration(milliseconds: 800),
                      () {
                        Navigator.of(context).pop();
                      },
                    );
                  },
                ),
              ],
            );
          },
        );
      }

      double _tileHeightSettings() {
        switch (screenSizeType) {
          case 8:
            return 62.0;
            break;
          case 7:
            return 62.0;
            break;
          case 6:
            return 57.0;
            break;
          case 5:
            return 57.0;
            break;
          case 4:
            return 57.0;
            break;
          case 3:
            return 57.0;
            break;
          case 2:
            return 56.0;
            break;
          case 1:
            return 53.0;
            break;
          case 0:
            return 46.0;
            break;
          default:
            return 55.0;
            break;
        }
      }

      double _drawerHeaderHeight() {
        switch (screenSizeType) {
          case 8:
            return 80.0;
            break;
          case 7:
            return 80.0;
            break;
          case 6:
            return 80.0;
            break;
          case 5:
            return 80.0;
            break;
          case 4:
            return 80.0;
            break;
          case 3:
            return 80.0;
            break;
          case 2:
            return 70.0;
            break;
          case 1:
            return 80.0;
            break;
          case 0:
            return 70.0;
            break;
          default:
            return 105.0;
            break;
        }
      }

      double _itemHeightSetting() {
        switch (screenSizeType) {
          case 8:
            return 52.0;
            break;
          case 7:
            return 52.0;
            break;
          case 6:
            return 50.0;
            break;
          case 5:
            return 50.0;
            break;
          case 4:
            return 50.0;
            break;
          case 3:
            return 50.0;
            break;
          case 2:
            return 48.0;
            break;
          case 1:
            return 46.0;
            break;
          case 0:
            return 40.0;
            break;
          default:
            return 50.0;
            break;
        }
      }

      double _aboveCancelSpaceLeft() {
        switch (screenSizeType) {
          case 8:
            return 16.0;
            break;
          case 7:
            return 16.0;
            break;
          case 6:
            return 16.0;
            break;
          case 5:
            return 16.0;
            break;
          case 4:
            return 16.0;
            break;
          case 3:
            return 16.0;
            break;
          case 2:
            return 5.0;
            break;
          case 1:
            return 16.0;
            break;
          case 0:
            return 16.0;
            break;
          default:
            return 16.0;
            break;
        }
      }

      double _spacerXTitle() {
        switch (screenSizeType) {
          case 8:
            return 12.0;
            break;
          case 7:
            return 12.0;
            break;
          case 6:
            return 12.0;
            break;
          case 5:
            return 12.0;
            break;
          case 4:
            return 12.0;
            break;
          case 3:
            return 12.0;
            break;
          case 2:
            return 12.0;
            break;
          case 1:
            return 12.0;
            break;
          case 0:
            return 12.0;
            break;
          default:
            return 22.0;
            break;
        }
      }

      _sendMail() async {
        const url =
            'mailto:[email protected]?subject=Feedback%20Corona%20Control&body=';
        if (await canLaunch(url)) {
          await launch(url);
        } else {
          throw 'Could not launch $url';
        }
      }

      Future<String> _navigateToUserState(BuildContext context) async {
        return await Navigator.push(
            context, MaterialPageRoute(builder: (context) => UserState()));
      }
    }
flutter
1个回答
0
投票

您可以在下面复制粘贴运行完整代码删除此行itemExtent: _tileHeightSettings() ?? 60.0,后,工作正常

工作演示

enter image description here

完整代码

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class DrawerLeft extends StatefulWidget {
  DrawerLeft({Key key}) : super(key: key);

  @override
  _DrawerLeftState createState() => _DrawerLeftState();
}

class _DrawerLeftState extends State<DrawerLeft> {
  final int nrOfSettingsBullets = 8;
  int screenSizeType = 0;

  @override
  Widget build(BuildContext context) {
    // first define settings titles (order matters)
    List<String> settings = [];
    settings.add('My Personal Situation');
    settings.add(
        'E-Mail'); // AppLocalizations.of(context).translate('Revert to Original Icons'));
    settings.add(
        'Test Dialog'); // AppLocalizations.of(context).translate('App help videos 1'));
    settings.add(
        't.b.d'); // AppLocalizations.of(context).translate('Facebook Like'));
    settings.add(
        't.b.d'); // AppLocalizations.of(context).translate('Our website'));
    settings
        .add('t.b.d'); // AppLocalizations.of(context).translate('Contact'));
    settings
        .add('t.b.d'); // AppLocalizations.of(context).translate('Rate App'));
    settings
        .add('t.b.d'); //AppLocalizations.of(context).translate('Impressum'));

    final double screenHeight = MediaQuery.of(context).size.longestSide;
    if (screenHeight >= 1000) {
      screenSizeType = 8;
    } else if (screenHeight >= 896) {
      // iPhone XSmax/XR
      screenSizeType = 7;
    } else if (screenHeight >= 812) {
      // iPhone XS
      screenSizeType = 6;
    } else if (screenHeight >= 800) {
      // Android Samsung Galaxy S7 5.1"
      screenSizeType = 5;
    } else if (screenHeight >= 736) {
      // iPhone 6S Plus
      screenSizeType = 4;
    } else if (screenHeight >= 690) {
      // Android Samsung Galaxy S9 5.8"
      screenSizeType = 3;
    } else if (screenHeight >= 683) {
      // Android Nexus 5X, Pixel 2
      screenSizeType = 2;
    } else if (screenHeight >= 667) {
      // iPhone 6S
      screenSizeType = 1;
    } else if (screenHeight >= 568) {
      // iPhone 5S
      screenSizeType = 0;
    } else {
      screenSizeType = 0;
    }

    return ListView(
      physics: const NeverScrollableScrollPhysics(),
      padding: EdgeInsets.zero,
      children: <Widget>[
        Container(
          height: _drawerHeaderHeight() ?? 80.0,
          child: DrawerHeader(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                SizedBox(
                  width: _spacerXTitle() ?? 12.0,
                ),
                Text(
                  'Settings',
                  style: TextStyle(
                      fontSize: 21.0,
                      color: Colors.white,
                      fontWeight: FontWeight.w600),
                ),
              ],
            ),
            decoration: BoxDecoration(
              color: Colors.blue,
            ),
          ),
        ),
        ListView.builder(
          physics: const NeverScrollableScrollPhysics(),
          shrinkWrap: true, // this way you don't need Expanded()
          itemCount: this.nrOfSettingsBullets,
          //itemExtent: _tileHeightSettings() ?? 60.0,         
          itemBuilder: (BuildContext ctxt, int index) {
            return Container(
              padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
              height: _tileHeightSettings() ?? 60.0,
              child: ListTile(
                title: Container(
                  alignment: AlignmentDirectional.centerStart,
                  height: _itemHeightSetting() ?? 50.0,
                  decoration: BoxDecoration(
                    color: Color.fromRGBO(0x00, 0x99, 0xCC, 0.2),
                    borderRadius: BorderRadius.all(Radius.circular(14.0)),
                    border: Border.all(
                        color: Color.fromRGBO(0x2d, 0x32, 0x7d, 0.2)),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      SizedBox(
                        width: 12.0,
                      ),
                      Text(
                        settings[index],
                        style: TextStyle(
                            fontSize: 20.0,
                            color: Color.fromRGBO(0x2d, 0x32, 0x7d, 1.0),
                            fontWeight: FontWeight.w600),
                      ),
                    ],
                  ),
                ),
                onTap: () async {
                  switch (index) {
                    case 0:
                      Navigator.of(context).pop();
                      _navigateToUserState(context);
                      break;
                    case 1:
                      _sendMail();
                      break;
                    case 2:
                      _showDialog(
                        'Wollen Sie wirklich TEST TEST ?',
                        'Alle Einstellungen werden TEST TEST',
                        // AppLocalizations.of(context).translate(
                        //     'Do you really want to revert to Original Icons ?'),
                        // AppLocalizations.of(context).translate(
                        //     'All privately added Image Icons will be deleted.'),
                      );
                      break;
                    case 3:
                      //_launchFacebookURL();
                      break;
                    case 4:
                      //_launchChunderURL();
                      break;
                    case 5:
                      break;
                    case 6:
                      //_sendReview();
                      break;
                    case 7:
                      break;
                    default:
                      break;
                  }
                },
              ),
            );
          },
        ),
        SizedBox(
          height: _aboveCancelSpaceLeft() ?? 16.0,
        ),
        MaterialButton(
          height: 70.0,
          minWidth: 70.0,
          color: Colors.blue,
          textColor: Colors.white,
          child: Text(
            'Cancel', // AppLocalizations.of(context).translate('Cancel'),
            style: TextStyle(
                fontSize: 21.0,
                color: Colors.white,
                fontWeight: FontWeight.w600),
          ),
          onPressed: () {
            Navigator.of(context).pop();
          },
          splashColor: Color.fromRGBO(0x00, 0x99, 0xCC, 1.0),
        )
      ],
    );
  }

  // user defined function
  void _showDialog(String title, String content) {
    // flutter defined function
    showDialog(
      context: context,
      builder: (BuildContext context) {
        // return object of type Dialog
        return AlertDialog(
          title: Text(title),
          content: Text(content),
          actions: <Widget>[
            FlatButton(
              child: Text(
                'No', //AppLocalizations.of(context).translate('No'),
                style: TextStyle(
                    fontSize: 21.0,
                    color: Color.fromRGBO(0x00, 0x99, 0xCC, 1.0),
                    fontWeight: FontWeight.w600),
              ),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            FlatButton(
              child: Text(
                'Yes', // AppLocalizations.of(context).translate('Yes'),
                style: TextStyle(
                    fontSize: 21.0,
                    color: Color.fromRGBO(0x00, 0x99, 0xCC, 1.0),
                    fontWeight: FontWeight.w600),
              ),
              onPressed: () async {
                // revert to Original Icons
                // Directory directory = await LocationiKK.dbDirectory();
                // DBHelperLocations databaseHelperLocations =
                //     DBHelperLocations();
                // databaseHelperLocations.resetLocationDB(directory);
                // await _loadLocationsFromSQLDBIntoContainer();
                Future.delayed(
                  const Duration(milliseconds: 800),
                  () {
                    Navigator.of(context).pop();
                  },
                );
              },
            ),
          ],
        );
      },
    );
  }

  double _tileHeightSettings() {
    print('screenSizeType ${screenSizeType}');
    switch (screenSizeType) {
      case 8:
        return 62.0;
        break;
      case 7:
        return 62.0;
        break;
      case 6:
        return 57.0;
        break;
      case 5:
        return 57.0;
        break;
      case 4:
        return 57.0;
        break;
      case 3:
        return 57.0;
        break;
      case 2:
        return 56.0;
        break;
      case 1:
        return 53.0;
        break;
      case 0:
        return 46.0;
        break;
      default:
        return 55.0;
        break;
    }
  }

  double _drawerHeaderHeight() {
    switch (screenSizeType) {
      case 8:
        return 80.0;
        break;
      case 7:
        return 80.0;
        break;
      case 6:
        return 80.0;
        break;
      case 5:
        return 80.0;
        break;
      case 4:
        return 80.0;
        break;
      case 3:
        return 80.0;
        break;
      case 2:
        return 70.0;
        break;
      case 1:
        return 80.0;
        break;
      case 0:
        return 70.0;
        break;
      default:
        return 105.0;
        break;
    }
  }

  double _itemHeightSetting() {
    switch (screenSizeType) {
      case 8:
        return 52.0;
        break;
      case 7:
        return 52.0;
        break;
      case 6:
        return 50.0;
        break;
      case 5:
        return 50.0;
        break;
      case 4:
        return 50.0;
        break;
      case 3:
        return 50.0;
        break;
      case 2:
        return 48.0;
        break;
      case 1:
        return 46.0;
        break;
      case 0:
        return 40.0;
        break;
      default:
        return 50.0;
        break;
    }
  }

  double _aboveCancelSpaceLeft() {
    switch (screenSizeType) {
      case 8:
        return 16.0;
        break;
      case 7:
        return 16.0;
        break;
      case 6:
        return 16.0;
        break;
      case 5:
        return 16.0;
        break;
      case 4:
        return 16.0;
        break;
      case 3:
        return 16.0;
        break;
      case 2:
        return 5.0;
        break;
      case 1:
        return 16.0;
        break;
      case 0:
        return 16.0;
        break;
      default:
        return 16.0;
        break;
    }
  }

  double _spacerXTitle() {
    switch (screenSizeType) {
      case 8:
        return 12.0;
        break;
      case 7:
        return 12.0;
        break;
      case 6:
        return 12.0;
        break;
      case 5:
        return 12.0;
        break;
      case 4:
        return 12.0;
        break;
      case 3:
        return 12.0;
        break;
      case 2:
        return 12.0;
        break;
      case 1:
        return 12.0;
        break;
      case 0:
        return 12.0;
        break;
      default:
        return 22.0;
        break;
    }
  }

  _sendMail() async {
    const url =
        'mailto:[email protected]?subject=Feedback%20Corona%20Control&body=';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  Future<String> _navigateToUserState(BuildContext context) async {
    return await Navigator.push(
        context, MaterialPageRoute(builder: (context) => UserState()));
  }
}

class UserState extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: "test"),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: DrawerLeft(),
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.