微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将日期选择器的文本字段和图标放在同一行

如何解决将日期选择器的文本字段和图标放在同一行

我想要做的就是在我的 Flutter 应用程序的同一行上有一个文本字段和一个日期选择器图标。当用户单击日期选择器图标时,他们将能够选择一个日期,该日期将在文本字段中设置。这应该很简单,但我不断收到错误消息。

此外,文本字段和图标现在甚至不会出现在屏幕上。请帮忙。

这是我进行热重载时的控制台输出

Performing hot reload...
Syncing files to device sdk gphone x86...
/C:/src/Flutter/Flutter/.pub-cache/hosted/pub.Flutter-io.cn/Flutter_datetime_picker-1.5.1/lib/Flutter_datetime_picker.dart:311:32: Warning: Operand of null-aware operation '??' has type 'Color' which excludes null.
 - 'Color' is from 'dart:ui'.
                  color: theme.backgroundColor ?? Colors.white,^

======== Exception caught by rendering library =====================================================
The following assertion was thrown during performlayout():
An InputDecorator,which is typically created by a TextField,cannot have an unbounded width.
This happens when the parent widget does not provide a finite width constraint. For example,if the InputDecorator is contained by a Row,then its width must be constrained. An Expanded widget or a SizedBox can be used to constrain the width of the InputDecorator or the TextField that contains it.
'package:Flutter/src/material/input_decorator.dart':
Failed assertion: line 959 pos 7: 'layoutConstraints.maxWidth < double.infinity'


Either the assertion indicates an error in the framework itself,or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case,please report this assertion by filing a bug on GitHub:
  https://github.com/Flutter/Flutter/issues/new?template=2_bug.md

The relevant error-causing widget was: 
  TextField file:///C:/Users/nkane/AndroidStudioProjects/tonnah/lib/screens/transaction_detail_screen.dart:582:21
When the exception was thrown,this was the stack: 
#2      _Renderdecoration._layout (package:Flutter/src/material/input_decorator.dart:959:7)
#3      _Renderdecoration.performlayout (package:Flutter/src/material/input_decorator.dart:1310:44)
#4      RenderObject.layout (package:Flutter/src/rendering/object.dart:1779:7)
#5      RenderProxyBoxMixin.performlayout (package:Flutter/src/rendering/proxy_Box.dart:116:14)
#6      RenderObject.layout (package:Flutter/src/rendering/object.dart:1779:7)
...
The following RenderObject was being processed when the exception was fired: _Renderdecoration#3d795 relayoutBoundary=up5 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...  needs compositing
...  parentData: <none> (can use size)
...  constraints: BoxConstraints(0.0<=w<=Infinity,0.0<=h<=50.0)
...  size: MISSING
RenderObject: _Renderdecoration#3d795 relayoutBoundary=up5 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
  needs compositing
  parentData: <none> (can use size)
  constraints: BoxConstraints(0.0<=w<=Infinity,0.0<=h<=50.0)
  size: MISSING
