如何根据屏幕尺寸调整Flutter小部件大小?

12 浏览
0 Comments

如何根据屏幕尺寸调整Flutter小部件大小?

我是一个刚开始使用flutter的初学者,我发现了这个包https://pub.dev/packages/flutter_calendar_carousel,我想要使用它。我试着在一个flutter web应用程序中使用它。我运行了示例代码,它能够正常工作,但是当我最大化窗口时,它的显示效果不好;大圆圈,与在小窗口中的显示效果不同(尺寸问题,缩放效果不好):

Maximized on an ultrawide monitor

我尝试使用AspectRatio小部件,它看起来好一些,但仍然太大。我不确定如何正确使用aspect ratio,但还有其他方法可以修复吗?简而言之,我希望日历能够根据屏幕的尺寸进行可读性和缩放。

以下是示例代码:

import 'package:flutter_calendar_carousel/flutter_calendar_carousel.dart'

show CalendarCarousel;

import 'package:flutter_calendar_carousel/classes/event.dart';

import 'package:flutter_calendar_carousel/classes/event_list.dart';

import 'package:intl/intl.dart' show DateFormat;

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

class MyApp extends StatelessWidget {

@override

Widget build(BuildContext context) {

return new MaterialApp(

title: 'dooboolab flutter calendar',

theme: new ThemeData(

primarySwatch: Colors.blue,

),

home: new MyHomePage(title: 'Flutter Calendar Carousel Example'),

);

}

}

class MyHomePage extends StatefulWidget {

MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override

_MyHomePageState createState() => new _MyHomePageState();

}

