前提・実現したいこと
ShowDialogを別のファイルから呼び出したい。
発生している問題・エラーメッセージ
════════ Exception caught by rendering library ═════════════════════════════════ RenderBox was not laid out: RenderPhysicalShape#81ed4 relayoutBoundary=up2 'package:flutter/src/rendering/box.dart': Failed assertion: line 1940 pos 12: 'hasSize' The relevant error-causing widget was AlertDialog lib/conponets_widgets/show_alert_dialog.dart:35
show_alert_page.dart
class SignInPage extends StatelessWidget { SignInPage({@required this.auth}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Time Tricker.'), ), body: Container( alignment: Alignment.center, padding: const EdgeInsets.symmetric(horizontal: 22.0, vertical: 40.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( 'SigIn Page', style: TextStyle( fontSize: 22.0, fontWeight: FontWeight.bold, ), ), SizedBox( height: 32.0, ), SocialSignInButton( color: Colors.white, imagePath: 'images/simple-logo.png', text: Text( 'Show Alert.', style: DefaultButtonStyle(), ), onPressed: () async{ ***こちらは上記のエラーが発生する。*** showAlertDialog(context, title: 'test alert', content: 'sample text.', defaultActionText: 'Ok', cancelActionText: 'cancel'); }), SizedBox( height: 12.0, ), SocialSignInButton( color: Colors.indigo, imagePath: 'images/sample2-logo.png', text: Text( 'Show Alert2.', style: DefaultButtonStyle().copyWith(color: Colors.white), ), onPressed: () { ***こちらは正常に呼び出せる。*** showDialog( context: context, builder: (context) => AlertDialog( title: Text('Hello'), content: Text('Smaple text'), actions: <Widget>[ TextButton( onPressed: () => Navigator.of(context).pop(false), child: Text('cancel'), ), TextButton( onPressed: () => Navigator.of(context).pop(true), child: Text('ok'), ), ], ), ); }), ), ), ], ), ), ); } } TextStyle DefaultButtonStyle() { return TextStyle( fontSize: 18.0, fontWeight: FontWeight.w300, color: Colors.black87); }
###show_alert_dialog.dart
Future<bool> showAlertDialog( BuildContext context, { @required String title, @required String content, String cancelActionText, @required String defaultActionText, }) { if (!Platform.isIOS) { return showDialog( context: context, builder: (context) => AlertDialog( title: Text(title), content: Text(content), actions: <Widget>[ if (cancelActionText != null) ...{ TextButton( onPressed: () => Navigator.of(context).pop(false), child: Text(cancelActionText), ), }, TextButton( onPressed: () => Navigator.of(context).pop(true), child: Text(defaultActionText), ), ], ), ); } return showCupertinoDialog( context: context, builder: (context) => AlertDialog( title: Text(title), content: Text(content), actions: <Widget>[ CupertinoDialogAction( onPressed: () => Navigator.of(context).pop(true), child: Text(defaultActionText), ), ], ), ); }
iOS,Androidどちらもエラーが発生します。
###試したこと
show_alert_dialog.dart内を変更
showDialog( context: context, builder: (context) => AlertDialog( title: Text(title), ***Containerでラップして高さと幅を持たせる*** content: Container(width: 300, height: 400, child: Text(content)), actions: <Widget>[ if (cancelActionText != null) ...{ TextButton( onPressed: () => Navigator.of(context).pop(false), child: Text(cancelActionText), ), }, TextButton( onPressed: () => Navigator.of(context).pop(true), child: Text(defaultActionText), ), ], ), );
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。