...  input: RenderRepaintBoundary#5840c NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...    needs compositing
...    parentData: offset=Offset(0.0,0.0)
...    constraints: MISSING
...    size: MISSING
...    usefulness ratio: no metrics collected yet (never painted)
...    child: RenderMouseRegion#4b1e1 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...      needs compositing
...      parentData: <none>
...      constraints: MISSING
...      size: MISSING
...      listeners: <none>
...      child: RenderRepaintBoundary#0aae3 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...        needs compositing
...        parentData: <none>
...        constraints: MISSING
...        size: MISSING
...        usefulness ratio: no metrics collected yet (never painted)
...        child: RenderCustomPaint#3a2b6 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...          needs compositing
...          parentData: <none>
...          constraints: MISSING
...          size: MISSING
...  label: RenderTransform#c189e NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...    parentData: offset=Offset(0.0,0.0)
...    constraints: MISSING
...    size: MISSING
...    transform matrix: [0] 1.0,0.0,-0.0
[1] 0.0,1.0,0.0
[2] 0.0,0.0
[3] 0.0,1.0
...    origin: null
...    alignment: null
...    textDirection: ltr
...    transformHitTests: true
...    child: RenderAnimatedOpacity#16d0b NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...      parentData: <none>
...      constraints: MISSING
...      size: MISSING
...      opacity: AnimationController#f80a1(⏮ 0.000; paused; for AnimatedOpacity)➩Cubic(0.40,0.00,0.20,1.00)➩Tween<double>(1.0 → null)➩1.0
...      child: RenderParagraph#211e5 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...        parentData: <none>
...        constraints: MISSING
...        size: MISSING
...        textAlign: center
...        textDirection: ltr
...        softWrap: wrapping at Box width
...        overflow: ellipsis
...        locale: en_US
...        maxLines: unlimited
...        text: TextSpan
...          debugLabel: ((englishLike subhead 2014).merge(blackMountainView subtitle1)).copyWith
...          inherit: false
...          color: Color(0x99000000)
...          family: Roboto
...          size: 16.0
...          weight: 400
...          baseline: alphabetic
...          decoration: Textdecoration.none
...          "Contract Date"
...  hint: RenderAnimatedOpacity#d7c0f NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...    parentData: offset=Offset(0.0,0.0)
...    constraints: MISSING
...    size: MISSING
...    opacity: AnimationController#02b53(⏮ 0.000; paused; for AnimatedOpacity)➩Cubic(0.40,1.00)➩Tween<double>(0.0 → null)➩0.0
...    alwaysIncludeSemantics
...    child: RenderParagraph#9b614 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...      parentData: <none>
...      constraints: MISSING
...      size: MISSING
...      textAlign: center
...      textDirection: ltr
...      softWrap: wrapping at Box width
...      overflow: ellipsis
...      locale: en_US
...      maxLines: 1
...      text: TextSpan
...        debugLabel: ((englishLike subhead 2014).merge(blackMountainView subtitle1)).copyWith
...        inherit: false
...        color: Color(0x99000000)
...        family: Roboto
...        size: 16.0
...        weight: 400
...        baseline: alphabetic
...        decoration: Textdecoration.none
...        "Contract Date"
...  helperError: RenderConstrainedBox#a8da3 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...    parentData: offset=Offset(0.0,0.0)
...    constraints: MISSING
...    size: MISSING
...    additionalConstraints: BoxConstraints(unconstrained)
...  container: RenderCustomPaint#7f6df NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...    parentData: offset=Offset(0.0,0.0)
...    constraints: MISSING
...    size: MISSING
====================================================================================================

======== Exception caught by rendering library =====================================================
The following assertion was thrown during performlayout():
RenderBox was not laid out: _Renderdecoration#3d795 relayoutBoundary=up5 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
'package:Flutter/src/rendering/Box.dart':
Failed assertion: line 1930 pos 12: 'hasSize'


Either the assertion indicates an error in the framework itself,this was the stack: 
#2      RenderBox.size (package:Flutter/src/rendering/Box.dart:1930:12)
#3      RenderProxyBoxMixin.performlayout (package:Flutter/src/rendering/proxy_Box.dart:117:21)
#4      RenderObject.layout (package:Flutter/src/rendering/object.dart:1779:7)
#5      RenderProxyBoxMixin.performlayout (package:Flutter/src/rendering/proxy_Box.dart:116:14)
#6      RenderObject.layout (package:Flutter/src/rendering/object.dart:1779:7)
...
The following RenderObject was being processed when the exception was fired: RenderPointerListener#48de7 relayoutBoundary=up4 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...  needs compositing
...  parentData: <none> (can use size)
...  constraints: BoxConstraints(0.0<=w<=Infinity,0.0<=h<=50.0)
...  size: MISSING
...  behavior: translucent
...  listeners: down
RenderObject: RenderPointerListener#48de7 relayoutBoundary=up4 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
  needs compositing
  parentData: <none> (can use size)
  constraints: BoxConstraints(0.0<=w<=Infinity,0.0<=h<=50.0)
  size: MISSING
  behavior: translucent
  listeners: down
