TextField需要方向性属性

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

我已经为文本字段添加了方向性功能,但却显示错误。

我试着为简单的认证页面创建了一个TextField,用于练习。我无法理解这里的问题,即使它返回一个材料,而且我也添加了一个方向性属性,但错误一直在发生。

下面的代码是main.dart文件。

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String email;
  String password;

  @override
  Widget build(BuildContext context) {
    return Material(
          child: Center(
        child: Column(
          mainAxisAlignment : MainAxisAlignment.center,
          children: <Widget>[

              TextField(
              textDirection: TextDirection.rtl,
              onChanged:(value){
                  email = value;
              },  
              ),        
        ]),
      ),
    );
  }
}

在终端显示的错误。

I/flutter (10175): The following assertion was thrown building TextField(textDirection: rtl, dirty, state:
I/flutter (10175): _TextFieldState#74c8d):
I/flutter (10175): No Directionality widget found.
I/flutter (10175): TextField widgets require a Directionality widget ancestor. 
I/flutter (10175): The specific widget that could not find a Directionality ancestor was:
I/flutter (10175):   TextField
I/flutter (10175): The ownership chain for the affected widget is: "TextField ← Column ← Center ← DefaultTextStyle ←
I/flutter (10175):   AnimatedDefaultTextStyle ← _InkFeatures-[GlobalKey#b1d6d ink renderer] ←
I/flutter (10175):   NotificationListener<LayoutChangedNotification> ← PhysicalModel ← AnimatedPhysicalModel ← Material
I/flutter (10175):   ← ⋯"
I/flutter (10175): Typically, the Directionality widget is introduced by the MaterialApp or WidgetsApp widget at the
I/flutter (10175): top of your application widget tree. It determines the ambient reading direction and is used, for
I/flutter (10175): example, to determine how to lay out text, how to interpret 
"start" and "end" values, and to resolve
I/flutter (10175): EdgeInsetsDirectional, AlignmentDirectional, and other *Directional objects.
I/flutter (10175): 
I/flutter (10175): The relevant error-causing widget was:
I/flutter (10175):   TextField file:///D:/Flutter_Projects/signup/lib/main.dart:26:15
I/flutter (10175):
I/flutter (10175): When the exception was thrown, this was the stack:
I/flutter (10175): #0      debugCheckHasDirectionality.<anonymous closure> (package:flutter/src/widgets/debug.dart:247:7)
I/flutter (10175): #1      debugCheckHasDirectionality (package:flutter/src/widgets/debug.dart:263:4)
I/flutter (10175): #2      _TextFieldState.build (package:flutter/src/material/text_field.dart:985:12)
I/flutter (10175): #3      StatefulElement.build (package:flutter/src/widgets/framework.dart:4628:28)
I/flutter (10175): #4      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4511:15)
I/flutter (10175): #5      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4684:11)
I/flutter (10175): #6      Element.rebuild (package:flutter/src/widgets/framework.dart:4227:5)
I/flutter (10175): #7      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4490:5)
I/flutter (10175): #8      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4675:11)
I/flutter (10175): #9      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4485:5)    
I/flutter (10175): #10     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3455:14)
I/flutter (10175): #11     MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5956:32)
I/flutter (10175): ...     Normal element mounting (54 frames)
I/flutter (10175): #65     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3455:14)
I/flutter (10175): #66     Element.updateChild (package:flutter/src/widgets/framework.dart:3223:18)
I/flutter (10175): #67     RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1132:16)
I/flutter (10175): #68     RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1103:5)
I/flutter (10175): #69     RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1045:17)
I/flutter (10175): #70     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2612:19)
I/flutter (10175): #71     RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1044:13)
I/flutter (10175): #72     WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:925:7)
I/flutter (10175): #73     WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:906:7)
I/flutter (10175): (elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
I/flutter (10175):
I/flutter (10175): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (10175): Another exception was thrown: A RenderFlex overflowed by 99317 pixels on the bottom.
flutter flutter-layout
2个回答
0
投票

使用MaterialApp代替Material可能会解决这个问题。


0
投票

用Scafold widget替换你的Material,你的构建方法应该是这样的。

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
          TextField(
            textDirection: TextDirection.rtl,
            onChanged: (value) {
               email = value;
            },
          ),
        ]),
      ),
    );
  }

© www.soinside.com 2019 - 2024. All rights reserved.