• 카테고리

    질문 & 답변
  • 세부 분야

    모바일 앱 개발

  • 해결 여부

    미해결

구글지도 사용하기 중 코드 질문..

23.09.13 17:20 작성 조회수 185

0

아래 코드에서 _HomeScreenState 클래스에서 appBar와 Body를 나누신 후에, 코드 정리중

appBar는 함수로, _CustomGoogleMap과 ChooCheckButton은 위젯으로 분리하여 만들어주셨는데, 그 이유가 뭔가요? appBar는 위젯으로 관리를 하면 안되는건가요?

 

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

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  static final LatLng companyLatLng = LatLng(37.5233273, 126.921252);
  static final CameraPosition initialPosition = CameraPosition(
    target: companyLatLng,
    zoom: 15,
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: renderAppbar(),
      body: Column(
        children: [
          _CustomGoogleMap(initialPosition: initialPosition),
          _ChoolCheckButton(),
        ],
      ),
    );
  }

  AppBar renderAppbar() {
    return AppBar(
      title: Text(
        '오늘도 출근',
        style: TextStyle(
          color: Colors.blue,
          fontWeight: FontWeight.w700,
        ),
      ),
      backgroundColor: Colors.white,
    );
  }
}

class _CustomGoogleMap extends StatelessWidget {
  final CameraPosition initialPosition;

  const _CustomGoogleMap({
    required this.initialPosition,
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Expanded(
      flex: 5,
      child: GoogleMap(
        mapType: MapType.normal,
        initialCameraPosition: initialPosition,
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Text(
        '출근',
      ),
    );
  }
}

답변 1

답변을 작성해보세요.

0

안녕하세요!

상관은 없습니다. 다만 AppBar는 일반 Widget이 아니라 PreferredSizedWidget을 Implement 해야합니다.

클래스로 만들경우 일반 위젯은 PreferredSizedWidget을 Implement 하지 않기 때문에 직접 implement 해줘야합니다.

직접 implement 해준다면 클래스로 생성해도 괜찮습니다.

감사합니다!