...  child: _Renderdecoration#3d795 relayoutBoundary=up5 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...    needs compositing
...    parentData: <none> (can use size)
...    constraints: BoxConstraints(0.0<=w<=Infinity,0.0<=h<=50.0)
...    size: MISSING
...    input: RenderRepaintBoundary#5840c NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...      needs compositing
...      parentData: offset=Offset(0.0,0.0)
...      constraints: MISSING
...      size: MISSING
...      usefulness ratio: no metrics collected yet (never painted)
...      child: RenderMouseRegion#4b1e1 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...        needs compositing
...        parentData: <none>
...        constraints: MISSING
...        size: MISSING
...        listeners: <none>
...        child: RenderRepaintBoundary#0aae3 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...          needs compositing
...          parentData: <none>
...          constraints: MISSING
...          size: MISSING
...          usefulness ratio: no metrics collected yet (never painted)
...    label: RenderTransform#c189e NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...      parentData: offset=Offset(0.0,0.0)
...      constraints: MISSING
...      size: MISSING
...      transform matrix: [0] 1.0,1.0
...      origin: null
...      alignment: null
...      textDirection: ltr
...      transformHitTests: true
...      child: RenderAnimatedOpacity#16d0b NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...        parentData: <none>
...        constraints: MISSING
...        size: MISSING
...        opacity: AnimationController#f80a1(⏮ 0.000; paused; for AnimatedOpacity)➩Cubic(0.40,1.00)➩Tween<double>(1.0 → null)➩1.0
...        child: RenderParagraph#211e5 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...          parentData: <none>
...          constraints: MISSING
...          size: MISSING
...          textAlign: center
...          textDirection: ltr
...          softWrap: wrapping at Box width
...          overflow: ellipsis
...          locale: en_US
...          maxLines: unlimited
...    hint: RenderAnimatedOpacity#d7c0f NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...      parentData: offset=Offset(0.0,0.0)
...      constraints: MISSING
...      size: MISSING
...      opacity: AnimationController#02b53(⏮ 0.000; paused; for AnimatedOpacity)➩Cubic(0.40,1.00)➩Tween<double>(0.0 → null)➩0.0
...      alwaysIncludeSemantics
...      child: RenderParagraph#9b614 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...        parentData: <none>
...        constraints: MISSING
...        size: MISSING
...        textAlign: center
...        textDirection: ltr
...        softWrap: wrapping at Box width
...        overflow: ellipsis
...        locale: en_US
...        maxLines: 1
...        text: TextSpan
...          debugLabel: ((englishLike subhead 2014).merge(blackMountainView subtitle1)).copyWith
...          inherit: false
...          color: Color(0x99000000)
...          family: Roboto
...          size: 16.0
...          weight: 400
...          baseline: alphabetic
...          decoration: Textdecoration.none
...          "Contract Date"
...    helperError: RenderConstrainedBox#a8da3 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...      parentData: offset=Offset(0.0,0.0)
...      constraints: MISSING
...      size: MISSING
...      additionalConstraints: BoxConstraints(unconstrained)
...    container: RenderCustomPaint#7f6df NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...      parentData: offset=Offset(0.0,0.0)
...      constraints: MISSING
...      size: MISSING
====================================================================================================

这是我尝试使用的代码。也许有更好的方法可以做到这一点,但这是我发现的。

Container(
                  alignment: Alignment.centerLeft,child:
                    SizedBox(
                    height: 50.0,width: 150,child: Row(
                    children: <Widget>[
                    TextField(
                      keyboardType: TextInputType.text,controller: contractDateController,textAlign: TextAlign.center,onChanged: (value) {
                        trxnProvider.changecontractDate(value); //,loggedInUid);
                      },decoration:
                      Inputdecoration(
                          hintText: 'Contract Date',labelText: 'Contract Date'),),IconButton(onpressed: () {_selectDate(context);},icon: Icon(Icons.calendar_today)),],

解决方法

好吧,您正在尝试将一个 IconButton 放在一个 textField 旁边,该文本字段占用所有“宽度”而不对其提供任何约束。这就是为什么这些小部件之间会发生这些冲突的原因,因此,它们中的任何一个都不会出现。

我建议删除该行并将 IconButton 放在 textField 的 SuffixIcon 中。

Container(
      alignment: Alignment.centerLeft,child: SizedBox(
        height: 50.0,width: 150,child: TextField(
          keyboardType: TextInputType.text,textAlign: TextAlign.center,controller: contractDateController,onChanged: (value) {trxnProvider.changecontractDate(value); 
          //,loggedInUid);},decoration: InputDecoration(
            hintText: 'Contract Date',labelText: 'Contract Date',suffixIcon: IconButton(
              icon: Icon(
                Icons.calendar_today,),onPressed: () {_selectDate(context);},

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。