class _MyHomePageState extends State {

DateTime _currentDate = DateTime(2019, 2, 3);

DateTime _currentDate2 = DateTime(2019, 2, 3);

String _currentMonth = DateFormat.yMMM().format(DateTime(2019, 2, 3));

DateTime _targetDateTime = DateTime(2019, 2, 3);

static Widget _eventIcon = new Container(

decoration: new BoxDecoration(

color: Colors.white,

borderRadius: BorderRadius.all(Radius.circular(1000)),

border: Border.all(color: Colors.blue, width: 2.0)),

child: new Icon(

Icons.person,

color: Colors.amber,

),

);

EventList _markedDateMap = new EventList(

events: {

new DateTime(2019, 2, 10): [

new Event(

date: new DateTime(2019, 2, 10),

title: 'Event 1',

icon: _eventIcon,

dot: Container(

margin: EdgeInsets.symmetric(horizontal: 1.0),

color: Colors.red,

height: 5.0,

width: 5.0,

),

),

new Event(

date: new DateTime(2019, 2, 10),

title: 'Event 2',

icon: _eventIcon,

),

new Event(

date: new DateTime(2019, 2, 10),

title: 'Event 3',

icon: _eventIcon,

),

],

},

);

CalendarCarousel _calendarCarousel, _calendarCarouselNoHeader;

@override

void initState() {

_markedDateMap.add(

new DateTime(2019, 2, 25),

new Event(

date: new DateTime(2019, 2, 25),

title: 'Event 5',

icon: _eventIcon,

));

_markedDateMap.add(

new DateTime(2019, 2, 10),

new Event(

date: new DateTime(2019, 2, 10),

title: 'Event 4',

icon: _eventIcon,

));

_markedDateMap.addAll(new DateTime(2019, 2, 11), [

new Event(

date: new DateTime(2019, 2, 11),

title: 'Event 1',

icon: _eventIcon,

),

new Event(

date: new DateTime(2019, 2, 11),

title: 'Event 2',

icon: _eventIcon,

),

new Event(

date: new DateTime(2019, 2, 11),

title: 'Event 3',

icon: _eventIcon,

),

]);

super.initState();

}

@override

Widget build(BuildContext context) {

_calendarCarousel = CalendarCarousel(

onDayPressed: (DateTime date, List events) {

this.setState(() => _currentDate = date);

events.forEach((event) => print(event.title));

},

weekendTextStyle: TextStyle(

color: Colors.red,

),

thisMonthDayBorderColor: Colors.grey,

headerText: 'Custom Header',

weekFormat: true,

markedDatesMap: _markedDateMap,

height: 200.0,

selectedDateTime: _currentDate2,

showIconBehindDayText: true,

customGridViewPhysics: NeverScrollableScrollPhysics(),

markedDateShowIcon: true,

markedDateIconMaxShown: 2,

selectedDayTextStyle: TextStyle(

color: Colors.yellow,

),

todayTextStyle: TextStyle(

color: Colors.blue,

),

markedDateIconBuilder: (event) {

return event.icon;

},

minSelectedDate: _currentDate.subtract(Duration(days: 360)),

maxSelectedDate: _currentDate.add(Duration(days: 360)),

todayButtonColor: Colors.transparent,

todayBorderColor: Colors.green,

markedDateMoreShowTotal: true,

);

_calendarCarouselNoHeader = CalendarCarousel(

todayBorderColor: Colors.green,

onDayPressed: (DateTime date, List events) {

this.setState(() => _currentDate2 = date);

events.forEach((event) => print(event.title));

},

daysHaveCircularBorder: true,

showOnlyCurrentMonthDate: false,

weekendTextStyle: TextStyle(

color: Colors.red,

),

thisMonthDayBorderColor: Colors.grey,

weekFormat: false,

markedDatesMap: _markedDateMap,

height: 420.0,

selectedDateTime: _currentDate2,

targetDateTime: _targetDateTime,

customGridViewPhysics: NeverScrollableScrollPhysics(),

markedDateCustomShapeBorder: CircleBorder(

side: BorderSide(color: Colors.yellow)

),

markedDateCustomTextStyle: TextStyle(

fontSize: 18,

color: Colors.blue,

),

showHeader: false,

todayTextStyle: TextStyle(

color: Colors.blue,

),

todayButtonColor: Colors.yellow,

selectedDayTextStyle: TextStyle(

color: Colors.yellow,

),

minSelectedDate: _currentDate.subtract(Duration(days: 360)),

maxSelectedDate: _currentDate.add(Duration(days: 360)),

prevDaysTextStyle: TextStyle(

fontSize: 16,

color: Colors.pinkAccent,

),

inactiveDaysTextStyle: TextStyle(

color: Colors.tealAccent,

fontSize: 16,

),

onCalendarChanged: (DateTime date) {

this.setState(() {

_targetDateTime = date;

_currentMonth = DateFormat.yMMM().format(_targetDateTime);

});

},

onDayLongPressed: (DateTime date) {

print('long pressed date $date');

},

);

return new Scaffold(

appBar: new AppBar(

title: new Text(widget.title),

),

body: SingleChildScrollView(

child: Column(

crossAxisAlignment: CrossAxisAlignment.start,

mainAxisAlignment: MainAxisAlignment.start,

children: [

Container(

margin: EdgeInsets.symmetric(horizontal: 16.0),

child: _calendarCarousel,

),

Container(

margin: EdgeInsets.only(

top: 30.0,

bottom: 16.0,

left: 16.0,

right: 16.0,

),

child: new Row(

children: [

Expanded(

child: Text(

_currentMonth,

style: TextStyle(

fontWeight: FontWeight.bold,

fontSize: 24.0,

),

)),

FlatButton(

child: Text('PREV'),

onPressed: () {

setState(() {

_targetDateTime = DateTime(_targetDateTime.year, _targetDateTime.month -1);

_currentMonth = DateFormat.yMMM().format(_targetDateTime);

});

},

),

FlatButton(

child: Text('NEXT'),

onPressed: () {

setState(() {

_targetDateTime = DateTime(_targetDateTime.year, _targetDateTime.month +1);

_currentMonth = DateFormat.yMMM().format(_targetDateTime);

});

},

)

],

),

),

Container(

margin: EdgeInsets.symmetric(horizontal: 16.0),

child: _calendarCarouselNoHeader,

),

],

),

));

}

}

0