skykkm0810 / MFC

연구노트용 Repository
0 stars 0 forks source link

220610 #4

Open skykkm0810 opened 2 years ago

skykkm0810 commented 2 years ago
  1. 부모 위젯에서 자식 위젯에 argument 전달

Myapp (Parent)


import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:mfc_user/component/button.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key);

void _testFunction() => print('test'); static const String _title = 'Flutter Code Sample';

@override Widget build(BuildContext context) { return MaterialApp( title: _title, home: Scaffold( appBar: AppBar(title: const Text(_title)), body: Center( child: StickyBtn(texts: '로그인하기', callback: _testFunction ), ), )); } }


> button.dart (child)

import 'package:flutter/material.dart';

class StickyBtn extends StatelessWidget {

const StickyBtn({Key? key, required this.texts,required this.callback}) : super(key: key); final String texts; final VoidCallback callback;

testFunction() => print('test');

@override Widget build(BuildContext context) { return ElevatedButton(onPressed: callback, child: Text(texts)); } }



### 참고
body: Center 에서 const를 붙이니 callback 함수가 null로 정의되어 넘어가지 않음 => const 제거 하고 함수전달. 필요!
skykkm0810 commented 2 years ago
  1. argument가 필수요소가 아닐때,

필수요소가 텍스트, 함수 / 불필요 요소 색 (배경 +글 )

class NormalBtn extends StatelessWidget {

  const NormalBtn({Key? key, required this.texts,required this.callback, this.background = Colors.lightBlue, this.color = Colors.white }) : super(key: key);
  final String texts;
  final VoidCallback callback;
  final Color background;
  final Color color;
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(

        onPressed: callback,
        style: ElevatedButton.styleFrom(
          primary: background,
          onPrimary: color,
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(20)),
          minimumSize: const Size.fromHeight(40),
          textStyle: const TextStyle(fontSize: 20 ,fontWeight: FontWeight.bold),
        ),
        child: Text(texts));
  }
}

필수요소는 required , 불필요는 final 선언 후 , required 없이 적고 기본값 적용.