repo_id
stringlengths 21
168
| file_path
stringlengths 36
210
| content
stringlengths 1
9.98M
| __index_level_0__
int64 0
0
|
---|---|---|---|
mirrored_repositories/flutter-examples/todo_list_using_provider/lib | mirrored_repositories/flutter-examples/todo_list_using_provider/lib/notifiers/todo_list.dart | import 'package:flutter/foundation.dart';
import 'package:flutter_new_provider_todo/models/todo.dart';
/// This class represents a model
/// that notify listeners when it has some changes
class TodoList extends ChangeNotifier {
List<Todo> _list = new List<Todo>();
List<Todo> get list => _list;
void add(Todo newItem) {
this._list.add(newItem);
notifyListeners();
}
void remove(Todo removedItem) {
this._list.remove(removedItem);
notifyListeners();
}
void clearDoneItems() {
this._list.removeWhere((Todo element) => element.done);
notifyListeners();
}
}
| 0 |
mirrored_repositories/flutter-examples/todo_list_using_provider/lib | mirrored_repositories/flutter-examples/todo_list_using_provider/lib/widgets/todo_list_item_toggle_button.dart | import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../models/todo.dart';
class TodoListItemToggleButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
print('build Toggle Item Button');
return Consumer<Todo>(
builder: (context, todo, child) {
print('rebuilding Consumer Todo List Item Toggle Item Button');
return new RaisedButton(
onPressed: (){
todo.toggle();
},
color: todo.done ? Colors.blue : Colors.red,
child: new Icon(Icons.refresh, color: Colors.white70,),
);
},
);
}
}
| 0 |
mirrored_repositories/flutter-examples/todo_list_using_provider/lib | mirrored_repositories/flutter-examples/todo_list_using_provider/lib/widgets/todo_list_item_wdt.dart | import 'package:flutter/material.dart';
import 'todo_list_item_display.dart';
import 'todo_list_item_toggle_button.dart';
class TodoListItemWdt extends StatelessWidget {
@override
Widget build(BuildContext context) {
print('rebuilding ToDo List Item: ');
return new Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
TodoListItemDisplay(),
TodoListItemToggleButton(),
],
);
}
}
| 0 |
mirrored_repositories/flutter-examples/todo_list_using_provider/lib | mirrored_repositories/flutter-examples/todo_list_using_provider/lib/widgets/todo_list_item_display.dart | import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../models/todo.dart';
class TodoListItemDisplay extends StatelessWidget {
@override
Widget build(BuildContext context) {
print('rebuilding Todo List Item Display');
return Consumer<Todo>(
builder: (context, todo, child) {
print('rebuilding Consumer Todo List Item Display');
return Text(
'${todo.title}: ${todo.done}',
style: TextStyle(fontSize: 16.0),
);
},
);
}
}
| 0 |
mirrored_repositories/flutter-examples/todo_list_using_provider/lib | mirrored_repositories/flutter-examples/todo_list_using_provider/lib/widgets/todo_list_wdt.dart | import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../models/todo.dart';
import '../notifiers/todo_list.dart';
import '../widgets/todo_list_item_wdt.dart';
class TodoListWdt extends StatelessWidget {
@override
Widget build(BuildContext context) {
print('building Todo List Wdt');
return Consumer<TodoList>(
builder: (context, todoList, child) {
print('rebuilding Consumer Todo List Wdt');
return ListView.builder(
physics: BouncingScrollPhysics(),
itemCount: todoList.list.length,
itemBuilder: (context, index) {
return ChangeNotifierProvider<Todo>(
create: (context) => todoList.list[index],
child: new TodoListItemWdt()
);
},
);
},
);
}
}
| 0 |
mirrored_repositories/flutter-examples/todo_list_using_provider/lib | mirrored_repositories/flutter-examples/todo_list_using_provider/lib/widgets/add_button.dart | import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../models/todo.dart';
import '../notifiers/todo_list.dart';
class AddButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
final todoList = Provider.of<TodoList>(context, listen: false);
print('build IncrementButton');
return FloatingActionButton(
onPressed: () {
todoList.add(
new Todo(id: '${todoList.list.length}', title: 'tituloTodo${todoList.list.length}')
);
},
tooltip: 'Increment',
child: Icon(Icons.add),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/todo_list_using_provider/lib | mirrored_repositories/flutter-examples/todo_list_using_provider/lib/models/todo.dart | import 'package:flutter/material.dart';
class Todo extends ChangeNotifier {
final String id;
String title;
bool done;
Todo({@required this.id, this.title, this.done = false});
/// Toggles the value of the item and notify to listeners
void toggle() {
this.done = !this.done;
notifyListeners();
}
}
| 0 |
mirrored_repositories/flutter-examples/todo_list_using_provider | mirrored_repositories/flutter-examples/todo_list_using_provider/test/widget_test.dart | // This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_new_provider_todo/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
| 0 |
mirrored_repositories/flutter-examples/unit_testing | mirrored_repositories/flutter-examples/unit_testing/lib/helpers.dart | import 'package:unit_testing/api/Places.dart';
import 'package:unit_testing/model/location.dart';
class TouristPlaces {
static Future<List<Location>> getData() async {
// Here we can call a real API.
return await PlacesAPI().fetchAllPlaces();
}
}
class FormValidator {
static String validateEmail(String email) {
final pattern = RegExp(
r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+");
if (email.isEmpty) {
return "please enter email";
} else if (!pattern.hasMatch(email)) {
return "please enter valid email";
} else {
return null;
}
}
static String validatePassword(String password) {
if (password.isEmpty) {
return "please enter your password";
} else if (password.length < 8) {
return "minimum lenght of password must be 8 characters";
} else {
return null;
}
}
}
| 0 |
mirrored_repositories/flutter-examples/unit_testing | mirrored_repositories/flutter-examples/unit_testing/lib/main.dart | import 'package:flutter/material.dart';
import 'package:unit_testing/screens/home_screen.dart';
import 'package:unit_testing/screens/sign_in_screen.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Material App',
initialRoute: "/",
routes: {
"/": (context) => SignInScreen(),
"/homeScreen": (context) => HomeScreen(),
},
);
}
}
| 0 |
mirrored_repositories/flutter-examples/unit_testing/lib | mirrored_repositories/flutter-examples/unit_testing/lib/components/place_info_card.dart | import 'package:flutter/material.dart';
import 'package:unit_testing/model/location.dart';
class PlaceInfo extends StatelessWidget {
final Location data;
PlaceInfo({this.data});
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.all(20),
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(15),
boxShadow: [
BoxShadow(
offset: Offset(0, 10),
blurRadius: 20,
color: Colors.black45,
)
],
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
data.name + "\n" + data.country,
style: TextStyle(
fontSize: 25,
color: Colors.black,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
SizedBox(
height: 20,
),
Image.asset(
data.image,
fit: BoxFit.fill,
),
SizedBox(
height: 20,
),
Text(
data.info,
style: TextStyle(fontSize: 23),
),
],
),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/unit_testing/lib | mirrored_repositories/flutter-examples/unit_testing/lib/model/location.dart | import 'lat_long.dart';
class Location {
final int id;
final String name;
final String image;
final LatLong latlong;
final String country;
final String info;
Location({
this.id,
this.name,
this.image,
this.latlong,
this.country,
this.info,
});
}
| 0 |
mirrored_repositories/flutter-examples/unit_testing/lib | mirrored_repositories/flutter-examples/unit_testing/lib/model/lat_long.dart | class LatLong {
final double latitude;
final double longitude;
LatLong({this.latitude, this.longitude});
}
| 0 |
mirrored_repositories/flutter-examples/unit_testing/lib | mirrored_repositories/flutter-examples/unit_testing/lib/api/Places.dart | import 'package:unit_testing/model/lat_long.dart';
import 'package:unit_testing/model/location.dart';
class PlacesAPI {
final List<Location> _locations = [
Location(
id: 1,
name: "Kumarakom Backwaters",
country: "India",
info: "Kerala’s scenic backwaters, "
"edged with coconut palms, "
"lush green rice paddies and picturesque villages,"
"make for a beautiful escape from hectic city life.",
image: "assets/1.jpeg",
latlong: LatLong(
latitude: 9.9540358,
longitude: 76.2671037,
),
),
Location(
id: 2,
name: "Angel Falls",
country: "Venezuela",
info: "Venezuela overflows with natural wonders, "
"including the world's highest waterfall—the 3,212-foot cascades of Angel Falls,"
" located in the UNESCO-protected Canaima National Park. "
"Canaima is by far the country's most popular attraction,"
" and the falls stretch an astounding 19 times higher than Niagara Falls. ",
image: "assets/2.jpg",
latlong: LatLong(
latitude: 5.9689135,
longitude: -62.5376132,
),
),
Location(
id: 3,
name: "Avenue of the Baobabs",
country: "Madagascar",
info:
"Separated from continental Africa by 250 miles of water, Madagascar "
"is the greatest adventure"
" you haven't had yet. The island nation's secrets include giant moths, "
"bug-eyed lemurs, and places like the surreal Avenue of the Baobabs,"
" where the centuries-old trees reach heights of nearly 100 feet.",
image: "assets/3.jpg",
latlong: LatLong(
latitude: -20.2498059,
longitude: 44.4172047,
),
),
Location(
id: 4,
name: "Denali National Park",
country: " Alaska",
info: "Despite controversies over name changes and a shrinking elevation,"
" Denali's beauty is worth braving the extreme low temperatures. Make a "
"road trip out of your visit, seeing as "
"much of the 6 million acres of shimmering lakes and jagged mountains as you can.",
image: "assets/4.jpg",
latlong: LatLong(
latitude: 63.2092486,
longitude: -152.2366999,
),
),
];
Future<List<Location>> fetchAllPlaces() {
return Future.delayed(Duration(seconds: 3), () {
return _locations;
});
}
}
| 0 |
mirrored_repositories/flutter-examples/unit_testing/lib | mirrored_repositories/flutter-examples/unit_testing/lib/screens/sign_in_screen.dart | import 'package:flutter/material.dart';
import 'package:unit_testing/helpers.dart';
class SignInScreen extends StatefulWidget {
@override
_SignInScreenState createState() => _SignInScreenState();
}
class _SignInScreenState extends State<SignInScreen> {
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Material(
child: Padding(
padding: const EdgeInsets.all(40.0),
child: Form(
key: _formKey,
child: Column(
children: [
Icon(
Icons.account_circle,
size: 200,
),
SizedBox(
height: 50,
),
TextFormField(
validator: (value) => FormValidator.validateEmail(value),
decoration: InputDecoration(
hintText: "Email",
),
),
SizedBox(
height: 50,
),
TextFormField(
validator: (value) => FormValidator.validatePassword(value),
decoration: InputDecoration(
hintText: "Password",
),
),
SizedBox(
height: 50,
),
RaisedButton(
onPressed: () {
if (_formKey.currentState.validate()) {
Navigator.of(context).pushReplacementNamed('/homeScreen');
}
},
child: Text('Sign In'),
)
],
),
),
),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/unit_testing/lib | mirrored_repositories/flutter-examples/unit_testing/lib/screens/home_screen.dart | import 'package:flutter/material.dart';
import 'package:unit_testing/components/place_info_card.dart';
import 'package:unit_testing/model/location.dart';
import '../helpers.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Unit Test'),
centerTitle: true,
),
body: FutureBuilder(
future: TouristPlaces.getData(), // this functions calls API.
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
final List<Location> data = snapshot.data;
return ListView.builder(
physics: BouncingScrollPhysics(),
itemCount: data.length,
itemBuilder: (context, index) => PlaceInfo(
data: data[index],
),
);
} else {
return Center(child: CircularProgressIndicator());
}
},
),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/unit_testing/test | mirrored_repositories/flutter-examples/unit_testing/test/unit/places_test.dart | import 'package:flutter_test/flutter_test.dart';
import 'package:unit_testing/helpers.dart';
import 'package:unit_testing/model/location.dart';
void main() {
test(
"fetched places",
() async {
print("test is running");
print("called API for feching Locations");
final places = await TouristPlaces.getData();
expect(places.length, greaterThan(0));
for (var i in places) {
final Location place = i;
expect(place.id, isNotNull);
}
},
);
}
| 0 |
mirrored_repositories/flutter-examples/unit_testing/test | mirrored_repositories/flutter-examples/unit_testing/test/unit/formValidator_test.dart | import 'package:flutter_test/flutter_test.dart';
import 'package:unit_testing/helpers.dart';
void main() {
group(
'Email test',
() {
test(
'Valid Email test',
() {
final result = FormValidator.validateEmail("[email protected]");
expect(result, isNull);
},
);
test(
'Invalid Email test',
() {
final result = FormValidator.validateEmail("abcf");
expect(result, 'please enter valid email');
},
);
test(
'Empty email test',
() {
final result = FormValidator.validateEmail("");
expect(result, 'please enter email');
},
);
},
);
group(
'Password Test',
() {
test(
'Empty password test',
() {
final resutl = FormValidator.validatePassword("");
expect(resutl, 'please enter your password');
},
);
test(
'Valid password test',
() {
final resutl = FormValidator.validatePassword("hello@123456");
expect(resutl, isNull);
},
);
test(
'Invalid password test',
() {
final resutl = FormValidator.validatePassword("hello");
expect(resutl, 'minimum lenght of password must be 8 characters');
},
);
},
);
}
| 0 |
mirrored_repositories/flutter-examples/navigation_drawer | mirrored_repositories/flutter-examples/navigation_drawer/lib/main.dart | import 'package:flutter/material.dart';
import 'package:navigation_drawer/screens/account.dart';
import 'package:navigation_drawer/screens/home.dart';
import 'package:navigation_drawer/screens/settings.dart';
void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home: HomeScreen(), // route for home is '/' implicitly
routes: <String, WidgetBuilder>{
// define the routes
SettingsScreen.routeName: (BuildContext context) => SettingsScreen(),
AccountScreen.routeName: (BuildContext context) => AccountScreen(),
},
));
}
| 0 |
mirrored_repositories/flutter-examples/navigation_drawer/lib | mirrored_repositories/flutter-examples/navigation_drawer/lib/screens/account.dart | import 'package:flutter/material.dart';
class AccountScreen extends StatelessWidget {
static const String routeName = "/account";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Account"),
),
body: Container(
child: Center(
child: Text("Account Screen"),
)),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/navigation_drawer/lib | mirrored_repositories/flutter-examples/navigation_drawer/lib/screens/settings.dart | import 'package:flutter/material.dart';
class SettingsScreen extends StatelessWidget {
static const String routeName = "/settings";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Settings"),
),
body: Container(
child: Center(
child: Text("Settings Screen"),
)),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/navigation_drawer/lib | mirrored_repositories/flutter-examples/navigation_drawer/lib/screens/home.dart | import 'package:flutter/material.dart';
import 'package:navigation_drawer/screens/account.dart';
import 'package:navigation_drawer/screens/settings.dart';
class HomeScreen extends StatefulWidget {
@override
HomeScreenState createState() => HomeScreenState();
}
class HomeScreenState extends State<HomeScreen> {
Drawer getNavDrawer(BuildContext context) {
var headerChild = DrawerHeader(child: Text("Header"));
var aboutChild = AboutListTile(
child: Text("About"),
applicationName: "Application Name",
applicationVersion: "v1.0.0",
applicationIcon: Icon(Icons.adb),
icon: Icon(Icons.info));
ListTile getNavItem(var icon, String s, String routeName) {
return ListTile(
leading: Icon(icon),
title: Text(s),
onTap: () {
setState(() {
// pop closes the drawer
Navigator.of(context).pop();
// navigate to the route
Navigator.of(context).pushNamed(routeName);
});
},
);
}
var myNavChildren = [
headerChild,
getNavItem(Icons.settings, "Settings", SettingsScreen.routeName),
getNavItem(Icons.home, "Home", "/"),
getNavItem(Icons.account_box, "Account", AccountScreen.routeName),
aboutChild
];
ListView listView = ListView(children: myNavChildren);
return Drawer(
child: listView,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Navigation Drawer Example"),
),
body: Container(
child: Center(
child: Text("Home Screen"),
)),
// Set the nav drawer
drawer: getNavDrawer(context),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/navigation_drawer/android | mirrored_repositories/flutter-examples/navigation_drawer/android/lib/main.dart | import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
| 0 |
mirrored_repositories/flutter-examples/navigation_drawer/android | mirrored_repositories/flutter-examples/navigation_drawer/android/test/widget_test.dart | // This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:android/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
| 0 |
mirrored_repositories/flutter-examples/stateful_widget | mirrored_repositories/flutter-examples/stateful_widget/lib/main.dart | import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: MyButton(),
));
}
class MyButton extends StatefulWidget {
@override
MyButtonState createState() {
return MyButtonState();
}
}
class MyButtonState extends State<MyButton> {
int counter = 0;
List<String> strings = ['Flutter', 'is', 'cool', "and","awesome!"];
String displayedString = "Hello World!";
void onPressOfButton() {
setState(() {
displayedString = strings[counter];
counter = counter < 4 ? counter + 1 : 0;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Stateful Widget"),
backgroundColor: Colors.green,
),
body: Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(displayedString, style: TextStyle(fontSize: 40.0)),
Padding(padding: EdgeInsets.all(10.0)),
RaisedButton(
child: Text(
"Press me",
style: TextStyle(color: Colors.white),
),
color: Colors.red,
onPressed: onPressOfButton,
)
],
),
),
),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/stateful_widget/android | mirrored_repositories/flutter-examples/stateful_widget/android/lib/main.dart | import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
| 0 |
mirrored_repositories/flutter-examples/stateful_widget/android | mirrored_repositories/flutter-examples/stateful_widget/android/test/widget_test.dart | // This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:android/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
| 0 |
mirrored_repositories/flutter-examples/custom_home_drawer | mirrored_repositories/flutter-examples/custom_home_drawer/lib/main.dart | import 'package:custom_home_drawer/screen/home_screen.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomeScreen(),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/custom_home_drawer/lib | mirrored_repositories/flutter-examples/custom_home_drawer/lib/screen/home_screen.dart | import 'package:flutter/material.dart';
class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> with SingleTickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 350),
);
static const maxSlide = 265;
static const maxRotate = -0.1;
static const dragStartPosition = 80;
int _counter = 0;
bool _canDragged = false;
void _openCloseDrawer() => _controller.isDismissed ? _controller.forward() : _controller.reverse();
void _onDragStart(DragStartDetails details) {
bool isDragLeft = _controller.isDismissed && details.globalPosition.dx < dragStartPosition;
bool isDragRight = _controller.isCompleted && details.globalPosition.dx > dragStartPosition;
_canDragged = isDragLeft || isDragRight;
}
void _onDragUpdate(DragUpdateDetails details) {
if (_canDragged) {
var delta = (details.primaryDelta ?? 0) / maxSlide;
_controller.value += delta;
}
}
void _onDragEnd(DragEndDetails details) {
if (_controller.isDismissed || _controller.isCompleted) {
return;
}
if (details.velocity.pixelsPerSecond.dx.abs() >= 365) {
var visualVelocity = details.velocity.pixelsPerSecond.dx / MediaQuery.of(context).size.width;
_controller.fling(velocity: visualVelocity);
} else if (_controller.value < 0.5) {
_controller.reverse();
} else {
_controller.forward();
}
}
void _incrementCounter() => setState(() => _counter++);
@override
Widget build(BuildContext context) {
return GestureDetector(
onHorizontalDragStart: _onDragStart,
onHorizontalDragUpdate: _onDragUpdate,
onHorizontalDragEnd: _onDragEnd,
child: AnimatedBuilder(
animation: _controller,
builder: (context, child) {
var slide = maxSlide * _controller.value;
var rotate = maxRotate * _controller.value;
var scale = 1 - (_controller.value * 0.4);
return Stack(
children: [
_homeDrawer(context),
Transform(
transform: Matrix4.identity()
..translate(slide)
..rotateZ(rotate)
..scale(scale),
alignment: Alignment.centerLeft,
child: _homeView(context),
)
],
);
},
),
);
}
Widget _homeView(BuildContext context) => Scaffold(
appBar: AppBar(
leading: IconButton(
onPressed: _openCloseDrawer,
icon: const Icon(Icons.menu),
),
title: const Text("Home Page"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
Widget _homeDrawer(BuildContext context) => Material(
child: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
color: Colors.blueAccent,
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
FlutterLogo(
size: 180,
style: FlutterLogoStyle.horizontal,
textColor: Colors.white,
),
ListTile(
leading: Icon(Icons.home),
title: Text('Home'),
),
ListTile(
leading: Icon(Icons.favorite),
title: Text('Favorites'),
),
ListTile(
leading: Icon(Icons.settings),
title: Text('Settings'),
),
ListTile(
leading: Icon(Icons.info),
title: Text('About'),
),
],
),
),
);
}
| 0 |
mirrored_repositories/flutter-examples/custom_home_drawer | mirrored_repositories/flutter-examples/custom_home_drawer/test/widget_test.dart | // This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility in the flutter_test package. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:custom_home_drawer/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
| 0 |
mirrored_repositories/flutter-examples/using_listview | mirrored_repositories/flutter-examples/using_listview/lib/contact_page.dart | import 'package:flutter/material.dart';
import 'package:using_listview/contactlist/contact_list.dart';
import 'package:using_listview/contactlist/modal/contact.dart';
class ContactPage extends StatelessWidget {
_buildContactList() {
return <ContactModal>[
const ContactModal(
fullName: 'Romain Hoogmoed', email: '[email protected]'),
const ContactModal(
fullName: 'Emilie Olsen', email: '[email protected]'),
const ContactModal(
fullName: 'Nishant Srivastava', email: '[email protected]'),
const ContactModal(
fullName: 'Romain Hoogmoed', email: '[email protected]'),
const ContactModal(
fullName: 'Emilie Olsen', email: '[email protected]'),
const ContactModal(
fullName: 'Nishant Srivastava', email: '[email protected]'),
const ContactModal(
fullName: 'Romain Hoogmoed', email: '[email protected]'),
const ContactModal(
fullName: 'Emilie Olsen', email: '[email protected]'),
const ContactModal(
fullName: 'Nishant Srivastava', email: '[email protected]'),
];
}
@override
Widget build(BuildContext context) {
return Scaffold(body: ContactsList(_buildContactList()));
}
}
| 0 |
mirrored_repositories/flutter-examples/using_listview | mirrored_repositories/flutter-examples/using_listview/lib/main.dart | import 'package:flutter/material.dart';
import 'package:using_listview/contact_page.dart';
void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Text("Using Listview"),
),
body: ContactPage(),
),
));
}
| 0 |
mirrored_repositories/flutter-examples/using_listview/lib | mirrored_repositories/flutter-examples/using_listview/lib/contactlist/contact_list_item.dart | import 'package:flutter/material.dart';
import 'package:using_listview/contactlist/modal/contact.dart';
class ContactListItem extends StatelessWidget {
final ContactModal _contactModal;
ContactListItem(this._contactModal);
@override
Widget build(BuildContext context) {
return ListTile(
leading: CircleAvatar(child: Text(_contactModal.fullName[0])),
title: Text(_contactModal.fullName),
subtitle: Text(_contactModal.email));
}
}
| 0 |
mirrored_repositories/flutter-examples/using_listview/lib | mirrored_repositories/flutter-examples/using_listview/lib/contactlist/contact_list.dart | import 'package:flutter/material.dart';
import 'package:using_listview/contactlist/contact_list_item.dart';
import 'package:using_listview/contactlist/modal/contact.dart';
class ContactsList extends StatelessWidget {
final List<ContactModal> _contactModal;
ContactsList(this._contactModal);
@override
Widget build(BuildContext context) {
return ListView(
padding: EdgeInsets.symmetric(vertical: 8.0),
children: _buildContactsList(),
);
}
List<ContactListItem> _buildContactsList() {
return _contactModal
.map((contact) => ContactListItem(contact))
.toList();
}
}
| 0 |
mirrored_repositories/flutter-examples/using_listview/lib/contactlist | mirrored_repositories/flutter-examples/using_listview/lib/contactlist/modal/contact.dart | class ContactModal {
final String fullName;
final String email;
const ContactModal({this.fullName, this.email});
}
| 0 |
mirrored_repositories/flutter-examples/using_listview/android | mirrored_repositories/flutter-examples/using_listview/android/lib/main.dart | import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
| 0 |
mirrored_repositories/flutter-examples/using_listview/android | mirrored_repositories/flutter-examples/using_listview/android/test/widget_test.dart | // This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:android/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
| 0 |
mirrored_repositories/flutter-examples/enabling_splash_screen | mirrored_repositories/flutter-examples/enabling_splash_screen/lib/main.dart | import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
appBar: AppBar(
title: Text("Splash Screen Example"),
),
body: Center(
child: Text("Hello World"),
),
),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/enabling_splash_screen/android | mirrored_repositories/flutter-examples/enabling_splash_screen/android/lib/main.dart | import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
| 0 |
mirrored_repositories/flutter-examples/enabling_splash_screen/android | mirrored_repositories/flutter-examples/enabling_splash_screen/android/test/widget_test.dart | // This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:android/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
| 0 |
mirrored_repositories/flutter-examples/using_tabs | mirrored_repositories/flutter-examples/using_tabs/lib/main.dart | import 'package:flutter/material.dart';
import 'package:using_tabs/tabs/first.dart';
import 'package:using_tabs/tabs/second.dart';
import 'package:using_tabs/tabs/third.dart';
void main() {
runApp(MaterialApp(
// Title
title: "Using Tabs",
// Home
home: MyHome()));
}
class MyHome extends StatefulWidget {
@override
MyHomeState createState() => MyHomeState();
}
// SingleTickerProviderStateMixin is used for animation
class MyHomeState extends State<MyHome> with SingleTickerProviderStateMixin {
/*
*-------------------- Setup Tabs ------------------*
*/
// Create a tab controller
late TabController controller;
@override
void initState() {
super.initState();
// Initialize the Tab Controller
controller = TabController(length: 3, vsync: this);
}
@override
void dispose() {
// Dispose of the Tab Controller
controller.dispose();
super.dispose();
}
TabBar getTabBar() {
return TabBar(
tabs: <Tab>[
Tab(
// set icon to the tab
icon: Icon(Icons.favorite),
),
Tab(
icon: Icon(Icons.adb),
),
Tab(
icon: Icon(Icons.airport_shuttle),
),
],
// setup the controller
controller: controller,
);
}
TabBarView getTabBarView(var tabs) {
return TabBarView(
// Add tabs as widgets
children: tabs,
// set the controller
controller: controller,
);
}
/*
*-------------------- Setup the page by setting up tabs in the body ------------------*
*/
@override
Widget build(BuildContext context) {
return Scaffold(
// Appbar
appBar: AppBar(
// Title
title: Text("Using Tabs"),
// Set the background color of the App Bar
backgroundColor: Colors.blue,
// Set the bottom property of the Appbar to include a Tab Bar
bottom: getTabBar()),
// Set the TabBar view as the body of the Scaffold
body: getTabBarView(<Widget>[First(), Second(), Third()]));
}
}
| 0 |
mirrored_repositories/flutter-examples/using_tabs/lib | mirrored_repositories/flutter-examples/using_tabs/lib/tabs/third.dart | import 'package:flutter/material.dart';
class Third extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: Column(
// center the children
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
Icons.airport_shuttle,
size: 160.0,
color: Colors.blue,
),
Text("Third Tab")
],
),
),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/using_tabs/lib | mirrored_repositories/flutter-examples/using_tabs/lib/tabs/second.dart | import 'package:flutter/material.dart';
class Second extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: Column(
// center the children
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
Icons.adb,
size: 160.0,
color: Colors.green,
),
Text("Second Tab")
],
),
),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/using_tabs/lib | mirrored_repositories/flutter-examples/using_tabs/lib/tabs/first.dart | import 'package:flutter/material.dart';
class First extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: Column(
// center the children
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
Icons.favorite,
size: 160.0,
color: Colors.red,
),
Text("First Tab")
],
),
),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/using_tabs/android | mirrored_repositories/flutter-examples/using_tabs/android/lib/main.dart | import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
| 0 |
mirrored_repositories/flutter-examples/using_tabs/android | mirrored_repositories/flutter-examples/using_tabs/android/test/widget_test.dart | // This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:android/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
| 0 |
mirrored_repositories/flutter-examples/simple_material_app | mirrored_repositories/flutter-examples/simple_material_app/lib/main.dart | import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
// Title
title: "Simple Material App",
// Home
home: Scaffold(
// Appbar
appBar: AppBar(
// Title
title: Text("Simple Material App"),
),
// Body
body: Container(
// Center the content
child: Center(
// Add Text
child: Text("Hello World!"),
),
),
)));
} | 0 |
mirrored_repositories/flutter-examples/simple_material_app/android | mirrored_repositories/flutter-examples/simple_material_app/android/lib/main.dart | import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
| 0 |
mirrored_repositories/flutter-examples/simple_material_app/android | mirrored_repositories/flutter-examples/simple_material_app/android/test/widget_test.dart | // This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:android/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
| 0 |
mirrored_repositories/flutter-examples/news_memes_app | mirrored_repositories/flutter-examples/news_memes_app/lib/main.dart | import 'package:flutter/material.dart';
import 'package:news_memes_app/Screens/HomePage.dart';
void main() {
runApp(MaterialApp(
home: HomePage(),
));
}
| 0 |
mirrored_repositories/flutter-examples/news_memes_app/lib | mirrored_repositories/flutter-examples/news_memes_app/lib/Screens/MemeScreen.dart | import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:news_memes_app/services/getMemes.dart';
import 'package:share/share.dart';
class MemeScreen extends StatefulWidget {
const MemeScreen({Key? key}) : super(key: key);
@override
_MemeScreenState createState() => _MemeScreenState();
}
class _MemeScreenState extends State<MemeScreen> {
List<dynamic> memeUrl = ['loading'];
bool memeLoading = true;
int index = 0;
void getMeme() async {
memeUrl = await showMemes();
setState(() {
memeLoading = false;
});
}
@override
void initState() {
// TODO: implement initState
super.initState();
getMeme();
}
@override
Widget build(BuildContext context) {
final Size size = MediaQuery.of(context).size;
return Scaffold(
backgroundColor: Color(0xFF28496E),
appBar: AppBar(
title: Text(
'Memes',
style: GoogleFonts.kanit(),
),
centerTitle: true,
actions: [
IconButton(
onPressed: () {
Share.share(memeUrl[index]);
},
icon: Icon(Icons.share_rounded))
],
),
bottomNavigationBar: Row(
mainAxisAlignment: MainAxisAlignment.center,
//crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 20),
child: Text(
'Swipe Up for more!',
style: GoogleFonts.changa(
fontSize: 18,
),
),
),
],
),
body: memeLoading
? Center(
child: CircularProgressIndicator(
color: Colors.white,
),
)
: Dismissible(
key: Key(memeUrl[index]),
direction: DismissDirection.up,
onDismissed: (direction) {
if (direction == DismissDirection.up) {
setState(() {
index++;
if (index == 10) {
index = 0;
memeUrl = ['loading'];
memeLoading = true;
getMeme();
}
});
}
},
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
image:
DecorationImage(image: NetworkImage(memeUrl[index]))),
),
));
}
}
| 0 |
mirrored_repositories/flutter-examples/news_memes_app/lib | mirrored_repositories/flutter-examples/news_memes_app/lib/Screens/HomePage.dart | import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:news_memes_app/Screens/MemeScreen.dart';
import 'package:news_memes_app/Screens/NewsScreen.dart';
import 'package:news_memes_app/services/getNews.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List<NewsModel> news = [];
@override
Widget build(BuildContext context) {
final Size size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(
'News-Memes',
style: GoogleFonts.gafata(),
),
),
body: Container(
height: size.height,
width: size.width,
color: Colors.black,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(40),
child: Image.asset(
'images/breakingnews.jpg',
scale: 4,
)),
SizedBox(
height: 40,
),
ElevatedButton(
onPressed: () async {
print('Loading news');
news = await showNews();
setState(() {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NewsScreen(news)));
// showingNews = true;
});
},
child: Text(
'Read some News',
style: GoogleFonts.lato(),
),
),
SizedBox(
height: 70,
),
ClipRRect(
borderRadius: BorderRadius.circular(40),
child: Image.asset(
'images/meme.jpg',
scale: 3,
)),
SizedBox(
height: 40,
),
ElevatedButton(
onPressed: () async {
//memeUrl = await showMemes();
Navigator.push(context,
MaterialPageRoute(builder: (context) => MemeScreen()));
},
child: Text(
'Have Some Fun!',
style: GoogleFonts.lato(),
),
),
],
),
),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/news_memes_app/lib | mirrored_repositories/flutter-examples/news_memes_app/lib/Screens/NewsScreen.dart | import 'package:flutter/material.dart';
import 'package:news_memes_app/services/getNews.dart';
class NewsScreen extends StatefulWidget {
//const NewsScreen({Key? key}) : super(key: key);
List<NewsModel> news;
NewsScreen(this.news);
@override
_NewsScreenState createState() => _NewsScreenState(news);
}
class _NewsScreenState extends State<NewsScreen> {
List<NewsModel> news;
_NewsScreenState(this.news);
@override
Widget build(BuildContext context) {
final Size size = MediaQuery.of(context).size;
return Scaffold(
backgroundColor: Color(0xFF28496E),
body: Center(
child: Center(
child: InteractiveViewer(
child: ListView(
// magnification: 1.2,
children: news
.map((e) => Padding(
padding: const EdgeInsets.symmetric(
vertical: 15, horizontal: 20),
child: Container(
width: size.width * 0.88,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25),
color: Colors.white,
),
child: Column(
children: [
e.imageUrl == 'NoImage'
? Container(
height: 40,
color: Colors.grey,
child: Text(
'No Preview Available!',
style: TextStyle(
fontWeight: FontWeight.w300,
color: Colors.black38,
),
),
)
: Padding(
padding: const EdgeInsets.symmetric(
vertical: 10, horizontal: 10),
child: ClipRRect(
borderRadius:
BorderRadius.circular(30),
child: Image(
image: NetworkImage(e.imageUrl))),
),
SizedBox(
height: 15,
),
Padding(
padding:
const EdgeInsets.fromLTRB(10, 0, 10, 0),
child: Text(
e.title,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black54,
),
),
),
Padding(
padding:
const EdgeInsets.fromLTRB(10, 0, 10, 20),
child: Text(
e.description,
style: TextStyle(
fontWeight: FontWeight.w300,
color: Colors.black54,
),
),
)
],
),
),
))
.toList(),
),
),
),
),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/news_memes_app/lib | mirrored_repositories/flutter-examples/news_memes_app/lib/services/getMemes.dart | import 'dart:convert';
import 'package:http/http.dart' as http;
Future<List<dynamic>> showMemes() async {
Uri uri = Uri.parse('https://meme-api.herokuapp.com/gimme/10');
final response = await http.get(uri);
if (response.statusCode == 200 || response.statusCode == 201) {
print(response.body);
Map<String, dynamic> map = json.decode(response.body);
List<dynamic> memes = map['memes'];
List<dynamic> memeUrls =
memes.map((e) => e['preview'][e['preview'].length - 1]).toList();
return memeUrls;
} else {
print('Error - ${response.statusCode}');
return [''];
}
}
| 0 |
mirrored_repositories/flutter-examples/news_memes_app/lib | mirrored_repositories/flutter-examples/news_memes_app/lib/services/getNews.dart | import 'dart:convert';
import 'package:http/http.dart' as http;
final String apikey = 'YOUR_API_KEY';
Future<List<NewsModel>> showNews() async {
Uri uri = Uri.parse(
'https://newsapi.org/v2/top-headlines?country=in&apiKey=$apikey');
final response = await http.get(uri);
if (response.statusCode == 200 || response.statusCode == 201) {
print(response.body);
Map<String, dynamic> result = json.decode(response.body);
List articles = result['articles'];
print('Articles are - $articles');
List<NewsModel> news = articles.map((e) => NewsModel.fromjson(e)).toList();
return news;
} else {
print('Error - ${response.statusCode}');
return [];
}
}
class NewsModel {
String title, description, content, imageUrl, date;
NewsModel(
this.title, this.description, this.content, this.imageUrl, this.date);
factory NewsModel.fromjson(Map<String, dynamic> jsondata) {
return NewsModel(
jsondata['title'] ?? '',
jsondata['description'] ?? '',
jsondata['content'] ?? '',
jsondata['urlToImage'] ?? 'NoImage',
jsondata['publishedAt'] ?? '');
}
}
| 0 |
mirrored_repositories/flutter-examples/image_from_network | mirrored_repositories/flutter-examples/image_from_network/lib/main.dart | import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Image from Network"),
),
body: Container(
child: Column(
children: <Widget>[
// Load image from network
Image.network(
'https://github.com/nisrulz/flutter-examples/raw/develop/image_from_network/img/flutter_logo.png'),
// even loads gifs
// Gif image from Giphy, all copyrights are owned by Giphy
Image.network(
'https://github.com/nisrulz/flutter-examples/raw/develop/image_from_network/img/loop_anim.gif'),
],
)),
),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/image_from_network/android | mirrored_repositories/flutter-examples/image_from_network/android/lib/main.dart | import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
| 0 |
mirrored_repositories/flutter-examples/image_from_network/android | mirrored_repositories/flutter-examples/image_from_network/android/test/widget_test.dart | // This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:android/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
| 0 |
mirrored_repositories/flutter-examples/using_stepper | mirrored_repositories/flutter-examples/using_stepper/lib/main.dart | import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
// Title
title: "Simple Material App",
// Home
home: MyHome()));
}
class MyHome extends StatefulWidget {
@override
MyHomeState createState() => MyHomeState();
}
class MyHomeState extends State<MyHome> {
// init the step to 0th position
int current_step = 0;
List<Step> my_steps = [
Step(
// Title of the Step
title: Text("Step 1"),
// Content, it can be any widget here. Using basic Text for this example
content: Text("Hello!"),
isActive: true),
Step(
title: Text("Step 2"),
content: Text("World!"),
// You can change the style of the step icon i.e number, editing, etc.
state: StepState.editing,
isActive: true),
Step(
title: Text("Step 3"),
content: Text("Hello World!"),
isActive: true),
];
@override
Widget build(BuildContext context) {
return Scaffold(
// Appbar
appBar: AppBar(
// Title
title: Text("Simple Material App"),
),
// Body
body: Container(
child: Stepper(
// Using a variable here for handling the currentStep
currentStep: this.current_step,
// List the steps you would like to have
steps: my_steps,
// Define the type of Stepper style
// StepperType.horizontal : Horizontal Style
// StepperType.vertical : Vertical Style
type: StepperType.vertical,
// Know the step that is tapped
onStepTapped: (step) {
// On hitting step itself, change the state and jump to that step
setState(() {
// update the variable handling the current step value
// jump to the tapped step
current_step = step;
});
// Log function call
print("onStepTapped : " + step.toString());
},
onStepCancel: () {
// On hitting cancel button, change the state
setState(() {
// update the variable handling the current step value
// going back one step i.e subtracting 1, until its 0
if (current_step > 0) {
current_step = current_step - 1;
} else {
current_step = 0;
}
});
// Log function call
print("onStepCancel : " + current_step.toString());
},
// On hitting continue button, change the state
onStepContinue: () {
setState(() {
// update the variable handling the current step value
// going back one step i.e adding 1, until its the length of the step
if (current_step < my_steps.length - 1) {
current_step = current_step + 1;
} else {
current_step = 0;
}
});
// Log function call
print("onStepContinue : " + current_step.toString());
},
)),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/using_stepper/android | mirrored_repositories/flutter-examples/using_stepper/android/lib/main.dart | import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
| 0 |
mirrored_repositories/flutter-examples/using_stepper/android | mirrored_repositories/flutter-examples/using_stepper/android/test/widget_test.dart | // This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:android/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/flutter_date_pickers.dart | export 'package:flutter_date_pickers/src/date_period.dart';
export 'package:flutter_date_pickers/src/date_picker_keys.dart';
export 'package:flutter_date_pickers/src/layout_settings.dart';
export 'package:flutter_date_pickers/src/date_picker_styles.dart';
export 'package:flutter_date_pickers/src/event_decoration.dart';
export 'package:flutter_date_pickers/src/day_picker.dart';
export 'package:flutter_date_pickers/src/week_picker.dart';
export 'package:flutter_date_pickers/src/month_picker.dart';
export 'package:flutter_date_pickers/src/range_picker.dart';
export 'package:flutter_date_pickers/src/unselectable_period_error.dart';
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/range_picker.dart | import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'date_period.dart';
import 'date_picker_keys.dart';
import 'date_picker_styles.dart';
import 'day_based_changable_picker.dart';
import 'day_picker_selection.dart';
import 'day_type.dart';
import 'event_decoration.dart';
import 'i_selectable_picker.dart';
import 'layout_settings.dart';
import 'typedefs.dart';
/// Date picker for range selection.
class RangePicker extends StatelessWidget {
/// Creates a range picker.
RangePicker(
{Key? key,
required this.selectedPeriod,
required this.onChanged,
required this.firstDate,
required this.lastDate,
this.initiallyShowDate,
this.datePickerLayoutSettings = const DatePickerLayoutSettings(),
this.datePickerStyles,
this.datePickerKeys,
this.selectableDayPredicate,
this.onSelectionError,
this.eventDecorationBuilder,
this.onMonthChanged})
: assert(!firstDate.isAfter(lastDate)),
assert(!lastDate.isBefore(firstDate)),
assert(!selectedPeriod.start.isBefore(firstDate)),
assert(!selectedPeriod.end.isAfter(lastDate)),
assert(initiallyShowDate == null
|| !initiallyShowDate.isAfter(lastDate)),
assert(initiallyShowDate == null
|| !initiallyShowDate.isBefore(firstDate)),
super(key: key);
/// The currently selected period.
///
/// This date is highlighted in the picker.
final DatePeriod selectedPeriod;
/// Called when the user picks a week.
final ValueChanged<DatePeriod> onChanged;
/// Called when the error was thrown after user selection.
/// (e.g. when user selected a range with one or more days
/// that can't be selected)
final OnSelectionError? onSelectionError;
/// The earliest date the user is permitted to pick.
final DateTime firstDate;
/// The latest date the user is permitted to pick.
final DateTime lastDate;
/// Date for defining what month should be shown initially.
///
/// In case of null start of the [selectedPeriod] will be shown.
final DateTime? initiallyShowDate;
/// Layout settings what can be customized by user
final DatePickerLayoutSettings datePickerLayoutSettings;
/// Some keys useful for integration tests
final DatePickerKeys? datePickerKeys;
/// Styles what can be customized by user
final DatePickerRangeStyles? datePickerStyles;
/// Function returns if day can be selected or not.
final SelectableDayPredicate? selectableDayPredicate;
/// Builder to get event decoration for each date.
///
/// All event styles are overridden by selected styles
/// except days with dayType is [DayType.notSelected].
final EventDecorationBuilder? eventDecorationBuilder;
/// Called when the user changes the month.
/// New DateTime object represents first day of new month and 00:00 time.
final ValueChanged<DateTime>? onMonthChanged;
@override
Widget build(BuildContext context) {
ISelectablePicker<DatePeriod> rangeSelectablePicker = RangeSelectable(
selectedPeriod, firstDate, lastDate,
selectableDayPredicate: selectableDayPredicate);
return DayBasedChangeablePicker<DatePeriod>(
selectablePicker: rangeSelectablePicker,
selection: DayPickerRangeSelection(selectedPeriod),
firstDate: firstDate,
lastDate: lastDate,
initiallyShownDate: initiallyShowDate,
onChanged: onChanged,
onSelectionError: onSelectionError,
datePickerLayoutSettings: datePickerLayoutSettings,
datePickerStyles: datePickerStyles ?? DatePickerRangeStyles(),
datePickerKeys: datePickerKeys,
eventDecorationBuilder: eventDecorationBuilder,
onMonthChanged: onMonthChanged,
);
}
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/icon_btn.dart | import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
/// Icon button widget built different
/// depends on [MaterialApp] or [CupertinoApp] ancestor.
class IconBtn extends StatelessWidget {
/// Widget to use inside button.
///
/// Typically [Icon] widget.
final Widget icon;
/// Function called when user tap on the button.
///
/// Can be null. In this case button will be disabled.
final VoidCallback? onTap;
/// Tooltip for button.
///
/// Applied only for material style buttons.
/// It means only if widget has [MaterialApp] ancestor.
final String? tooltip;
/// Creates button with [icon] different
/// depends on [MaterialApp] or [CupertinoApp] ancestor.
const IconBtn({
Key? key,
required this.icon,
this.onTap,
this.tooltip
}) : super(key: key);
@override
Widget build(BuildContext context) {
bool isMaterial = Material.of(context) != null;
return isMaterial
? _materialBtn()
: _cupertinoBtn();
}
Widget _cupertinoBtn() =>
CupertinoButton(
padding: const EdgeInsets.all(0.0),
child: icon,
onPressed: onTap,
);
Widget _materialBtn() =>
IconButton(
icon: icon,
tooltip: tooltip ?? "",
onPressed: onTap,
);
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/day_based_changable_picker.dart | import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:intl/intl.dart' as intl;
import 'basic_day_based_widget.dart';
import 'date_picker_keys.dart';
import 'date_picker_styles.dart';
import 'day_based_changeable_picker_presenter.dart';
import 'day_picker_selection.dart';
import 'day_type.dart';
import 'event_decoration.dart';
import 'i_selectable_picker.dart';
import 'layout_settings.dart';
import 'month_navigation_row.dart';
import 'semantic_sorting.dart';
import 'typedefs.dart';
import 'utils.dart';
const Locale _defaultLocale = Locale('en', 'US');
/// Date picker based on [DayBasedPicker] picker (for days, weeks, ranges).
/// Allows select previous/next month.
class DayBasedChangeablePicker<T> extends StatefulWidget {
/// The currently selected date.
///
/// This date is highlighted in the picker.
final DayPickerSelection selection;
/// Called when the user picks a new T.
final ValueChanged<T> onChanged;
/// Called when the error was thrown after user selection.
final OnSelectionError? onSelectionError;
/// The earliest date the user is permitted to pick.
final DateTime firstDate;
/// The latest date the user is permitted to pick.
final DateTime lastDate;
/// Date for defining what month should be shown initially.
///
/// Default value is [selection.earliest].
final DateTime initiallyShowDate;
/// Layout settings what can be customized by user
final DatePickerLayoutSettings datePickerLayoutSettings;
/// Styles what can be customized by user
final DatePickerRangeStyles datePickerStyles;
/// Some keys useful for integration tests
final DatePickerKeys? datePickerKeys;
/// Logic for date selections.
final ISelectablePicker<T> selectablePicker;
/// Builder to get event decoration for each date.
///
/// All event styles are overridden by selected styles
/// except days with dayType is [DayType.notSelected].
final EventDecorationBuilder? eventDecorationBuilder;
/// Called when the user changes the month
final ValueChanged<DateTime>? onMonthChanged;
/// Create picker with option to change month.
DayBasedChangeablePicker(
{Key? key,
required this.selection,
required this.onChanged,
required this.firstDate,
required this.lastDate,
required this.datePickerLayoutSettings,
required this.datePickerStyles,
required this.selectablePicker,
DateTime? initiallyShownDate,
this.datePickerKeys,
this.onSelectionError,
this.eventDecorationBuilder,
this.onMonthChanged})
: initiallyShowDate = initiallyShownDate ?? selection.earliest,
super(key: key);
@override
State<DayBasedChangeablePicker<T>> createState() =>
_DayBasedChangeablePickerState<T>();
}
// todo: Check initial selection and call onSelectionError in case it has error
// todo: (ISelectablePicker.curSelectionIsCorrupted);
class _DayBasedChangeablePickerState<T>
extends State<DayBasedChangeablePicker<T>> {
DateTime _todayDate = DateTime.now();
Locale curLocale = _defaultLocale;
MaterialLocalizations localizations = _defaultLocalizations;
PageController _dayPickerController = PageController();
// Styles from widget fulfilled with current Theme.
DatePickerRangeStyles _resultStyles = DatePickerRangeStyles();
DayBasedChangeablePickerPresenter _presenter = _defaultPresenter;
Timer? _timer;
StreamSubscription<T>? _changesSubscription;
@override
void initState() {
super.initState();
// Initially display the pre-selected date.
final int monthPage = _getInitPage();
_dayPickerController = PageController(initialPage: monthPage);
_changesSubscription = widget.selectablePicker.onUpdate
.listen((newSelectedDate) => widget.onChanged(newSelectedDate))
..onError((e) => widget.onSelectionError != null
? widget.onSelectionError!.call(e)
: print(e.toString()));
_updateCurrentDate();
_initPresenter();
}
@override
void didUpdateWidget(DayBasedChangeablePicker<T> oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.datePickerStyles != oldWidget.datePickerStyles) {
final ThemeData theme = Theme.of(context);
_resultStyles = widget.datePickerStyles.fulfillWithTheme(theme);
}
if (widget.selectablePicker != oldWidget.selectablePicker) {
_changesSubscription = widget.selectablePicker.onUpdate
.listen((newSelectedDate) => widget.onChanged(newSelectedDate))
..onError((e) => widget.onSelectionError != null
? widget.onSelectionError!.call(e)
: print(e.toString()));
}
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
curLocale = Localizations.localeOf(context);
MaterialLocalizations? curLocalizations =
Localizations.of<MaterialLocalizations>(context, MaterialLocalizations);
if (curLocalizations != null && localizations != curLocalizations) {
localizations = curLocalizations;
_initPresenter();
}
final ThemeData theme = Theme.of(context);
_resultStyles = widget.datePickerStyles.fulfillWithTheme(theme);
}
@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
return SizedBox(
width: widget.datePickerLayoutSettings.monthPickerPortraitWidth,
height: widget.datePickerLayoutSettings.maxDayPickerHeight,
child: Column(
children: <Widget>[
widget.datePickerLayoutSettings.hideMonthNavigationRow
? const SizedBox()
: SizedBox(
height: widget.datePickerLayoutSettings.dayPickerRowHeight,
child: Padding(
//match _DayPicker main layout padding
padding: widget.datePickerLayoutSettings.contentPadding,
child: _buildMonthNavigationRow()),
),
Expanded(
child: Semantics(
sortKey: MonthPickerSortKey.calendar,
child: _buildDayPickerPageView(),
),
),
],
));
}
@override
void dispose() {
_timer?.cancel();
_dayPickerController.dispose();
_changesSubscription?.cancel();
widget.selectablePicker.dispose();
_presenter.dispose();
super.dispose();
}
void _updateCurrentDate() {
_todayDate = DateTime.now();
final DateTime tomorrow =
DateTime(_todayDate.year, _todayDate.month, _todayDate.day + 1);
Duration timeUntilTomorrow = tomorrow.difference(_todayDate);
timeUntilTomorrow +=
const Duration(seconds: 1); // so we don't miss it by rounding
_timer?.cancel();
_timer = Timer(timeUntilTomorrow, () {
setState(_updateCurrentDate);
});
}
// ignore: prefer_expression_function_bodies
Widget _buildMonthNavigationRow() {
return StreamBuilder<DayBasedChangeablePickerState>(
stream: _presenter.data,
initialData: _presenter.lastVal,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const SizedBox();
}
DayBasedChangeablePickerState state = snapshot.data!;
return MonthNavigationRow(
previousPageIconKey: widget.datePickerKeys?.previousPageIconKey,
nextPageIconKey: widget.datePickerKeys?.nextPageIconKey,
previousMonthTooltip: state.prevTooltip,
nextMonthTooltip: state.nextTooltip,
onPreviousMonthTapped:
state.isFirstMonth ? null : _presenter.gotoPrevMonth,
onNextMonthTapped:
state.isLastMonth ? null : _presenter.gotoNextMonth,
title: Text(
state.curMonthDis,
key: widget.datePickerKeys?.selectedPeriodKeys,
style: _resultStyles.displayedPeriodTitle,
),
nextIcon: widget.datePickerStyles.nextIcon,
prevIcon: widget.datePickerStyles.prevIcon,
);
});
}
Widget _buildDayPickerPageView() => PageView.builder(
controller: _dayPickerController,
scrollDirection: Axis.horizontal,
itemCount:
DatePickerUtils.monthDelta(widget.firstDate, widget.lastDate) + 1,
itemBuilder: _buildCalendar,
onPageChanged: _handleMonthPageChanged,
);
Widget _buildCalendar(BuildContext context, int index) {
final DateTime targetDate =
DatePickerUtils.addMonthsToMonthDate(widget.firstDate, index);
return DayBasedPicker(
key: ValueKey<DateTime>(targetDate),
selectablePicker: widget.selectablePicker,
currentDate: _todayDate,
firstDate: widget.firstDate,
lastDate: widget.lastDate,
displayedMonth: targetDate,
datePickerLayoutSettings: widget.datePickerLayoutSettings,
selectedPeriodKey: widget.datePickerKeys?.selectedPeriodKeys,
datePickerStyles: _resultStyles,
eventDecorationBuilder: widget.eventDecorationBuilder,
localizations: localizations,
);
}
// Returns appropriate date to be shown for init.
// If [widget.initiallyShowDate] is out of bounds [widget.firstDate]
// - [widget.lastDate], nearest bound will be used.
DateTime _getCheckedInitialDate() {
DateTime initiallyShowDateChecked = widget.initiallyShowDate;
if (initiallyShowDateChecked.isBefore(widget.firstDate)) {
initiallyShowDateChecked = widget.firstDate;
}
if (initiallyShowDateChecked.isAfter(widget.lastDate)) {
initiallyShowDateChecked = widget.lastDate;
}
return initiallyShowDateChecked;
}
int _getInitPage() {
final initialDate = _getCheckedInitialDate();
int initPage = DatePickerUtils.monthDelta(
widget.firstDate, initialDate
);
return initPage;
}
void _initPresenter() {
_presenter.dispose();
_presenter = DayBasedChangeablePickerPresenter(
firstDate: widget.firstDate,
lastDate: widget.lastDate,
localizations: localizations,
showPrevMonthDates: widget.datePickerLayoutSettings.showPrevMonthEnd,
showNextMonthDates: widget.datePickerLayoutSettings.showNextMonthStart,
firstDayOfWeekIndex: widget.datePickerStyles.firstDayOfeWeekIndex);
_presenter.data.listen(_onStateChanged);
// date used to define what month should be shown
DateTime initSelection = _getCheckedInitialDate();
// Give information about initial selection to presenter.
// It should be done after first frame when PageView is already created.
// Otherwise event from presenter will cause a error.
WidgetsBinding.instance!.addPostFrameCallback((_) {
_presenter.setSelectedDate(initSelection);
});
}
void _onStateChanged(DayBasedChangeablePickerState newState) {
DateTime newMonth = newState.currentMonth;
final int monthPage =
DatePickerUtils.monthDelta(widget.firstDate, newMonth);
_dayPickerController.animateToPage(monthPage,
duration: const Duration(milliseconds: 200), curve: Curves.easeInOut);
}
void _handleMonthPageChanged(int monthPage) {
DateTime firstMonth = widget.firstDate;
DateTime newMonth = DateTime(firstMonth.year, firstMonth.month + monthPage);
_presenter.changeMonth(newMonth);
widget.onMonthChanged?.call(newMonth);
}
static MaterialLocalizations get _defaultLocalizations =>
MaterialLocalizationEn(
twoDigitZeroPaddedFormat:
intl.NumberFormat('00', _defaultLocale.toString()),
fullYearFormat: intl.DateFormat.y(_defaultLocale.toString()),
longDateFormat: intl.DateFormat.yMMMMEEEEd(_defaultLocale.toString()),
shortMonthDayFormat: intl.DateFormat.MMMd(_defaultLocale.toString()),
decimalFormat:
intl.NumberFormat.decimalPattern(_defaultLocale.toString()),
shortDateFormat: intl.DateFormat.yMMMd(_defaultLocale.toString()),
mediumDateFormat: intl.DateFormat.MMMEd(_defaultLocale.toString()),
compactDateFormat: intl.DateFormat.yMd(_defaultLocale.toString()),
yearMonthFormat: intl.DateFormat.yMMMM(_defaultLocale.toString()),
);
static DayBasedChangeablePickerPresenter get _defaultPresenter =>
DayBasedChangeablePickerPresenter(
firstDate: DateTime.now(),
lastDate: DateTime.now(),
localizations: _defaultLocalizations,
showPrevMonthDates: false,
showNextMonthDates: false,
firstDayOfWeekIndex: 1);
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/date_picker_keys.dart | import 'package:flutter/material.dart';
/// Keys for some date picker's widgets.
///
/// Useful for integration tests to find widgets.
class DatePickerKeys {
/// Key for the previous page icon widget.
final Key previousPageIconKey;
/// Key for the next page icon widget.
final Key nextPageIconKey;
/// Key for showing month.
final Key selectedPeriodKeys;
///
DatePickerKeys(
this.previousPageIconKey, this.nextPageIconKey, this.selectedPeriodKeys);
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/basic_day_based_widget.dart | import 'package:flutter/material.dart';
import 'date_picker_mixin.dart';
import 'date_picker_styles.dart';
import 'day_type.dart';
import 'event_decoration.dart';
import 'i_selectable_picker.dart';
import 'layout_settings.dart';
import 'utils.dart';
/// Widget for date pickers based on days and cover entire month.
/// Each cell of this picker is day.
class DayBasedPicker<T> extends StatelessWidget with CommonDatePickerFunctions {
/// Selection logic.
final ISelectablePicker selectablePicker;
/// The current date at the time the picker is displayed.
final DateTime currentDate;
/// The earliest date the user is permitted to pick.
/// (only year, month and day matter, time doesn't matter)
final DateTime firstDate;
/// The latest date the user is permitted to pick.
/// (only year, month and day matter, time doesn't matter)
final DateTime lastDate;
/// The month whose days are displayed by this picker.
final DateTime displayedMonth;
/// Layout settings what can be customized by user
final DatePickerLayoutSettings datePickerLayoutSettings;
/// Key fo selected month (useful for integration tests)
final Key? selectedPeriodKey;
/// Styles what can be customized by user
final DatePickerRangeStyles datePickerStyles;
/// Builder to get event decoration for each date.
///
/// All event styles are overridden by selected styles
/// except days with dayType is [DayType.notSelected].
final EventDecorationBuilder? eventDecorationBuilder;
/// Localizations used to get strings for prev/next button tooltips,
/// weekday headers and display values for days numbers.
final MaterialLocalizations localizations;
/// Creates main date picker view where every cell is day.
DayBasedPicker(
{Key? key,
required this.currentDate,
required this.firstDate,
required this.lastDate,
required this.displayedMonth,
required this.datePickerLayoutSettings,
required this.datePickerStyles,
required this.selectablePicker,
required this.localizations,
this.selectedPeriodKey,
this.eventDecorationBuilder})
: assert(!firstDate.isAfter(lastDate)),
super(key: key);
@override
Widget build(BuildContext context) {
final List<Widget> labels = <Widget>[];
List<Widget> headers = _buildHeaders(localizations);
List<Widget> daysBeforeMonthStart = _buildCellsBeforeStart(localizations);
List<Widget> monthDays = _buildMonthCells(localizations);
List<Widget> daysAfterMonthEnd = _buildCellsAfterEnd(localizations);
labels.addAll(headers);
labels.addAll(daysBeforeMonthStart);
labels.addAll(monthDays);
labels.addAll(daysAfterMonthEnd);
return Padding(
padding: datePickerLayoutSettings.contentPadding,
child: Column(
children: <Widget>[
Flexible(
child: GridView.custom(
physics: datePickerLayoutSettings.scrollPhysics,
gridDelegate: datePickerLayoutSettings.dayPickerGridDelegate,
childrenDelegate:
SliverChildListDelegate(labels, addRepaintBoundaries: false),
),
),
],
),
);
}
List<Widget> _buildHeaders(MaterialLocalizations localizations) {
final int firstDayOfWeekIndex = datePickerStyles.firstDayOfeWeekIndex ??
localizations.firstDayOfWeekIndex;
DayHeaderStyleBuilder dayHeaderStyleBuilder =
datePickerStyles.dayHeaderStyleBuilder ??
// ignore: avoid_types_on_closure_parameters
(int i) => datePickerStyles.dayHeaderStyle;
List<Widget> headers = getDayHeaders(dayHeaderStyleBuilder,
localizations.narrowWeekdays, firstDayOfWeekIndex);
return headers;
}
List<Widget> _buildCellsBeforeStart(MaterialLocalizations localizations) {
List<Widget> result = [];
final int year = displayedMonth.year;
final int month = displayedMonth.month;
final int firstDayOfWeekIndex = datePickerStyles.firstDayOfeWeekIndex ??
localizations.firstDayOfWeekIndex;
final int firstDayOffset =
computeFirstDayOffset(year, month, firstDayOfWeekIndex);
final bool showDates = datePickerLayoutSettings.showPrevMonthEnd;
if (showDates) {
int prevMonth = month - 1;
if (prevMonth < 1) prevMonth = 12;
int prevYear = prevMonth == 12 ? year - 1 : year;
int daysInPrevMonth = DatePickerUtils.getDaysInMonth(prevYear, prevMonth);
List<Widget> days = List.generate(firstDayOffset, (index) => index)
.reversed
.map((i) => daysInPrevMonth - i)
.map((day) => _buildCell(prevYear, prevMonth, day))
.toList();
result = days;
} else {
result = List.generate(firstDayOffset, (_) => const SizedBox.shrink());
}
return result;
}
List<Widget> _buildMonthCells(MaterialLocalizations localizations) {
List<Widget> result = [];
final int year = displayedMonth.year;
final int month = displayedMonth.month;
final int daysInMonth = DatePickerUtils.getDaysInMonth(year, month);
for (int i = 1; i <= daysInMonth; i += 1) {
Widget dayWidget = _buildCell(year, month, i);
result.add(dayWidget);
}
return result;
}
List<Widget> _buildCellsAfterEnd(MaterialLocalizations localizations) {
List<Widget> result = [];
final bool showDates = datePickerLayoutSettings.showNextMonthStart;
if (!showDates) return result;
final int year = displayedMonth.year;
final int month = displayedMonth.month;
final int firstDayOfWeekIndex = datePickerStyles.firstDayOfeWeekIndex ??
localizations.firstDayOfWeekIndex;
final int firstDayOffset =
computeFirstDayOffset(year, month, firstDayOfWeekIndex);
final int daysInMonth = DatePickerUtils.getDaysInMonth(year, month);
final int totalFilledDays = firstDayOffset + daysInMonth;
int reminder = totalFilledDays % 7;
if (reminder == 0) return result;
final int emptyCellsNum = 7 - reminder;
int nextMonth = month + 1;
result = List.generate(emptyCellsNum, (i) => i + 1)
.map((day) => _buildCell(year, nextMonth, day))
.toList();
return result;
}
Widget _buildCell(int year, int month, int day) {
DateTime dayToBuild = DateTime(year, month, day);
dayToBuild = _checkDateTime(dayToBuild);
DayType dayType = selectablePicker.getDayType(dayToBuild);
Widget dayWidget = _DayCell(
day: dayToBuild,
currentDate: currentDate,
selectablePicker: selectablePicker,
datePickerStyles: datePickerStyles,
eventDecorationBuilder: eventDecorationBuilder,
localizations: localizations,
);
if (dayType != DayType.disabled) {
dayWidget = GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => selectablePicker.onDayTapped(dayToBuild),
child: dayWidget,
);
}
return dayWidget;
}
/// Checks if [DateTime] is same day as [lastDate] or [firstDate]
/// and returns dt corrected (with time of [lastDate] or [firstDate]).
DateTime _checkDateTime(DateTime dt) {
DateTime result = dt;
// If dayToBuild is the first day we need to save original time for it.
if (DatePickerUtils.sameDate(dt, firstDate)) result = firstDate;
// If dayToBuild is the last day we need to save original time for it.
if (DatePickerUtils.sameDate(dt, lastDate)) result = lastDate;
return result;
}
}
class _DayCell extends StatelessWidget {
/// Day for this cell.
final DateTime day;
/// Selection logic.
final ISelectablePicker selectablePicker;
/// Styles what can be customized by user
final DatePickerRangeStyles datePickerStyles;
/// The current date at the time the picker is displayed.
final DateTime currentDate;
/// Builder to get event decoration for each date.
///
/// All event styles are overridden by selected styles
/// except days with dayType is [DayType.notSelected].
final EventDecorationBuilder? eventDecorationBuilder;
final MaterialLocalizations localizations;
const _DayCell(
{Key? key,
required this.day,
required this.selectablePicker,
required this.datePickerStyles,
required this.currentDate,
required this.localizations,
this.eventDecorationBuilder})
: super(key: key);
@override
Widget build(BuildContext context) {
DayType dayType = selectablePicker.getDayType(day);
BoxDecoration? decoration;
TextStyle? itemStyle;
if (dayType != DayType.disabled && dayType != DayType.notSelected) {
itemStyle = _getSelectedTextStyle(dayType);
decoration = _getSelectedDecoration(dayType);
} else if (dayType == DayType.disabled) {
itemStyle = datePickerStyles.disabledDateStyle;
} else if (DatePickerUtils.sameDate(currentDate, day)) {
itemStyle = datePickerStyles.currentDateStyle;
} else {
itemStyle = datePickerStyles.defaultDateTextStyle;
}
// Merges decoration and textStyle with [EventDecoration].
//
// Merges only in cases if [dayType] is DayType.notSelected.
// If day is current day it is also gets event decoration
// instead of decoration for current date.
if (dayType == DayType.notSelected && eventDecorationBuilder != null) {
EventDecoration? eDecoration = eventDecorationBuilder != null
? eventDecorationBuilder!.call(day)
: null;
decoration = eDecoration?.boxDecoration ?? decoration;
itemStyle = eDecoration?.textStyle ?? itemStyle;
}
String semanticLabel = '${localizations.formatDecimal(day.day)}, '
'${localizations.formatFullDate(day)}';
bool daySelected =
dayType != DayType.disabled && dayType != DayType.notSelected;
Widget dayWidget = Container(
decoration: decoration,
child: Center(
child: Semantics(
// We want the day of month to be spoken first irrespective of the
// locale-specific preferences or TextDirection. This is because
// an accessibility user is more likely to be interested in the
// day of month before the rest of the date, as they are looking
// for the day of month. To do that we prepend day of month to the
// formatted full date.
label: semanticLabel,
selected: daySelected,
child: ExcludeSemantics(
child: Text(localizations.formatDecimal(day.day), style: itemStyle),
),
),
),
);
return dayWidget;
}
BoxDecoration? _getSelectedDecoration(DayType dayType) {
BoxDecoration? result;
if (dayType == DayType.single) {
result = datePickerStyles.selectedSingleDateDecoration;
} else if (dayType == DayType.start) {
result = datePickerStyles.selectedPeriodStartDecoration;
} else if (dayType == DayType.end) {
result = datePickerStyles.selectedPeriodLastDecoration;
} else {
result = datePickerStyles.selectedPeriodMiddleDecoration;
}
return result;
}
TextStyle? _getSelectedTextStyle(DayType dayType) {
TextStyle? result;
if (dayType == DayType.single) {
result = datePickerStyles.selectedDateStyle;
} else if (dayType == DayType.start) {
result = datePickerStyles.selectedPeriodStartTextStyle;
} else if (dayType == DayType.end) {
result = datePickerStyles.selectedPeriodEndTextStyle;
} else {
result = datePickerStyles.selectedPeriodMiddleTextStyle;
}
return result;
}
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/layout_settings.dart | import 'dart:math' as math;
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'day_picker.dart';
import 'month_picker.dart';
import 'range_picker.dart';
import 'week_picker.dart';
// layout defaults
const Duration _kPageScrollDuration = Duration(milliseconds: 200);
const double _kDayPickerRowHeight = 42.0;
const int _kMaxDayPickerRowCount = 6; // A 31 day month that starts on Saturday.
const double _kMonthPickerPortraitWidth = 330.0;
const EdgeInsetsGeometry _kContentPadding =
EdgeInsets.symmetric(horizontal: 8.0);
/// Settings for the layout of the [DayPicker], [WeekPicker], [RangePicker]
/// and [MonthPicker].
class DatePickerLayoutSettings {
/// Duration for scroll to previous or next page.
final Duration pagesScrollDuration;
/// Determines the scroll physics of a date picker widget.
///
/// Can be null. In this case default physics for [ScrollView] will be used.
final ScrollPhysics? scrollPhysics;
/// Height of the one row in picker including headers.
///
/// Default is [_kDayPickerRowHeight].
final double dayPickerRowHeight;
/// Width of the day based pickers.
final double monthPickerPortraitWidth;
///
final int maxDayPickerRowCount;
/// Padding for the entire picker.
final EdgeInsetsGeometry contentPadding;
/// If the first dates from the next month should be shown
/// to complete last week of the selected month.
///
/// false by default.
final bool showNextMonthStart;
/// If the last dates from the previous month should be shown
/// to complete first week of the selected month.
///
/// false by default.
final bool showPrevMonthEnd;
/// Hide Month navigation row
/// false by default.
final bool hideMonthNavigationRow;
/// Grid delegate for the picker according to [dayPickerRowHeight] and
/// [maxDayPickerRowCount].
SliverGridDelegate get dayPickerGridDelegate =>
_DayPickerGridDelegate(dayPickerRowHeight, maxDayPickerRowCount);
/// Maximum height of the day based picker according to [dayPickerRowHeight]
/// and [maxDayPickerRowCount].
///
/// Two extra rows:
/// one for the day-of-week header and one for the month header.
double get maxDayPickerHeight =>
dayPickerRowHeight * (maxDayPickerRowCount + 2);
/// Creates layout settings for the date picker.
///
/// Usually used in [DayPicker], [WeekPicker], [RangePicker]
/// and [MonthPicker].
const DatePickerLayoutSettings({
this.pagesScrollDuration = _kPageScrollDuration,
this.dayPickerRowHeight = _kDayPickerRowHeight,
this.monthPickerPortraitWidth = _kMonthPickerPortraitWidth,
this.maxDayPickerRowCount = _kMaxDayPickerRowCount,
this.contentPadding = _kContentPadding,
this.showNextMonthStart = false,
this.showPrevMonthEnd = false,
this.hideMonthNavigationRow = false,
this.scrollPhysics
});
}
class _DayPickerGridDelegate extends SliverGridDelegate {
final double _dayPickerRowHeight;
final int _maxDayPickerRowCount;
const _DayPickerGridDelegate(
this._dayPickerRowHeight, this._maxDayPickerRowCount);
@override
SliverGridLayout getLayout(SliverConstraints constraints) {
const int columnCount = DateTime.daysPerWeek;
final double tileWidth = constraints.crossAxisExtent / columnCount;
final double tileHeight = math.min(_dayPickerRowHeight,
constraints.viewportMainAxisExtent / (_maxDayPickerRowCount + 1));
return SliverGridRegularTileLayout(
crossAxisCount: columnCount,
mainAxisStride: tileHeight,
crossAxisStride: tileWidth,
childMainAxisExtent: tileHeight,
childCrossAxisExtent: tileWidth,
reverseCrossAxis: axisDirectionIsReversed(constraints.crossAxisDirection),
);
}
@override
bool shouldRelayout(SliverGridDelegate oldDelegate) => false;
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/date_period.dart | /// Date period.
class DatePeriod {
/// Start of this period.
final DateTime start;
/// End of this period.
final DateTime end;
///
const DatePeriod(this.start, this.end)
: assert(start != null),
assert(end != null);
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/date_picker_mixin.dart | import 'package:flutter/material.dart';
import 'date_picker_styles.dart';
///
mixin CommonDatePickerFunctions {
/// Builds widgets showing abbreviated days of week. The first widget in the
/// returned list corresponds to the first day of week for the current locale.
///
/// Examples:
///
/// ```
/// ┌ Sunday is the first day of week in the US (en_US)
/// |
/// S M T W T F S <-- the returned list contains these widgets
/// _ _ _ _ _ 1 2
/// 3 4 5 6 7 8 9
///
/// ┌ But it's Monday in the UK (en_GB)
/// |
/// M T W T F S S <-- the returned list contains these widgets
/// _ _ _ _ 1 2 3
/// 4 5 6 7 8 9 10
/// ```
List<Widget> getDayHeaders(
DayHeaderStyleBuilder headerStyleBuilder,
List<String> narrowWeekdays,
int firstDayOfWeekIndex) {
final List<Widget> result = <Widget>[];
for (int i = firstDayOfWeekIndex; true; i = (i + 1) % 7) {
DayHeaderStyle? headerStyle = headerStyleBuilder(i);
final String weekday = narrowWeekdays[i];
Widget header = ExcludeSemantics(
child: Container(
decoration: headerStyle?.decoration,
child: Center(
child: Text(
weekday,
style: headerStyle?.textStyle
)
),
),
);
result.add(header);
if (i == (firstDayOfWeekIndex - 1) % 7) {
break;
}
}
return result;
}
/// Computes the offset from the first day of week that the first day of the
/// [month] falls on.
///
/// For example, September 1, 2017 falls on a Friday, which in the calendar
/// localized for United States English appears as:
///
/// ```
/// S M T W T F S
/// _ _ _ _ _ 1 2
/// ```
///
/// The offset for the first day of the months is the number of leading blanks
/// in the calendar, i.e. 5.
///
/// The same date localized for the Russian calendar has a different offset,
/// because the first day of week is Monday rather than Sunday:
///
/// ```
/// M T W T F S S
/// _ _ _ _ 1 2 3
/// ```
///
/// So the offset is 4, rather than 5.
///
/// This code consolidates the following:
///
/// - [DateTime.weekday] provides a 1-based index into days of week, with 1
/// falling on Monday.
/// - [MaterialLocalizations.firstDayOfWeekIndex] provides a 0-based index
/// into the [MaterialLocalizations.narrowWeekdays] list.
/// - [MaterialLocalizations.narrowWeekdays] list provides localized names of
/// days of week, always starting with Sunday and ending with Saturday.
int computeFirstDayOffset(
int year, int month, int firstDayOfWeekFromSunday) {
// 0-based day of week, with 0 representing Monday.
final int weekdayFromMonday = DateTime(year, month).weekday - 1;
// firstDayOfWeekFromSunday recomputed to be Monday-based
final int firstDayOfWeekFromMonday = (firstDayOfWeekFromSunday - 1) % 7;
// Number of days between the first day of week appearing on the calendar,
// and the day corresponding to the 1-st of the month.
return (weekdayFromMonday - firstDayOfWeekFromMonday) % 7;
}
} | 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/month_navigation_row.dart | import 'package:flutter/material.dart';
import 'day_picker.dart' as day_picker;
import 'icon_btn.dart';
import 'range_picker.dart';
import 'semantic_sorting.dart';
import 'week_picker.dart';
/// Month navigation widget for day based date pickers like
/// [day_picker.DayPicker],
/// [WeekPicker],
/// [RangePicker].
///
/// It is row with [title] of showing month in the center and icons to selects
/// previous and next month around it.
class MonthNavigationRow extends StatelessWidget {
/// Key for previous page icon.
///
/// Can be useful in integration tests to find icon.
final Key? previousPageIconKey;
/// Key for next page icon.
///
/// Can be useful in integration tests to find icon.
final Key? nextPageIconKey;
/// Function called when [nextIcon] is tapped.
final VoidCallback? onNextMonthTapped;
/// Function called when [prevIcon] is tapped.
final VoidCallback? onPreviousMonthTapped;
/// Tooltip for the [nextIcon].
final String? nextMonthTooltip;
/// Tooltip for the [prevIcon].
final String? previousMonthTooltip;
/// Widget to use at the end of this row (after title).
final Widget nextIcon;
/// Widget to use at the beginning of this row (before title).
final Widget prevIcon;
/// Usually [Text] widget.
final Widget? title;
/// Creates month navigation row.
const MonthNavigationRow({
Key? key,
this.previousPageIconKey,
this.nextPageIconKey,
this.onNextMonthTapped,
this.onPreviousMonthTapped,
this.nextMonthTooltip,
this.previousMonthTooltip,
this.title,
required this.nextIcon,
required this.prevIcon
}) : super(key: key);
@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Semantics(
sortKey: MonthPickerSortKey.previousMonth,
child: IconBtn(
key: previousPageIconKey,
icon: prevIcon,
tooltip: previousMonthTooltip,
onTap: onPreviousMonthTapped,
),
),
Expanded(
child: Container(
alignment: Alignment.center,
child: Center(
child: ExcludeSemantics(
child: title,
),
),
),
),
Semantics(
sortKey: MonthPickerSortKey.nextMonth,
child: IconBtn(
key: nextPageIconKey,
icon: nextIcon,
tooltip: nextMonthTooltip,
onTap: onNextMonthTapped,
),
),
],
);
}
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/i_selectable_picker.dart | import 'dart:async';
import 'package:flutter/material.dart';
import 'date_period.dart';
import 'day_picker.dart' as day_picker;
import 'day_type.dart';
import 'range_picker.dart';
import 'unselectable_period_error.dart';
import 'utils.dart';
/// Interface for selection logic of the different date pickers.
///
/// T - is selection type.
abstract class ISelectablePicker<T> {
/// The earliest date the user is permitted to pick.
/// (only year, month and day matter, time doesn't matter)
final DateTime firstDate;
/// The latest date the user is permitted to pick.
/// (only year, month and day matter, time doesn't matter)
final DateTime lastDate;
/// Function returns if day can be selected or not.
final SelectableDayPredicate _selectableDayPredicate;
/// StreamController for new selection (T).
@protected
StreamController<T> onUpdateController = StreamController<T>.broadcast();
/// Stream with new selected (T) event.
///
/// Throws [UnselectablePeriodException]
/// if there is any custom disabled date in selected.
Stream<T> get onUpdate => onUpdateController.stream;
/// Constructor with required fields that used in non-abstract methods
/// ([isDisabled]).
ISelectablePicker(this.firstDate, this.lastDate,
{SelectableDayPredicate? selectableDayPredicate})
: _selectableDayPredicate =
selectableDayPredicate ?? _defaultSelectableDayPredicate;
/// If current selection exists and includes day/days that can't be selected
/// according to the [_selectableDayPredicate]'
bool get curSelectionIsCorrupted;
/// Returns [DayType] for given [day].
DayType getDayType(DateTime day);
/// Call when user tap on the day cell.
void onDayTapped(DateTime selectedDate);
/// Returns if given day is disabled.
///
/// Returns weather given day before the beginning of the [firstDate]
/// or after the end of the [lastDate].
///
/// If [_selectableDayPredicate] is set checks it as well.
@protected
bool isDisabled(DateTime day) {
final DateTime beginOfTheFirstDay =
DatePickerUtils.startOfTheDay(firstDate);
final DateTime endOfTheLastDay = DatePickerUtils.endOfTheDay(lastDate);
final bool customDisabled =
_selectableDayPredicate != null ? !_selectableDayPredicate(day) : false;
return day.isAfter(endOfTheLastDay) ||
day.isBefore(beginOfTheFirstDay) ||
customDisabled;
}
/// Closes [onUpdateController].
/// After it [onUpdateController] can't get new events.
void dispose() {
onUpdateController.close();
}
static bool _defaultSelectableDayPredicate(_) => true;
}
/// Selection logic for WeekPicker.
class WeekSelectable extends ISelectablePicker<DatePeriod> {
/// Initialized in ctor body.
late DateTime _firstDayOfSelectedWeek;
/// Initialized in ctor body.
late DateTime _lastDayOfSelectedWeek;
// It is int from 0 to 6 where 0 points to Sunday and 6 points to Saturday.
// According to MaterialLocalization.firstDayOfWeekIndex.
final int _firstDayOfWeekIndex;
@override
bool get curSelectionIsCorrupted => _checkCurSelection();
/// Creates selection logic for WeekPicker.
///
/// Entire week will be selected if
/// * it is between [firstDate] and [lastDate]
/// * it doesn't include unselectable days according to the
/// [selectableDayPredicate]
///
/// If one or more days of the week are before [firstDate]
/// first selection date will be the same as [firstDate].
///
/// If one or more days of the week are after [lastDate]
/// last selection date will be the same as [lastDate].
///
/// If one or more days of week are not selectable according to the
/// [selectableDayPredicate] nothing will be returned as selection
/// but [UnselectablePeriodException] will be thrown.
WeekSelectable(DateTime selectedDate, this._firstDayOfWeekIndex,
DateTime firstDate, DateTime lastDate,
{SelectableDayPredicate? selectableDayPredicate})
: super(firstDate, lastDate,
selectableDayPredicate: selectableDayPredicate) {
DatePeriod selectedWeek = _getNewSelectedPeriod(selectedDate);
_firstDayOfSelectedWeek = selectedWeek.start;
_lastDayOfSelectedWeek = selectedWeek.end;
_checkCurSelection();
}
@override
DayType getDayType(DateTime date) {
DayType result;
DatePeriod selectedPeriod =
DatePeriod(_firstDayOfSelectedWeek, _lastDayOfSelectedWeek);
bool selectedPeriodIsBroken =
_disabledDatesInPeriod(selectedPeriod).isNotEmpty;
if (isDisabled(date)) {
result = DayType.disabled;
} else if (_isDaySelected(date) && !selectedPeriodIsBroken) {
DateTime firstNotDisabledDayOfSelectedWeek =
_firstDayOfSelectedWeek.isBefore(firstDate)
? firstDate
: _firstDayOfSelectedWeek;
DateTime lastNotDisabledDayOfSelectedWeek =
_lastDayOfSelectedWeek.isAfter(lastDate)
? lastDate
: _lastDayOfSelectedWeek;
if (DatePickerUtils.sameDate(date, firstNotDisabledDayOfSelectedWeek) &&
DatePickerUtils.sameDate(date, lastNotDisabledDayOfSelectedWeek)) {
result = DayType.single;
} else if (DatePickerUtils.sameDate(date, _firstDayOfSelectedWeek) ||
DatePickerUtils.sameDate(date, firstDate)) {
result = DayType.start;
} else if (DatePickerUtils.sameDate(date, _lastDayOfSelectedWeek) ||
DatePickerUtils.sameDate(date, lastDate)) {
result = DayType.end;
} else {
result = DayType.middle;
}
} else {
result = DayType.notSelected;
}
return result;
}
@override
void onDayTapped(DateTime selectedDate) {
DatePeriod newPeriod = _getNewSelectedPeriod(selectedDate);
List<DateTime> customDisabledDays = _disabledDatesInPeriod(newPeriod);
customDisabledDays.isEmpty
? onUpdateController.add(newPeriod)
: onUpdateController.addError(
UnselectablePeriodException(customDisabledDays, newPeriod));
}
// Returns new selected period according to tapped date.
// Doesn't check custom disabled days.
// You have to check it separately if it needs.
DatePeriod _getNewSelectedPeriod(DateTime tappedDay) {
DatePeriod newPeriod;
DateTime firstDayOfTappedWeek =
DatePickerUtils.getFirstDayOfWeek(tappedDay, _firstDayOfWeekIndex);
DateTime lastDayOfTappedWeek =
DatePickerUtils.getLastDayOfWeek(tappedDay, _firstDayOfWeekIndex);
DateTime firstNotDisabledDayOfSelectedWeek =
firstDayOfTappedWeek.isBefore(firstDate)
? firstDate
: firstDayOfTappedWeek;
DateTime lastNotDisabledDayOfSelectedWeek =
lastDayOfTappedWeek.isAfter(lastDate) ? lastDate : lastDayOfTappedWeek;
newPeriod = DatePeriod(
firstNotDisabledDayOfSelectedWeek, lastNotDisabledDayOfSelectedWeek);
return newPeriod;
}
bool _isDaySelected(DateTime date) {
DateTime startOfTheStartDay =
DatePickerUtils.startOfTheDay(_firstDayOfSelectedWeek);
DateTime endOfTheLastDay =
DatePickerUtils.endOfTheDay(_lastDayOfSelectedWeek);
return !(date.isBefore(startOfTheStartDay) ||
date.isAfter(endOfTheLastDay));
}
List<DateTime> _disabledDatesInPeriod(DatePeriod period) {
List<DateTime> result = <DateTime>[];
var date = period.start;
while (!date.isAfter(period.end)) {
if (isDisabled(date)) result.add(date);
date = date.add(Duration(days: 1));
}
return result;
}
// Returns if current selection contains disabled dates.
// Returns false if there is no any selection.
bool _checkCurSelection() {
bool noSelection =
_firstDayOfSelectedWeek == null || _lastDayOfSelectedWeek == null;
if (noSelection) return false;
DatePeriod selectedPeriod =
DatePeriod(_firstDayOfSelectedWeek, _lastDayOfSelectedWeek);
List<DateTime> disabledDates = _disabledDatesInPeriod(selectedPeriod);
bool selectedPeriodIsBroken = disabledDates.isNotEmpty;
return selectedPeriodIsBroken;
}
}
/// Selection logic for [day_picker.DayPicker].
class DaySelectable extends ISelectablePicker<DateTime> {
/// Currently selected date.
DateTime selectedDate;
@override
bool get curSelectionIsCorrupted => _checkCurSelection();
/// Creates selection logic for [day_picker.DayPicker].
///
/// Every day can be selected if it is between [firstDate] and [lastDate]
/// and not unselectable according to the [selectableDayPredicate].
///
/// If day is not selectable according to the [selectableDayPredicate]
/// nothing will be returned as selection
/// but [UnselectablePeriodException] will be thrown.
DaySelectable(this.selectedDate, DateTime firstDate, DateTime lastDate,
{SelectableDayPredicate? selectableDayPredicate})
: super(firstDate, lastDate,
selectableDayPredicate: selectableDayPredicate);
@override
DayType getDayType(DateTime date) {
DayType result;
if (isDisabled(date)) {
result = DayType.disabled;
} else if (_isDaySelected(date)) {
result = DayType.single;
} else {
result = DayType.notSelected;
}
return result;
}
@override
void onDayTapped(DateTime selectedDate) {
DateTime newSelected = DatePickerUtils.sameDate(firstDate, selectedDate)
? selectedDate
: DateTime(selectedDate.year, selectedDate.month, selectedDate.day);
onUpdateController.add(newSelected);
}
bool _isDaySelected(DateTime date) =>
DatePickerUtils.sameDate(date, selectedDate);
// Returns if current selection is disabled
// according to the [_selectableDayPredicate].
//
// Returns false if there is no any selection.
bool _checkCurSelection() {
if (selectedDate == null) return false;
bool selectedIsBroken = _selectableDayPredicate(selectedDate);
return selectedIsBroken;
}
}
/// Selection logic for [day_picker.DayPicker] where many single days can be
/// selected.
class DayMultiSelectable extends ISelectablePicker<List<DateTime>> {
/// Currently selected dates.
List<DateTime> selectedDates;
/// Creates selection logic for [day_picker.DayPicker].
///
/// Every day can be selected if it is between [firstDate] and [lastDate]
/// and not unselectable according to the [selectableDayPredicate].
///
/// If day is not selectable according to the [selectableDayPredicate]
/// nothing will be returned as selection
/// but [UnselectablePeriodException] will be thrown.
DayMultiSelectable(this.selectedDates, DateTime firstDate, DateTime lastDate,
{SelectableDayPredicate? selectableDayPredicate})
: super(firstDate, lastDate,
selectableDayPredicate: selectableDayPredicate);
@override
bool get curSelectionIsCorrupted => _checkCurSelection();
@override
DayType getDayType(DateTime date) {
DayType result;
if (isDisabled(date)) {
result = DayType.disabled;
} else if (_isDaySelected(date)) {
result = DayType.single;
} else {
result = DayType.notSelected;
}
return result;
}
@override
void onDayTapped(DateTime selectedDate) {
bool alreadyExist =
selectedDates.any((d) => DatePickerUtils.sameDate(d, selectedDate));
if (alreadyExist) {
List<DateTime> newSelectedDates = List.from(selectedDates)
..removeWhere((d) => DatePickerUtils.sameDate(d, selectedDate));
onUpdateController.add(newSelectedDates);
} else {
DateTime newSelected = DatePickerUtils.sameDate(firstDate, selectedDate)
? selectedDate
: DateTime(selectedDate.year, selectedDate.month, selectedDate.day);
List<DateTime> newSelectedDates = List.from(selectedDates)
..add(newSelected);
onUpdateController.add(newSelectedDates);
}
}
bool _isDaySelected(DateTime date) =>
selectedDates.any((d) => DatePickerUtils.sameDate(date, d));
// Returns if current selection is disabled
// according to the [_selectableDayPredicate].
//
// Returns false if there is no any selection.
bool _checkCurSelection() {
if (selectedDates == null || selectedDates.isEmpty) return false;
bool selectedIsBroken = selectedDates.every(_selectableDayPredicate);
return selectedIsBroken;
}
}
/// Selection logic for [RangePicker].
class RangeSelectable extends ISelectablePicker<DatePeriod> {
/// Initially selected period.
DatePeriod selectedPeriod;
@override
bool get curSelectionIsCorrupted => _checkCurSelection();
/// Creates selection logic for [RangePicker].
///
/// Period can be selected if
/// * it is between [firstDate] and [lastDate]
/// * it doesn't include unselectable days according to the
/// [selectableDayPredicate]
///
///
/// If one or more days of the period are not selectable according to the
/// [selectableDayPredicate] nothing will be returned as selection
/// but [UnselectablePeriodException] will be thrown.
RangeSelectable(this.selectedPeriod, DateTime firstDate, DateTime lastDate,
{SelectableDayPredicate? selectableDayPredicate})
: super(firstDate, lastDate,
selectableDayPredicate: selectableDayPredicate);
@override
DayType getDayType(DateTime date) {
DayType result;
bool selectedPeriodIsBroken =
_disabledDatesInPeriod(selectedPeriod).isNotEmpty;
if (isDisabled(date)) {
result = DayType.disabled;
} else if (_isDaySelected(date) && !selectedPeriodIsBroken) {
if (DatePickerUtils.sameDate(date, selectedPeriod.start) &&
DatePickerUtils.sameDate(date, selectedPeriod.end)) {
result = DayType.single;
} else if (DatePickerUtils.sameDate(date, selectedPeriod.start) ||
DatePickerUtils.sameDate(date, firstDate)) {
result = DayType.start;
} else if (DatePickerUtils.sameDate(date, selectedPeriod.end) ||
DatePickerUtils.sameDate(date, lastDate)) {
result = DayType.end;
} else {
result = DayType.middle;
}
} else {
result = DayType.notSelected;
}
return result;
}
@override
void onDayTapped(DateTime selectedDate) {
DatePeriod newPeriod = _getNewSelectedPeriod(selectedDate);
List<DateTime> customDisabledDays = _disabledDatesInPeriod(newPeriod);
customDisabledDays.isEmpty
? onUpdateController.add(newPeriod)
: onUpdateController.addError(
UnselectablePeriodException(customDisabledDays, newPeriod));
}
// Returns new selected period according to tapped date.
DatePeriod _getNewSelectedPeriod(DateTime tappedDate) {
// check if was selected only one date and we should generate period
bool sameDate =
DatePickerUtils.sameDate(selectedPeriod.start, selectedPeriod.end);
DatePeriod newPeriod;
// Was selected one-day-period.
// With new user tap will be generated 2 dates as a period.
if (sameDate) {
// if user tap on the already selected single day
bool selectedAlreadySelectedDay =
DatePickerUtils.sameDate(tappedDate, selectedPeriod.end);
bool isSelectedFirstDay = DatePickerUtils.sameDate(tappedDate, firstDate);
bool isSelectedLastDay = DatePickerUtils.sameDate(tappedDate, lastDate);
if (selectedAlreadySelectedDay) {
if (isSelectedFirstDay && isSelectedLastDay) {
newPeriod = DatePeriod(firstDate, lastDate);
} else if (isSelectedFirstDay) {
newPeriod =
DatePeriod(firstDate, DatePickerUtils.endOfTheDay(firstDate));
} else if (isSelectedLastDay) {
newPeriod =
DatePeriod(DatePickerUtils.startOfTheDay(lastDate), lastDate);
} else {
newPeriod = DatePeriod(DatePickerUtils.startOfTheDay(tappedDate),
DatePickerUtils.endOfTheDay(tappedDate));
}
} else {
DateTime startOfTheSelectedDay =
DatePickerUtils.startOfTheDay(selectedPeriod.start);
if (!tappedDate.isAfter(startOfTheSelectedDay)) {
newPeriod = DatePickerUtils.sameDate(tappedDate, firstDate)
? DatePeriod(firstDate, selectedPeriod.end)
: DatePeriod(DatePickerUtils.startOfTheDay(tappedDate),
selectedPeriod.end);
} else {
newPeriod = DatePickerUtils.sameDate(tappedDate, lastDate)
? DatePeriod(selectedPeriod.start, lastDate)
: DatePeriod(selectedPeriod.start,
DatePickerUtils.endOfTheDay(tappedDate));
}
}
// Was selected 2 dates as a period.
// With new user tap new one-day-period will be generated.
} else {
bool sameAsFirst = DatePickerUtils.sameDate(tappedDate, firstDate);
bool sameAsLast = DatePickerUtils.sameDate(tappedDate, lastDate);
if (sameAsFirst && sameAsLast) {
newPeriod = DatePeriod(firstDate, lastDate);
} else if (sameAsFirst) {
newPeriod =
DatePeriod(firstDate, DatePickerUtils.endOfTheDay(firstDate));
} else if (sameAsLast) {
newPeriod =
DatePeriod(DatePickerUtils.startOfTheDay(tappedDate), lastDate);
} else {
newPeriod = DatePeriod(DatePickerUtils.startOfTheDay(tappedDate),
DatePickerUtils.endOfTheDay(tappedDate));
}
}
return newPeriod;
}
// Returns if current selection contains disabled dates.
// Returns false if there is no any selection.
bool _checkCurSelection() {
if (selectedPeriod == null) return false;
List<DateTime> disabledDates = _disabledDatesInPeriod(selectedPeriod);
bool selectedPeriodIsBroken = disabledDates.isNotEmpty;
return selectedPeriodIsBroken;
}
List<DateTime> _disabledDatesInPeriod(DatePeriod period) {
List<DateTime> result = <DateTime>[];
var date = period.start;
while (!date.isAfter(period.end)) {
if (isDisabled(date)) result.add(date);
date = date.add(Duration(days: 1));
}
return result;
}
bool _isDaySelected(DateTime date) {
DateTime startOfTheStartDay =
DatePickerUtils.startOfTheDay(selectedPeriod.start);
DateTime endOfTheLastDay = DatePickerUtils.endOfTheDay(selectedPeriod.end);
return !(date.isBefore(startOfTheStartDay) ||
date.isAfter(endOfTheLastDay));
}
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/week_picker.dart | import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'date_period.dart';
import 'date_picker_keys.dart';
import 'date_picker_styles.dart';
import 'day_based_changable_picker.dart';
import 'day_picker_selection.dart';
import 'day_type.dart';
import 'event_decoration.dart';
import 'i_selectable_picker.dart';
import 'layout_settings.dart';
import 'typedefs.dart';
/// Date picker for selection a week.
class WeekPicker extends StatelessWidget {
/// Creates a month picker.
WeekPicker(
{Key? key,
required this.selectedDate,
required this.onChanged,
required this.firstDate,
required this.lastDate,
this.initiallyShowDate,
this.datePickerLayoutSettings = const DatePickerLayoutSettings(),
this.datePickerStyles,
this.datePickerKeys,
this.selectableDayPredicate,
this.onSelectionError,
this.eventDecorationBuilder,
this.onMonthChanged})
: assert(!firstDate.isAfter(lastDate)),
assert(!lastDate.isBefore(firstDate)),
assert(!selectedDate.isBefore(firstDate)),
assert(!selectedDate.isAfter(lastDate)),
assert(initiallyShowDate == null
|| !initiallyShowDate.isAfter(lastDate)),
assert(initiallyShowDate == null
|| !initiallyShowDate.isBefore(firstDate)),
super(key: key);
/// The currently selected date.
///
/// This date is highlighted in the picker.
final DateTime selectedDate;
/// Called when the user picks a week.
final ValueChanged<DatePeriod> onChanged;
/// Called when the error was thrown after user selection.
/// (e.g. when user selected a week with one or more days
/// what can't be selected)
final OnSelectionError? onSelectionError;
/// The earliest date the user is permitted to pick.
final DateTime firstDate;
/// The latest date the user is permitted to pick.
final DateTime lastDate;
/// Date for defining what month should be shown initially.
///
/// In case of null month with earliest date of the selected week
/// will be shown.
final DateTime? initiallyShowDate;
/// Layout settings what can be customized by user
final DatePickerLayoutSettings datePickerLayoutSettings;
/// Some keys useful for integration tests
final DatePickerKeys? datePickerKeys;
/// Styles what can be customized by user
final DatePickerRangeStyles? datePickerStyles;
/// Function returns if day can be selected or not.
final SelectableDayPredicate? selectableDayPredicate;
/// Builder to get event decoration for each date.
///
/// All event styles are overriden by selected styles
/// except days with dayType is [DayType.notSelected].
final EventDecorationBuilder? eventDecorationBuilder;
/// Called when the user changes the month.
/// New DateTime object represents first day of new month and 00:00 time.
final ValueChanged<DateTime>? onMonthChanged;
@override
Widget build(BuildContext context) {
MaterialLocalizations localizations = MaterialLocalizations.of(context);
int firstDayOfWeekIndex = datePickerStyles?.firstDayOfeWeekIndex ??
localizations.firstDayOfWeekIndex;
ISelectablePicker<DatePeriod> weekSelectablePicker = WeekSelectable(
selectedDate, firstDayOfWeekIndex, firstDate, lastDate,
selectableDayPredicate: selectableDayPredicate);
return DayBasedChangeablePicker<DatePeriod>(
selectablePicker: weekSelectablePicker,
// todo: maybe create selection for week
// todo: and change logic here to work with it
selection: DayPickerSingleSelection(selectedDate),
firstDate: firstDate,
lastDate: lastDate,
initiallyShownDate: initiallyShowDate,
onChanged: onChanged,
onSelectionError: onSelectionError,
datePickerLayoutSettings: datePickerLayoutSettings,
datePickerStyles: datePickerStyles ?? DatePickerRangeStyles(),
datePickerKeys: datePickerKeys,
eventDecorationBuilder: eventDecorationBuilder,
onMonthChanged: onMonthChanged,
);
}
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/unselectable_period_error.dart | import 'date_period.dart';
import 'range_picker.dart';
import 'week_picker.dart';
/// Exception thrown when selected period contains custom disabled days.
class UnselectablePeriodException implements Exception {
/// Dates inside selected period what can't be selected
/// according custom rules.
final List<DateTime> customDisabledDates;
/// Selected period wanted by the user.
final DatePeriod period;
/// Creates exception that stores dates that can not be selected.
///
/// See also:
/// *[WeekPicker.onSelectionError]
/// *[RangePicker.onSelectionError]
UnselectablePeriodException(this.customDisabledDates, this.period);
@override
String toString() =>
"UnselectablePeriodException:"
" ${customDisabledDates.length} dates inside selected period "
"(${period.start} - ${period.end}) "
"can't be selected according custom rules (selectable pridicate). "
"Check 'customDisabledDates' property "
"to get entire list of such dates.";
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/day_based_changeable_picker_presenter.dart | import 'dart:async';
import 'package:flutter/material.dart';
import 'day_based_changable_picker.dart';
import 'utils.dart';
/// Presenter for [DayBasedChangeablePicker] to handle month changes.
class DayBasedChangeablePickerPresenter {
/// First date user can select.
final DateTime firstDate;
/// Last date user can select.
final DateTime lastDate;
/// Localization.
final MaterialLocalizations localizations;
/// If empty day cells before 1st day of showing month should be filled with
/// date from the last week of the previous month.
final bool showPrevMonthDates;
/// If empty day cells after last day of showing month should be filled with
/// date from the first week of the next month.
final bool showNextMonthDates;
/// Index of the first day in week.
/// 0 is Sunday, 6 is Saturday.
final int firstDayOfWeekIndex;
/// View model stream for the [DayBasedChangeablePicker].
Stream<DayBasedChangeablePickerState> get data => _controller.stream;
/// Last view model state of the [DayBasedChangeablePicker].
DayBasedChangeablePickerState? get lastVal => _lastVal;
/// Creates presenter to use for [DayBasedChangeablePicker].
DayBasedChangeablePickerPresenter({
required this.firstDate,
required this.lastDate,
required this.localizations,
required this.showPrevMonthDates,
required this.showNextMonthDates,
int? firstDayOfWeekIndex
}): firstDayOfWeekIndex = firstDayOfWeekIndex
?? localizations.firstDayOfWeekIndex;
/// Update state according to the [selectedDate] if it needs.
void setSelectedDate(DateTime selectedDate) {
// bool firstAndLastNotNull = _firstShownDate != null
// && _lastShownDate != null;
//
// bool selectedOnCurPage = firstAndLastNotNull
// && !selectedDate.isBefore(_firstShownDate)
// && !selectedDate.isAfter(_lastShownDate);
// if (selectedOnCurPage) return;
changeMonth(selectedDate);
}
/// Update state to show previous month.
void gotoPrevMonth() {
DateTime oldCur = _lastVal!.currentMonth;
DateTime newCurDate = DateTime(oldCur.year, oldCur.month - 1);
changeMonth(newCurDate);
}
/// Update state to show next month.
void gotoNextMonth() {
DateTime oldCur = _lastVal!.currentMonth;
DateTime newCurDate = DateTime(oldCur.year, oldCur.month + 1);
changeMonth(newCurDate);
}
/// Update state to change month to the [newMonth].
void changeMonth(DateTime newMonth) {
bool sameMonth = _lastVal != null
&& DatePickerUtils.sameMonth(_lastVal!.currentMonth, newMonth);
if (sameMonth) return;
int monthPage = DatePickerUtils.monthDelta(firstDate, newMonth);
DateTime prevMonth = DatePickerUtils
.addMonthsToMonthDate(firstDate, monthPage - 1);
DateTime curMonth = DatePickerUtils
.addMonthsToMonthDate(firstDate, monthPage);
DateTime nextMonth = DatePickerUtils
.addMonthsToMonthDate(firstDate, monthPage + 1);
String prevMonthStr = localizations.formatMonthYear(prevMonth);
String curMonthStr = localizations.formatMonthYear(curMonth);
String nextMonthStr = localizations.formatMonthYear(nextMonth);
bool isFirstMonth = DatePickerUtils.sameMonth(curMonth, firstDate);
bool isLastMonth = DatePickerUtils.sameMonth(curMonth, lastDate);
String? prevTooltip = isFirstMonth
? null
: "${localizations.previousMonthTooltip} $prevMonthStr";
String? nextTooltip = isLastMonth
? null
: "${localizations.nextMonthTooltip} $nextMonthStr";
DayBasedChangeablePickerState newState = DayBasedChangeablePickerState(
currentMonth: curMonth,
curMonthDis: curMonthStr,
prevMonthDis: prevMonthStr,
nextMonthDis: nextMonthStr,
prevTooltip: prevTooltip,
nextTooltip: nextTooltip,
isFirstMonth: isFirstMonth,
isLastMonth: isLastMonth
);
_updateState(newState);
}
/// Closes controller.
void dispose () {
_controller.close();
}
void _updateState(DayBasedChangeablePickerState newState) {
_lastVal = newState;
_controller.add(newState);
}
final StreamController<DayBasedChangeablePickerState> _controller =
StreamController.broadcast();
DayBasedChangeablePickerState? _lastVal;
}
/// View Model for the [DayBasedChangeablePicker].
class DayBasedChangeablePickerState {
/// Display name of the current month.
final String curMonthDis;
/// Display name of the previous month.
final String prevMonthDis;
/// Display name of the next month.
final String nextMonthDis;
/// Tooltip for the previous month icon.
final String? prevTooltip;
/// Tooltip for the next month icon.
final String? nextTooltip;
/// Tooltip for the current month icon.
final DateTime currentMonth;
/// If selected month is the month contains last date user can select.
final bool isLastMonth;
/// If selected month is the month contains first date user can select.
final bool isFirstMonth;
/// Creates view model for the [DayBasedChangeablePicker].
DayBasedChangeablePickerState({
required this.curMonthDis,
required this.prevMonthDis,
required this.nextMonthDis,
required this.currentMonth,
required this.isLastMonth,
required this.isFirstMonth,
this.prevTooltip,
this.nextTooltip,
});
} | 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/month_picker.dart | import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:intl/intl.dart' as intl;
import 'date_picker_keys.dart';
import 'date_picker_styles.dart';
import 'layout_settings.dart';
import 'semantic_sorting.dart';
import 'utils.dart';
const Locale _defaultLocale = Locale('en', 'US');
/// Month picker widget.
class MonthPicker extends StatefulWidget {
/// Month picker widget.
MonthPicker(
{Key? key,
required this.selectedDate,
required this.onChanged,
required this.firstDate,
required this.lastDate,
this.datePickerLayoutSettings = const DatePickerLayoutSettings(),
this.datePickerKeys,
required this.datePickerStyles})
: assert(!firstDate.isAfter(lastDate)),
assert(!selectedDate.isBefore(firstDate)),
assert(!selectedDate.isAfter(lastDate)),
super(key: key);
/// The currently selected date.
///
/// This date is highlighted in the picker.
final DateTime selectedDate;
/// Called when the user picks a month.
final ValueChanged<DateTime> onChanged;
/// The earliest date the user is permitted to pick.
final DateTime firstDate;
/// The latest date the user is permitted to pick.
final DateTime lastDate;
/// Layout settings what can be customized by user
final DatePickerLayoutSettings datePickerLayoutSettings;
/// Some keys useful for integration tests
final DatePickerKeys? datePickerKeys;
/// Styles what can be customized by user
final DatePickerStyles datePickerStyles;
@override
State<StatefulWidget> createState() => _MonthPickerState();
}
class _MonthPickerState extends State<MonthPicker> {
PageController _monthPickerController = PageController();
Locale locale = _defaultLocale;
MaterialLocalizations localizations = _defaultLocalizations;
TextDirection textDirection = TextDirection.ltr;
DateTime _todayDate = DateTime.now();
DateTime _previousYearDate = DateTime(DateTime.now().year - 1);
DateTime _nextYearDate = DateTime(DateTime.now().year + 1);
DateTime _currentDisplayedYearDate = DateTime.now();
Timer? _timer;
/// True if the earliest allowable year is displayed.
bool get _isDisplayingFirstYear =>
!_currentDisplayedYearDate.isAfter(DateTime(widget.firstDate.year));
/// True if the latest allowable year is displayed.
bool get _isDisplayingLastYear =>
!_currentDisplayedYearDate.isBefore(DateTime(widget.lastDate.year));
@override
void initState() {
super.initState();
// Initially display the pre-selected date.
final int yearPage =
DatePickerUtils.yearDelta(widget.firstDate, widget.selectedDate);
_monthPickerController.dispose();
_monthPickerController = PageController(initialPage: yearPage);
_handleYearPageChanged(yearPage);
_updateCurrentDate();
}
@override
void didUpdateWidget(MonthPicker oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.selectedDate != oldWidget.selectedDate) {
final int yearPage =
DatePickerUtils.yearDelta(widget.firstDate, widget.selectedDate);
_monthPickerController = PageController(initialPage: yearPage);
_handleYearPageChanged(yearPage);
}
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
try {
locale = Localizations.localeOf(context);
MaterialLocalizations? curLocalizations =
Localizations.of<MaterialLocalizations>(
context, MaterialLocalizations);
if (curLocalizations != null && localizations != curLocalizations) {
localizations = curLocalizations;
}
textDirection = Directionality.of(context);
// No MaterialLocalizations or Directionality or Locale was found
// and ".of" method throws error
// trying to cast null to MaterialLocalizations.
} on TypeError catch (_) {}
}
void _updateCurrentDate() {
_todayDate = DateTime.now();
final DateTime tomorrow =
DateTime(_todayDate.year, _todayDate.month, _todayDate.day + 1);
Duration timeUntilTomorrow = tomorrow.difference(_todayDate);
timeUntilTomorrow +=
const Duration(seconds: 1); // so we don't miss it by rounding
_timer?.cancel();
_timer = Timer(timeUntilTomorrow, () {
setState(_updateCurrentDate);
});
}
/// Add years to a year truncated date.
DateTime _addYearsToYearDate(DateTime yearDate, int yearsToAdd) =>
DateTime(yearDate.year + yearsToAdd);
Widget _buildItems(BuildContext context, int index) {
final DateTime year = _addYearsToYearDate(widget.firstDate, index);
final ThemeData theme = Theme.of(context);
DatePickerStyles styles = widget.datePickerStyles;
styles = styles.fulfillWithTheme(theme);
return _MonthPicker(
key: ValueKey<DateTime>(year),
selectedDate: widget.selectedDate,
currentDate: _todayDate,
onChanged: widget.onChanged,
firstDate: widget.firstDate,
lastDate: widget.lastDate,
datePickerLayoutSettings: widget.datePickerLayoutSettings,
displayedYear: year,
selectedPeriodKey: widget.datePickerKeys?.selectedPeriodKeys,
datePickerStyles: styles,
locale: locale,
localizations: localizations,
);
}
void _handleNextYear() {
if (!_isDisplayingLastYear) {
String yearStr = localizations.formatYear(_nextYearDate);
SemanticsService.announce(yearStr, textDirection);
_monthPickerController.nextPage(
duration: widget.datePickerLayoutSettings.pagesScrollDuration,
curve: Curves.ease);
}
}
void _handlePreviousYear() {
if (!_isDisplayingFirstYear) {
String yearStr = localizations.formatYear(_previousYearDate);
SemanticsService.announce(yearStr, textDirection);
_monthPickerController.previousPage(
duration: widget.datePickerLayoutSettings.pagesScrollDuration,
curve: Curves.ease);
}
}
void _handleYearPageChanged(int yearPage) {
setState(() {
_previousYearDate = _addYearsToYearDate(widget.firstDate, yearPage - 1);
_currentDisplayedYearDate =
_addYearsToYearDate(widget.firstDate, yearPage);
_nextYearDate = _addYearsToYearDate(widget.firstDate, yearPage + 1);
});
}
@override
Widget build(BuildContext context) {
int yearsCount =
DatePickerUtils.yearDelta(widget.firstDate, widget.lastDate) + 1;
return SizedBox(
width: widget.datePickerLayoutSettings.monthPickerPortraitWidth,
height: widget.datePickerLayoutSettings.maxDayPickerHeight,
child: Stack(
children: <Widget>[
Semantics(
sortKey: YearPickerSortKey.calendar,
child: PageView.builder(
key: ValueKey<DateTime>(widget.selectedDate),
controller: _monthPickerController,
scrollDirection: Axis.horizontal,
itemCount: yearsCount,
itemBuilder: _buildItems,
onPageChanged: _handleYearPageChanged,
),
),
PositionedDirectional(
top: 0.0,
start: 8.0,
child: Semantics(
sortKey: YearPickerSortKey.previousYear,
child: IconButton(
key: widget.datePickerKeys?.previousPageIconKey,
icon: widget.datePickerStyles.prevIcon,
tooltip: _isDisplayingFirstYear
? null
: '${localizations.formatYear(_previousYearDate)}',
onPressed: _isDisplayingFirstYear ? null : _handlePreviousYear,
),
),
),
PositionedDirectional(
top: 0.0,
end: 8.0,
child: Semantics(
sortKey: YearPickerSortKey.nextYear,
child: IconButton(
key: widget.datePickerKeys?.nextPageIconKey,
icon: widget.datePickerStyles.nextIcon,
tooltip: _isDisplayingLastYear
? null
: '${localizations.formatYear(_nextYearDate)}',
onPressed: _isDisplayingLastYear ? null : _handleNextYear,
),
),
),
],
),
);
}
static MaterialLocalizations get _defaultLocalizations =>
MaterialLocalizationEn(
twoDigitZeroPaddedFormat:
intl.NumberFormat('00', _defaultLocale.toString()),
fullYearFormat: intl.DateFormat.y(_defaultLocale.toString()),
longDateFormat: intl.DateFormat.yMMMMEEEEd(_defaultLocale.toString()),
shortMonthDayFormat: intl.DateFormat.MMMd(_defaultLocale.toString()),
decimalFormat:
intl.NumberFormat.decimalPattern(_defaultLocale.toString()),
shortDateFormat: intl.DateFormat.yMMMd(_defaultLocale.toString()),
mediumDateFormat: intl.DateFormat.MMMEd(_defaultLocale.toString()),
compactDateFormat: intl.DateFormat.yMd(_defaultLocale.toString()),
yearMonthFormat: intl.DateFormat.yMMMM(_defaultLocale.toString()),
);
}
class _MonthPicker extends StatelessWidget {
/// The month whose days are displayed by this picker.
final DateTime displayedYear;
/// The earliest date the user is permitted to pick.
final DateTime firstDate;
/// The latest date the user is permitted to pick.
final DateTime lastDate;
/// The currently selected date.
///
/// This date is highlighted in the picker.
final DateTime selectedDate;
/// The current date at the time the picker is displayed.
final DateTime currentDate;
/// Layout settings what can be customized by user
final DatePickerLayoutSettings datePickerLayoutSettings;
/// Called when the user picks a day.
final ValueChanged<DateTime> onChanged;
/// Key fo selected month (useful for integration tests)
final Key? selectedPeriodKey;
/// Styles what can be customized by user
final DatePickerStyles datePickerStyles;
final MaterialLocalizations localizations;
final Locale locale;
_MonthPicker(
{required this.displayedYear,
required this.firstDate,
required this.lastDate,
required this.selectedDate,
required this.currentDate,
required this.onChanged,
required this.datePickerLayoutSettings,
required this.datePickerStyles,
required this.localizations,
required this.locale,
this.selectedPeriodKey,
Key? key})
: assert(!firstDate.isAfter(lastDate)),
assert(selectedDate.isAfter(firstDate) ||
selectedDate.isAtSameMomentAs(firstDate)),
super(key: key);
// We only need to know if month of passed day
// before the month of the firstDate or after the month of the lastDate.
//
// Don't need to compare day and time.
bool _isDisabled(DateTime month) {
DateTime beginningOfTheFirstDateMonth =
DateTime(firstDate.year, firstDate.month);
DateTime endOfTheLastDateMonth = DateTime(lastDate.year, lastDate.month + 1)
.subtract(Duration(microseconds: 1));
return month.isAfter(endOfTheLastDateMonth) ||
month.isBefore(beginningOfTheFirstDateMonth);
}
@override
Widget build(BuildContext context) {
final ThemeData themeData = Theme.of(context);
final int monthsInYear = 12;
final int year = displayedYear.year;
final int day = 1;
final List<Widget> labels = <Widget>[];
for (int i = 0; i < monthsInYear; i += 1) {
final int month = i + 1;
final DateTime monthToBuild = DateTime(year, month, day);
final bool disabled = _isDisabled(monthToBuild);
final bool isSelectedMonth =
selectedDate.year == year && selectedDate.month == month;
BoxDecoration? decoration;
TextStyle? itemStyle = themeData.textTheme.bodyText2;
if (isSelectedMonth) {
itemStyle = datePickerStyles.selectedDateStyle;
decoration = datePickerStyles.selectedSingleDateDecoration;
} else if (disabled) {
itemStyle = datePickerStyles.disabledDateStyle;
} else if (currentDate.year == year && currentDate.month == month) {
// The current month gets a different text color.
itemStyle = datePickerStyles.currentDateStyle;
} else {
itemStyle = datePickerStyles.defaultDateTextStyle;
}
String monthStr = _getMonthStr(monthToBuild);
Widget monthWidget = Container(
decoration: decoration,
child: Center(
child: Semantics(
// We want the day of month to be spoken first irrespective of the
// locale-specific preferences or TextDirection. This is because
// an accessibility user is more likely to be interested in the
// day of month before the rest of the date, as they are looking
// for the day of month. To do that we prepend day of month to the
// formatted full date.
label: '${localizations.formatDecimal(month)}, '
'${localizations.formatFullDate(monthToBuild)}',
selected: isSelectedMonth,
child: ExcludeSemantics(
child: Text(monthStr, style: itemStyle),
),
),
),
);
if (!disabled) {
monthWidget = GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
DatePickerUtils.sameMonth(firstDate, monthToBuild)
? onChanged(firstDate)
: onChanged(monthToBuild);
},
child: monthWidget,
);
}
labels.add(monthWidget);
}
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Column(
children: <Widget>[
Container(
height: datePickerLayoutSettings.dayPickerRowHeight,
child: Center(
child: ExcludeSemantics(
child: Text(
localizations.formatYear(displayedYear),
key: selectedPeriodKey,
style: datePickerStyles.displayedPeriodTitle,
),
),
),
),
Flexible(
child: GridView.count(
physics: datePickerLayoutSettings.scrollPhysics,
crossAxisCount: 4,
children: labels,
),
),
],
),
);
}
// Returns only month made with intl.DateFormat.MMM() for current [locale].
// We can'r use [localizations] here because MaterialLocalizations doesn't
// provide short month string.
String _getMonthStr(DateTime date) {
String month = intl.DateFormat.MMM(locale.toString()).format(date);
return month;
}
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/day_picker_selection.dart | import 'date_period.dart';
import 'utils.dart';
/// Base class for day based pickers selection.
abstract class DayPickerSelection {
/// If this is before [dateTime].
bool isBefore(DateTime dateTime);
/// If this is after [dateTime].
bool isAfter(DateTime dateTime);
/// Returns earliest [DateTime] in this selection.
DateTime get earliest;
/// If this selection is empty.
bool get isEmpty;
/// If this selection is not empty.
bool get isNotEmpty;
/// Constructor to allow children to have constant constructor.
const DayPickerSelection();
}
/// Selection with only one selected date.
///
/// See also:
/// * [DayPickerMultiSelection] - selection with one or many single dates.
/// * [DayPickerRangeSelection] - date period selection.
class DayPickerSingleSelection extends DayPickerSelection {
/// Selected date.
final DateTime selectedDate;
/// Creates selection with only one selected date.
const DayPickerSingleSelection(this.selectedDate)
: assert(selectedDate != null);
@override
bool isAfter(DateTime dateTime) => selectedDate.isAfter(dateTime);
@override
bool isBefore(DateTime dateTime) => selectedDate.isAfter(dateTime);
@override
DateTime get earliest => selectedDate;
@override
bool get isEmpty => selectedDate == null;
@override
bool get isNotEmpty => selectedDate != null;
}
/// Selection with one or many single dates.
///
/// See also:
/// * [DayPickerSingleSelection] - selection with only one selected date.
/// * [DayPickerRangeSelection] - date period selection.
class DayPickerMultiSelection extends DayPickerSelection {
/// List of the selected dates.
final List<DateTime> selectedDates;
/// Selection with one or many single dates.
DayPickerMultiSelection(this.selectedDates)
: assert(selectedDates != null);
@override
bool isAfter(DateTime dateTime)
=> selectedDates.every((d) => d.isAfter(dateTime));
@override
bool isBefore(DateTime dateTime)
=> selectedDates.every((d) => d.isBefore(dateTime));
@override
DateTime get earliest => DatePickerUtils.getEarliestFromList(selectedDates);
@override
bool get isEmpty => selectedDates.isEmpty;
@override
bool get isNotEmpty => selectedDates.isNotEmpty;
}
/// Date period selection.
///
/// See also:
/// * [DayPickerSingleSelection] - selection with only one selected date.
/// * [DayPickerMultiSelection] - selection with one or many single dates.
class DayPickerRangeSelection extends DayPickerSelection {
/// Selected period.
final DatePeriod selectedRange;
/// Date period selection.
const DayPickerRangeSelection(this.selectedRange)
: assert(selectedRange != null);
@override
DateTime get earliest => selectedRange.start;
@override
bool isAfter(DateTime dateTime) => selectedRange.start.isAfter(dateTime);
@override
bool isBefore(DateTime dateTime) => selectedRange.end.isBefore(dateTime);
@override
bool get isEmpty => selectedRange == null;
@override
bool get isNotEmpty => selectedRange != null;
} | 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/typedefs.dart | import 'range_picker.dart';
import 'unselectable_period_error.dart';
import 'week_picker.dart';
/// Signature for function that can be used to handle incorrect selections.
///
/// See also:
/// * [WeekPicker.onSelectionError]
/// * [RangePicker.onSelectionError]
typedef OnSelectionError = void Function(UnselectablePeriodException e);
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/day_type.dart | /// Type of the day in day based date picker.
enum DayType {
/// start of the selected period
start,
/// middle of the selected period
middle,
/// end of the selected period
end,
/// selected single day
single,
/// disabled day
disabled,
/// not selected day (but not disabled)
notSelected
} | 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/utils.dart | /// Bunch of useful functions for date pickers.
class DatePickerUtils {
/// Returns if two objects have same year, month and day.
/// Time doesn't matter.
static bool sameDate(DateTime dateTimeOne, DateTime dateTimeTwo) =>
dateTimeOne.year == dateTimeTwo.year &&
dateTimeOne.month == dateTimeTwo.month &&
dateTimeOne.day == dateTimeTwo.day;
/// Returns if two objects have same year and month.
/// Day and time don't matter/
static bool sameMonth(DateTime dateTimeOne, DateTime dateTimeTwo) =>
dateTimeOne.year == dateTimeTwo.year
&& dateTimeOne.month == dateTimeTwo.month;
// Do not use this directly - call getDaysInMonth instead.
static const List<int> _daysInMonth = <int>[
31,
-1,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31
];
/// Returns the number of days in a month, according to the proleptic
/// Gregorian calendar.
///
/// This applies the leap year logic introduced by the Gregorian reforms of
/// 1582. It will not give valid results for dates prior to that time.
static int getDaysInMonth(int year, int month) {
if (month == DateTime.february) {
final bool isLeapYear =
(year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
return isLeapYear ? 29 : 28;
}
return _daysInMonth[month - 1];
}
/// Returns number of months between [startDate] and [endDate]
static int monthDelta(DateTime startDate, DateTime endDate) =>
(endDate.year - startDate.year) * 12 +
endDate.month -
startDate.month;
/// Add months to a month truncated date.
static DateTime addMonthsToMonthDate(DateTime monthDate, int monthsToAdd) =>
// year is switched automatically if new month > 12
DateTime(monthDate.year, monthDate.month + monthsToAdd);
/// Returns number of years between [startDate] and [endDate]
static int yearDelta(DateTime startDate, DateTime endDate) =>
endDate.year - startDate.year;
/// Returns start of the first day of the week with given day.
///
/// Start of the week calculated using firstDayIndex which is int from 0 to 6
/// where 0 points to Sunday and 6 points to Saturday.
/// (according to MaterialLocalization.firstDayIfWeekIndex)
static DateTime getFirstDayOfWeek(DateTime day, int firstDayIndex) {
// from 1 to 7 where 1 points to Monday and 7 points to Sunday
int weekday = day.weekday;
// to match weekdays where Sunday is 7 not 0
if (firstDayIndex == 0) firstDayIndex = 7;
int diff = weekday - firstDayIndex;
if (diff < 0) diff = 7 + diff;
DateTime firstDayOfWeek = day.subtract(Duration(days: diff));
firstDayOfWeek = startOfTheDay(firstDayOfWeek);
return firstDayOfWeek;
}
/// Returns end of the last day of the week with given day.
///
/// Start of the week calculated using firstDayIndex which is int from 0 to 6
/// where 0 points to Sunday and 6 points to Saturday.
/// (according to MaterialLocalization.firstDayIfWeekIndex)
static DateTime getLastDayOfWeek(DateTime day, int firstDayIndex) {
// from 1 to 7 where 1 points to Monday and 7 points to Sunday
int weekday = day.weekday;
// to match weekdays where Sunday is 7 not 0
if (firstDayIndex == 0) firstDayIndex = 7;
int lastDayIndex = firstDayIndex - 1;
if (lastDayIndex == 0) lastDayIndex = 7;
int diff = lastDayIndex - weekday;
if (diff < 0) diff = 7 + diff;
DateTime lastDayOfWeek = day.add(Duration(days: diff));
lastDayOfWeek = endOfTheDay(lastDayOfWeek);
return lastDayOfWeek;
}
/// Returns end of the given day.
///
/// End time is 1 millisecond before start of the next day.
static DateTime endOfTheDay(DateTime date) {
DateTime tomorrowStart = DateTime(date.year, date.month, date.day + 1);
DateTime result = tomorrowStart.subtract(const Duration(milliseconds: 1));
return result;
}
/// Returns start of the given day.
///
/// Start time is 00:00:00.
static DateTime startOfTheDay(DateTime date) =>
DateTime(date.year, date.month, date.day);
/// Returns first shown date for the [curMonth].
///
/// First shown date is not always 1st day of the [curMonth].
/// It can be day from previous month if [showEndOfPrevMonth] is true.
///
/// If [showEndOfPrevMonth] is true empty day cells before 1st [curMonth]
/// are filled with days from the previous month.
static DateTime firstShownDate({
required DateTime curMonth,
required bool showEndOfPrevMonth,
required int firstDayOfWeekFromSunday}) {
DateTime result = DateTime(curMonth.year, curMonth.month, 1);
if (showEndOfPrevMonth) {
int firstDayOffset = computeFirstDayOffset(curMonth.year, curMonth.month,
firstDayOfWeekFromSunday);
if (firstDayOffset == 0) return result;
int prevMonth = curMonth.month - 1;
if (prevMonth < 1) prevMonth = 12;
int prevYear = prevMonth == 12
? curMonth.year - 1
: curMonth.year;
int daysInPrevMonth = getDaysInMonth(prevYear, prevMonth);
int firstShownDay = daysInPrevMonth - firstDayOffset + 1;
result = DateTime(prevYear, prevMonth, firstShownDay);
}
return result;
}
/// Returns last shown date for the [curMonth].
///
/// Last shown date is not always last day of the [curMonth].
/// It can be day from next month if [showStartNextMonth] is true.
///
/// If [showStartNextMonth] is true empty day cells after last day
/// of [curMonth] are filled with days from the next month.
static DateTime lastShownDate({
required DateTime curMonth,
required bool showStartNextMonth,
required int firstDayOfWeekFromSunday}) {
int daysInCurMonth = getDaysInMonth(curMonth.year, curMonth.month);
DateTime result = DateTime(curMonth.year, curMonth.month, daysInCurMonth);
if (showStartNextMonth) {
int firstDayOffset = computeFirstDayOffset(curMonth.year, curMonth.month,
firstDayOfWeekFromSunday);
int totalDays = firstDayOffset + daysInCurMonth;
int trailingDaysCount = 7 - totalDays % 7;
bool fullWeekTrailing = trailingDaysCount == 7;
if (fullWeekTrailing) return result;
result = DateTime(curMonth.year, curMonth.month + 1, trailingDaysCount);
}
return result;
}
/// Computes the offset from the first day of week that the first day of the
/// [month] falls on.
///
/// For example, September 1, 2017 falls on a Friday, which in the calendar
/// localized for United States English appears as:
///
/// ```
/// S M T W T F S
/// _ _ _ _ _ 1 2
/// ```
///
/// The offset for the first day of the months is the number of leading blanks
/// in the calendar, i.e. 5.
///
/// The same date localized for the Russian calendar has a different offset,
/// because the first day of week is Monday rather than Sunday:
///
/// ```
/// M T W T F S S
/// _ _ _ _ 1 2 3
/// ```
///
/// So the offset is 4, rather than 5.
///
/// This code consolidates the following:
///
/// - [DateTime.weekday] provides a 1-based index into days of week, with 1
/// falling on Monday.
/// - MaterialLocalizations.firstDayOfWeekIndex provides a 0-based index
/// into the MaterialLocalizations.narrowWeekdays list.
/// - MaterialLocalizations.narrowWeekdays list provides localized names of
/// days of week, always starting with Sunday and ending with Saturday.
static int computeFirstDayOffset(
int year, int month, int firstDayOfWeekFromSunday) {
// 0-based day of week, with 0 representing Monday.
final int weekdayFromMonday = DateTime(year, month).weekday - 1;
// firstDayOfWeekFromSunday recomputed to be Monday-based
final int firstDayOfWeekFromMonday = (firstDayOfWeekFromSunday - 1) % 7;
// Number of days between the first day of week appearing on the calendar,
// and the day corresponding to the 1-st of the month.
return (weekdayFromMonday - firstDayOfWeekFromMonday) % 7;
}
/// Returns earliest [DateTime] from the list.
///
/// [dates] must not be null.
/// In case it is null, [ArgumentError] will be thrown.
static DateTime getEarliestFromList(List<DateTime> dates) {
ArgumentError.checkNotNull(dates, "dates");
return dates.fold(dates[0], getEarliest);
}
/// Returns earliest [DateTime] from two.
///
/// If two [DateTime]s is the same moment first ([a]) will be return.
static DateTime getEarliest(DateTime a, DateTime b)
=> a.isBefore(b) ? a : b;
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/date_picker_styles.dart | import 'package:flutter/material.dart';
import 'range_picker.dart';
import 'week_picker.dart';
/// 0 points to Sunday, and 6 points to Saturday.
typedef DayHeaderStyleBuilder = DayHeaderStyle? Function(int dayOfTheWeek);
/// Common styles for date pickers.
///
/// To define more styles for date pickers which allow select some range
/// (e.g. [RangePicker], [WeekPicker]) use [DatePickerRangeStyles].
@immutable
class DatePickerStyles {
/// Styles for title of displayed period
/// (e.g. month for day picker and year for month picker).
final TextStyle? displayedPeriodTitle;
/// Style for the number of current date.
final TextStyle? currentDateStyle;
/// Style for the numbers of disabled dates.
final TextStyle? disabledDateStyle;
/// Style for the number of selected date.
final TextStyle? selectedDateStyle;
/// Used for date which is neither current nor disabled nor selected.
final TextStyle? defaultDateTextStyle;
/// Day cell decoration for selected date in case only one date is selected.
final BoxDecoration? selectedSingleDateDecoration;
/// Style for the day header.
///
/// If you need to customize day header's style depends on day of the week
/// use [dayHeaderStyleBuilder] instead.
final DayHeaderStyle? dayHeaderStyle;
/// Builder to customize styles for day headers depends on day of the week.
/// Where 0 points to Sunday and 6 points to Saturday.
///
/// Builder must return not null value for every weekday from 0 to 6.
///
/// If styles should be the same for any day of the week
/// use [dayHeaderStyle] instead.
final DayHeaderStyleBuilder? dayHeaderStyleBuilder;
/// Widget which will be shown left side of the shown page title.
/// User goes to previous data period by click on it.
final Widget prevIcon;
/// Widget which will be shown right side of the shown page title.
/// User goes to next data period by click on it.
final Widget nextIcon;
/// Index of the first day of week, where 0 points to Sunday, and 6 points to
/// Saturday. Must not be less 0 or more then 6.
///
/// Can be null. In this case value from current locale will be used.
final int? firstDayOfeWeekIndex;
/// Styles for date picker.
DatePickerStyles({
this.displayedPeriodTitle,
this.currentDateStyle,
this.disabledDateStyle,
this.selectedDateStyle,
this.selectedSingleDateDecoration,
this.defaultDateTextStyle,
this.dayHeaderStyleBuilder,
this.dayHeaderStyle,
this.firstDayOfeWeekIndex,
this.prevIcon = const Icon(Icons.chevron_left),
this.nextIcon = const Icon(Icons.chevron_right)
}) : assert(!(dayHeaderStyle != null && dayHeaderStyleBuilder != null),
"Should be only one from: dayHeaderStyleBuilder, dayHeaderStyle."),
assert(dayHeaderStyleBuilder == null
|| _validateDayHeaderStyleBuilder(dayHeaderStyleBuilder),
"dayHeaderStyleBuilder must return not null value from every weekday "
"(from 0 to 6)."),
assert(_validateFirstDayOfWeek(firstDayOfeWeekIndex),
"firstDayOfeWeekIndex must be null or in correct range (from 0 to 6).");
/// Return new [DatePickerStyles] object where fields
/// with null values set with defaults from theme.
DatePickerStyles fulfillWithTheme(ThemeData theme) {
Color accentColor = theme.accentColor;
TextStyle? _displayedPeriodTitle =
displayedPeriodTitle ?? theme.textTheme.subtitle1;
TextStyle? _currentDateStyle = currentDateStyle ??
theme.textTheme.bodyText1?.copyWith(color: theme.accentColor);
TextStyle? _disabledDateStyle = disabledDateStyle ??
theme.textTheme.bodyText2?.copyWith(color: theme.disabledColor);
TextStyle? _selectedDateStyle =
selectedDateStyle ?? theme.accentTextTheme.bodyText1;
TextStyle? _defaultDateTextStyle =
defaultDateTextStyle ?? theme.textTheme.bodyText2;
BoxDecoration _selectedSingleDateDecoration =
selectedSingleDateDecoration ??
BoxDecoration(
color: accentColor,
borderRadius: const BorderRadius.all(Radius.circular(10.0)));
DayHeaderStyle? _dayHeaderStyle = dayHeaderStyle;
if (dayHeaderStyleBuilder == null && _dayHeaderStyle == null) {
_dayHeaderStyle = DayHeaderStyle(textStyle: theme.textTheme.caption);
}
return DatePickerStyles(
disabledDateStyle: _disabledDateStyle,
currentDateStyle: _currentDateStyle,
displayedPeriodTitle: _displayedPeriodTitle,
selectedDateStyle: _selectedDateStyle,
selectedSingleDateDecoration: _selectedSingleDateDecoration,
defaultDateTextStyle: _defaultDateTextStyle,
dayHeaderStyle: _dayHeaderStyle,
dayHeaderStyleBuilder: dayHeaderStyleBuilder,
nextIcon: nextIcon,
prevIcon: prevIcon
);
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other.runtimeType != runtimeType) return false;
return other is DatePickerStyles
&& other.displayedPeriodTitle == displayedPeriodTitle
&& other.currentDateStyle == currentDateStyle
&& other.disabledDateStyle == disabledDateStyle
&& other.selectedDateStyle == selectedDateStyle
&& other.defaultDateTextStyle == defaultDateTextStyle
&& other.selectedSingleDateDecoration == selectedSingleDateDecoration
&& other.dayHeaderStyle == dayHeaderStyle
&& other.dayHeaderStyleBuilder == dayHeaderStyleBuilder
&& other.prevIcon == prevIcon
&& other.nextIcon == nextIcon
&& other.firstDayOfeWeekIndex == firstDayOfeWeekIndex;
}
@override
int get hashCode =>
hashValues(
displayedPeriodTitle,
currentDateStyle,
disabledDateStyle,
selectedDateStyle,
defaultDateTextStyle,
selectedSingleDateDecoration,
dayHeaderStyle,
dayHeaderStyleBuilder,
prevIcon,
nextIcon,
firstDayOfeWeekIndex
);
static bool _validateDayHeaderStyleBuilder(DayHeaderStyleBuilder builder) {
List<int> weekdays = const [0, 1, 2, 3, 4, 5, 6];
// ignore: avoid_types_on_closure_parameters
bool valid = weekdays.every((int weekday) => builder(weekday) != null);
return valid;
}
static bool _validateFirstDayOfWeek(int? index) {
if (index == null) return true;
bool valid = index >= 0 && index <= 6;
return valid;
}
}
/// Styles for date pickers which allow select some range
/// (e.g. RangePicker, WeekPicker).
@immutable
class DatePickerRangeStyles extends DatePickerStyles {
/// Decoration for the first date of the selected range.
final BoxDecoration? selectedPeriodStartDecoration;
/// Text style for the first date of the selected range.
///
/// If null - default [DatePickerStyles.selectedDateStyle] will be used.
final TextStyle? selectedPeriodStartTextStyle;
/// Decoration for the last date of the selected range.
final BoxDecoration? selectedPeriodLastDecoration;
/// Text style for the last date of the selected range.
///
/// If null - default [DatePickerStyles.selectedDateStyle] will be used.
final TextStyle? selectedPeriodEndTextStyle;
/// Decoration for the date of the selected range
/// which is not first date and not end date of this range.
///
/// If there is only one date selected
/// [DatePickerStyles.selectedSingleDateDecoration] will be used.
final BoxDecoration? selectedPeriodMiddleDecoration;
/// Text style for the middle date of the selected range.
///
/// If null - default [DatePickerStyles.selectedDateStyle] will be used.
final TextStyle? selectedPeriodMiddleTextStyle;
/// Return new [DatePickerRangeStyles] object
/// where fields with null values set with defaults from given theme.
@override
DatePickerRangeStyles fulfillWithTheme(ThemeData theme) {
Color accentColor = theme.accentColor;
DatePickerStyles commonStyles = super.fulfillWithTheme(theme);
final BoxDecoration _selectedPeriodStartDecoration =
selectedPeriodStartDecoration ??
BoxDecoration(
color: accentColor,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(10.0),
bottomLeft: Radius.circular(10.0)),
);
final BoxDecoration _selectedPeriodLastDecoration =
selectedPeriodLastDecoration ??
BoxDecoration(
color: accentColor,
borderRadius: const BorderRadius.only(
topRight: Radius.circular(10.0),
bottomRight: Radius.circular(10.0)),
);
final BoxDecoration _selectedPeriodMiddleDecoration =
selectedPeriodMiddleDecoration ??
BoxDecoration(
color: accentColor,
shape: BoxShape.rectangle,
);
final TextStyle? _selectedPeriodStartTextStyle =
selectedPeriodStartTextStyle ?? commonStyles.selectedDateStyle;
final TextStyle? _selectedPeriodMiddleTextStyle =
selectedPeriodMiddleTextStyle ?? commonStyles.selectedDateStyle;
final TextStyle? _selectedPeriodEndTextStyle =
selectedPeriodEndTextStyle ?? commonStyles.selectedDateStyle;
return DatePickerRangeStyles(
disabledDateStyle: commonStyles.disabledDateStyle,
currentDateStyle: commonStyles.currentDateStyle,
displayedPeriodTitle: commonStyles.displayedPeriodTitle,
selectedDateStyle: commonStyles.selectedDateStyle,
selectedSingleDateDecoration: commonStyles.selectedSingleDateDecoration,
defaultDateTextStyle: commonStyles.defaultDateTextStyle,
dayHeaderStyle: commonStyles.dayHeaderStyle,
dayHeaderStyleBuilder: commonStyles.dayHeaderStyleBuilder,
firstDayOfWeekIndex: firstDayOfeWeekIndex,
selectedPeriodStartDecoration: _selectedPeriodStartDecoration,
selectedPeriodMiddleDecoration: _selectedPeriodMiddleDecoration,
selectedPeriodLastDecoration: _selectedPeriodLastDecoration,
selectedPeriodStartTextStyle: _selectedPeriodStartTextStyle,
selectedPeriodMiddleTextStyle: _selectedPeriodMiddleTextStyle,
selectedPeriodEndTextStyle: _selectedPeriodEndTextStyle,
);
}
/// Styles for the pickers that allows to select range ([RangePicker],
/// [WeekPicker]).
DatePickerRangeStyles({
displayedPeriodTitle,
currentDateStyle,
disabledDateStyle,
selectedDateStyle,
selectedSingleDateDecoration,
defaultDateTextStyle,
dayHeaderStyle,
dayHeaderStyleBuilder,
Widget nextIcon = const Icon(Icons.chevron_right),
Widget prevIcon = const Icon(Icons.chevron_left),
firstDayOfWeekIndex,
this.selectedPeriodLastDecoration,
this.selectedPeriodMiddleDecoration,
this.selectedPeriodStartDecoration,
this.selectedPeriodStartTextStyle,
this.selectedPeriodMiddleTextStyle,
this.selectedPeriodEndTextStyle,
}) : super(
displayedPeriodTitle: displayedPeriodTitle,
currentDateStyle: currentDateStyle,
disabledDateStyle: disabledDateStyle,
selectedDateStyle: selectedDateStyle,
selectedSingleDateDecoration: selectedSingleDateDecoration,
defaultDateTextStyle: defaultDateTextStyle,
dayHeaderStyle: dayHeaderStyle,
dayHeaderStyleBuilder: dayHeaderStyleBuilder,
nextIcon: nextIcon,
prevIcon: prevIcon,
firstDayOfeWeekIndex: firstDayOfWeekIndex
);
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other.runtimeType != runtimeType) return false;
return other is DatePickerRangeStyles
&& other.selectedPeriodStartDecoration == selectedPeriodStartDecoration
&& other.selectedPeriodStartTextStyle == selectedPeriodStartTextStyle
&& other.selectedPeriodLastDecoration == selectedPeriodLastDecoration
&& other.selectedPeriodEndTextStyle == selectedPeriodEndTextStyle
&& other.selectedPeriodMiddleDecoration ==selectedPeriodMiddleDecoration
&& other.selectedPeriodMiddleTextStyle == selectedPeriodMiddleTextStyle
&& other.displayedPeriodTitle == displayedPeriodTitle
&& other.currentDateStyle == currentDateStyle
&& other.disabledDateStyle == disabledDateStyle
&& other.selectedDateStyle == selectedDateStyle
&& other.defaultDateTextStyle == defaultDateTextStyle
&& other.selectedSingleDateDecoration == selectedSingleDateDecoration
&& other.dayHeaderStyle == dayHeaderStyle
&& other.dayHeaderStyleBuilder == dayHeaderStyleBuilder
&& other.prevIcon == prevIcon
&& other.nextIcon == nextIcon
&& other.firstDayOfeWeekIndex == firstDayOfeWeekIndex;
}
@override
int get hashCode =>
hashValues(
selectedPeriodStartDecoration,
selectedPeriodStartTextStyle,
selectedPeriodLastDecoration,
selectedPeriodEndTextStyle,
selectedPeriodMiddleDecoration,
selectedPeriodMiddleTextStyle,
displayedPeriodTitle,
currentDateStyle,
disabledDateStyle,
selectedDateStyle,
defaultDateTextStyle,
selectedSingleDateDecoration,
dayHeaderStyle,
dayHeaderStyleBuilder,
prevIcon,
nextIcon,
firstDayOfeWeekIndex
);
}
/// User styles for the day header in date picker.
@immutable
class DayHeaderStyle {
/// If null - textTheme.caption from the Theme will be used.
final TextStyle? textStyle;
/// If null - no decoration will be applied for the day header;
final BoxDecoration? decoration;
/// Creates styles for the day headers in date pickers.
///
/// See also:
/// * [DatePickerStyles.dayHeaderStyleBuilder]
const DayHeaderStyle({
this.textStyle,
this.decoration
});
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other.runtimeType != runtimeType) return false;
return other is DayHeaderStyle
&& other.textStyle == textStyle
&& other.decoration == decoration;
}
@override
int get hashCode => hashValues(
textStyle,
decoration
);
} | 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/day_picker.dart | import 'package:flutter/material.dart';
import 'date_picker_keys.dart';
import 'date_picker_styles.dart';
import 'day_based_changable_picker.dart';
import 'day_picker_selection.dart';
import 'day_type.dart';
import 'event_decoration.dart';
import 'i_selectable_picker.dart';
import 'layout_settings.dart';
/// Date picker for selection one day.
class DayPicker<T extends Object> extends StatelessWidget {
DayPicker._({Key? key,
required this.onChanged,
required this.firstDate,
required this.lastDate,
required this.selectionLogic,
required this.selection,
this.initiallyShowDate,
this.datePickerLayoutSettings = const DatePickerLayoutSettings(),
this.datePickerStyles,
this.datePickerKeys,
this.selectableDayPredicate,
this.eventDecorationBuilder,
this.onMonthChanged}) : super(key: key);
/// Creates a day picker where only one single day can be selected.
///
/// See also:
/// * [DayPicker.multi] - day picker where many single days can be selected.
static DayPicker<DateTime> single({
Key? key,
required DateTime selectedDate,
required ValueChanged<DateTime> onChanged,
required DateTime firstDate,
required DateTime lastDate,
DatePickerLayoutSettings datePickerLayoutSettings
= const DatePickerLayoutSettings(),
DateTime? initiallyShowDate,
DatePickerRangeStyles? datePickerStyles,
DatePickerKeys? datePickerKeys,
SelectableDayPredicate? selectableDayPredicate,
EventDecorationBuilder? eventDecorationBuilder,
ValueChanged<DateTime>? onMonthChanged
})
{
assert(!firstDate.isAfter(lastDate));
assert(!lastDate.isBefore(firstDate));
assert(!selectedDate.isBefore(firstDate));
assert(!selectedDate.isAfter(lastDate));
assert(initiallyShowDate == null
|| !initiallyShowDate.isAfter(lastDate));
assert(initiallyShowDate == null
|| !initiallyShowDate.isBefore(firstDate));
final selection = DayPickerSingleSelection(selectedDate);
final selectionLogic = DaySelectable(
selectedDate, firstDate, lastDate,
selectableDayPredicate: selectableDayPredicate);
return DayPicker<DateTime>._(
onChanged: onChanged,
firstDate: firstDate,
lastDate: lastDate,
initiallyShowDate: initiallyShowDate,
selectionLogic: selectionLogic,
selection: selection,
eventDecorationBuilder: eventDecorationBuilder,
onMonthChanged: onMonthChanged,
selectableDayPredicate: selectableDayPredicate,
datePickerKeys: datePickerKeys,
datePickerStyles: datePickerStyles,
datePickerLayoutSettings: datePickerLayoutSettings,
);
}
/// Creates a day picker where many single days can be selected.
///
/// See also:
/// * [DayPicker.single] - day picker where only one single day
/// can be selected.
static DayPicker<List<DateTime>> multi({Key? key,
required List<DateTime> selectedDates,
required ValueChanged<List<DateTime>> onChanged,
required DateTime firstDate,
required DateTime lastDate,
DatePickerLayoutSettings datePickerLayoutSettings
= const DatePickerLayoutSettings(),
DateTime? initiallyShowDate,
DatePickerRangeStyles? datePickerStyles,
DatePickerKeys? datePickerKeys,
SelectableDayPredicate? selectableDayPredicate,
EventDecorationBuilder? eventDecorationBuilder,
ValueChanged<DateTime>? onMonthChanged})
{
assert(!firstDate.isAfter(lastDate));
assert(!lastDate.isBefore(firstDate));
assert(initiallyShowDate == null
|| !initiallyShowDate.isAfter(lastDate));
assert(initiallyShowDate == null
|| !initiallyShowDate.isBefore(lastDate));
final selection = DayPickerMultiSelection(selectedDates);
final selectionLogic = DayMultiSelectable(
selectedDates, firstDate, lastDate,
selectableDayPredicate: selectableDayPredicate);
return DayPicker<List<DateTime>>._(
onChanged: onChanged,
firstDate: firstDate,
lastDate: lastDate,
initiallyShowDate: initiallyShowDate,
selectionLogic: selectionLogic,
selection: selection,
eventDecorationBuilder: eventDecorationBuilder,
onMonthChanged: onMonthChanged,
selectableDayPredicate: selectableDayPredicate,
datePickerKeys: datePickerKeys,
datePickerStyles: datePickerStyles,
datePickerLayoutSettings: datePickerLayoutSettings,
);
}
/// The currently selected date.
///
/// This date is highlighted in the picker.
final DayPickerSelection selection;
/// Called when the user picks a day.
final ValueChanged<T> onChanged;
/// The earliest date the user is permitted to pick.
final DateTime firstDate;
/// The latest date the user is permitted to pick.
final DateTime lastDate;
/// Date for defining what month should be shown initially.
///
/// In case of null earliest of the [selection] will be shown.
final DateTime? initiallyShowDate;
/// Layout settings what can be customized by user
final DatePickerLayoutSettings datePickerLayoutSettings;
/// Styles what can be customized by user
final DatePickerRangeStyles? datePickerStyles;
/// Some keys useful for integration tests
final DatePickerKeys? datePickerKeys;
/// Function returns if day can be selected or not.
///
/// If null
final SelectableDayPredicate? selectableDayPredicate;
/// Builder to get event decoration for each date.
///
/// All event styles are overriden by selected styles
/// except days with dayType is [DayType.notSelected].
final EventDecorationBuilder? eventDecorationBuilder;
// Called when the user changes the month.
/// New DateTime object represents first day of new month and 00:00 time.
final ValueChanged<DateTime>? onMonthChanged;
/// Logic to handle user's selections.
final ISelectablePicker<T> selectionLogic;
@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
return DayBasedChangeablePicker<T>(
selectablePicker: selectionLogic,
selection: selection,
firstDate: firstDate,
lastDate: lastDate,
initiallyShownDate: initiallyShowDate,
onChanged: onChanged,
datePickerLayoutSettings: datePickerLayoutSettings,
datePickerStyles: datePickerStyles ?? DatePickerRangeStyles(),
datePickerKeys: datePickerKeys,
eventDecorationBuilder: eventDecorationBuilder,
onMonthChanged: onMonthChanged,
);
}
} | 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/semantic_sorting.dart | import 'package:flutter/semantics.dart';
/// Defines semantic traversal order of the top-level widgets
/// inside the day or week picker.
class MonthPickerSortKey extends OrdinalSortKey {
/// Previous month key.
static const MonthPickerSortKey previousMonth = MonthPickerSortKey(1.0);
/// Next month key.
static const MonthPickerSortKey nextMonth = MonthPickerSortKey(2.0);
/// Calendar key.
static const MonthPickerSortKey calendar = MonthPickerSortKey(3.0);
///
const MonthPickerSortKey(double order) : super(order);
}
/// Defines semantic traversal order of the top-level widgets
/// inside the month picker.
class YearPickerSortKey extends OrdinalSortKey {
/// Previous year key.
static const YearPickerSortKey previousYear = YearPickerSortKey(1.0);
/// Next year key.
static const YearPickerSortKey nextYear = YearPickerSortKey(2.0);
/// Calendar key.
static const YearPickerSortKey calendar = YearPickerSortKey(3.0);
///
const YearPickerSortKey(double order) : super(order);
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/lib/src/event_decoration.dart | import 'package:flutter/widgets.dart';
import 'day_picker.dart';
import 'range_picker.dart';
import 'week_picker.dart';
/// Signature for function which is used to set set specific decoration for
/// some days in [DayPicker], [WeekPicker] and [RangePicker].
///
/// See also:
/// * [DayPicker.eventDecorationBuilder]
/// * [WeekPicker.eventDecorationBuilder]
/// * [RangePicker.eventDecorationBuilder]
typedef EventDecorationBuilder = EventDecoration? Function(DateTime date);
/// Class to store styles for event (specific day in the date picker).
@immutable
class EventDecoration {
/// Cell decoration for the specific day in the date picker (event).
final BoxDecoration? boxDecoration;
/// Style for number of the specific day in the date picker (event).
final TextStyle? textStyle;
/// Creates decoration for special day.
///
/// Used for [EventDecorationBuilder] function which is usually passed to
/// [DayPicker.eventDecorationBuilder], [WeekPicker.eventDecorationBuilder]
/// and [RangePicker.eventDecorationBuilder] to set specific decoration for
/// some days.
const EventDecoration({this.boxDecoration, this.textStyle});
} | 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master | mirrored_repositories/flutter-examples/flutter_date_pickers-master/test/date_time_utils.dart | /// Bunch of useful functions for date pickers.
class DateTimeUtils {
/// Returns if two objects have same year, month and day.
/// Time doesn't matter.
static bool sameDate(DateTime dateTimeOne, DateTime dateTimeTwo) =>
dateTimeOne.year == dateTimeTwo.year &&
dateTimeOne.month == dateTimeTwo.month &&
dateTimeOne.day == dateTimeTwo.day;
/// Returns if two objects have same year and month.
/// Day and time don't matter/
static bool sameMonth(DateTime dateTimeOne, DateTime dateTimeTwo) =>
dateTimeOne.year == dateTimeTwo.year
&& dateTimeOne.month == dateTimeTwo.month;
/// Returns number of months between [startDate] and [endDate]
static int monthDelta(DateTime startDate, DateTime endDate) =>
(endDate.year - startDate.year) * 12 +
endDate.month -
startDate.month;
/// Add months to a month truncated date.
static DateTime addMonthsToMonthDate(DateTime monthDate, int monthsToAdd) =>
// year is switched automatically if new month > 12
DateTime(monthDate.year, monthDate.month + monthsToAdd);
/// Returns number of years between [startDate] and [endDate]
static int yearDelta(DateTime startDate, DateTime endDate) =>
endDate.year - startDate.year;
/// Returns start of the first day of the week with given day.
///
/// Start of the week calculated using firstDayIndex which is int from 0 to 6
/// where 0 points to Sunday and 6 points to Saturday.
/// (according to MaterialLocalization.firstDayIfWeekIndex)
static DateTime getFirstDayOfWeek(DateTime day, int firstDayIndex) {
// from 1 to 7 where 1 points to Monday and 7 points to Sunday
int weekday = day.weekday;
// to match weekdays where Sunday is 7 not 0
if (firstDayIndex == 0) firstDayIndex = 7;
int diff = weekday - firstDayIndex;
if (diff < 0) diff = 7 + diff;
DateTime firstDayOfWeek = day.subtract(Duration(days: diff));
firstDayOfWeek = startOfTheDay(firstDayOfWeek);
return firstDayOfWeek;
}
/// Returns end of the last day of the week with given day.
///
/// Start of the week calculated using firstDayIndex which is int from 0 to 6
/// where 0 points to Sunday and 6 points to Saturday.
/// (according to MaterialLocalization.firstDayIfWeekIndex)
static DateTime getLastDayOfWeek(DateTime day, int firstDayIndex) {
// from 1 to 7 where 1 points to Monday and 7 points to Sunday
int weekday = day.weekday;
// to match weekdays where Sunday is 7 not 0
if (firstDayIndex == 0) firstDayIndex = 7;
int lastDayIndex = firstDayIndex - 1;
if (lastDayIndex == 0) lastDayIndex = 7;
int diff = lastDayIndex - weekday;
if (diff < 0) diff = 7 + diff;
DateTime lastDayOfWeek = day.add(Duration(days: diff));
lastDayOfWeek = endOfTheDay(lastDayOfWeek);
return lastDayOfWeek;
}
/// Returns end of the given day.
///
/// End time is 1 millisecond before start of the next day.
static DateTime endOfTheDay(DateTime date) =>
DateTime(date.year, date.month, date.day)
.add(const Duration(days: 1))
.subtract(const Duration(milliseconds: 1));
/// Returns start of the given day.
///
/// Start time is 00:00:00.
static DateTime startOfTheDay(DateTime date) =>
DateTime(date.year, date.month, date.day);
/// Computes the offset from the first day of week that the first day of the
/// [month] falls on.
///
/// For example, September 1, 2017 falls on a Friday, which in the calendar
/// localized for United States English appears as:
///
/// ```
/// S M T W T F S
/// _ _ _ _ _ 1 2
/// ```
///
/// The offset for the first day of the months is the number of leading blanks
/// in the calendar, i.e. 5.
///
/// The same date localized for the Russian calendar has a different offset,
/// because the first day of week is Monday rather than Sunday:
///
/// ```
/// M T W T F S S
/// _ _ _ _ 1 2 3
/// ```
///
/// So the offset is 4, rather than 5.
///
/// This code consolidates the following:
///
/// - [DateTime.weekday] provides a 1-based index into days of week, with 1
/// falling on Monday.
/// - MaterialLocalizations.firstDayOfWeekIndex provides a 0-based index
/// into the MaterialLocalizations.narrowWeekdays list.
/// - MaterialLocalizations.narrowWeekdays list provides localized names of
/// days of week, always starting with Sunday and ending with Saturday.
static int computeFirstDayOffset(
int year, int month, int firstDayOfWeekFromSunday) {
// 0-based day of week, with 0 representing Monday.
final int weekdayFromMonday = DateTime(year, month).weekday - 1;
// firstDayOfWeekFromSunday recomputed to be Monday-based
final int firstDayOfWeekFromMonday = (firstDayOfWeekFromSunday - 1) % 7;
// Number of days between the first day of week appearing on the calendar,
// and the day corresponding to the 1-st of the month.
return (weekdayFromMonday - firstDayOfWeekFromMonday) % 7;
}
/// Returns earliest [DateTime] from the list.
///
/// [dates] must not be null.
/// In case it is null, [ArgumentError] will be thrown.
static DateTime getEarliestFromList(List<DateTime> dates) {
ArgumentError.checkNotNull(dates, "dates");
return dates.fold(dates[0], getEarliest);
}
/// Returns earliest [DateTime] from two.
///
/// If two [DateTime]s is the same moment first ([a]) will be return.
static DateTime getEarliest(DateTime a, DateTime b)
=> a.isBefore(b) ? a : b;
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master | mirrored_repositories/flutter-examples/flutter_date_pickers-master/test/day_multi_selectable_test.dart | import 'package:flutter_date_pickers/src/day_type.dart';
import 'package:flutter_date_pickers/src/i_selectable_picker.dart';
import 'package:flutter_test/flutter_test.dart';
import 'date_time_utils.dart';
void main() {
group("DayMultiSelectable test.", () {
test("getDayType() returns correct type for different dates", () {
final now = DateTime.now();
final selectedDates = [
now.subtract(const Duration(days: 1)),
now,
now.add(const Duration(days: 1)),
];
final firstDate = now.subtract(const Duration(days: 10));
final lastDate = now.add(const Duration(days: 10));
final disabledDate = now.subtract(const Duration(days: 5));
// ignore: prefer_function_declarations_over_variables
final selectablePredicate = (DateTime d)
=> !DateTimeUtils.sameDate(d, disabledDate);
final selectableLogic = DayMultiSelectable(
selectedDates, firstDate, lastDate,
selectableDayPredicate: selectablePredicate);
final notSelectedEnabledDateType = selectableLogic.getDayType(firstDate);
expect(notSelectedEnabledDateType, DayType.notSelected);
final disabledDateType = selectableLogic.getDayType(disabledDate);
expect(disabledDateType, DayType.disabled);
for (DateTime d in selectedDates) {
final selectedDateType = selectableLogic.getDayType(d);
expect(selectedDateType, DayType.single,
reason: "Incorrect DayType for the date ${d.toString()}");
}
});
});
} | 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master | mirrored_repositories/flutter-examples/flutter_date_pickers-master/test/range_selectable_test.dart | import 'package:flutter_date_pickers/flutter_date_pickers.dart';
import 'package:flutter_date_pickers/src/day_type.dart';
import 'package:flutter_date_pickers/src/i_selectable_picker.dart';
import 'package:flutter_test/flutter_test.dart';
import 'date_time_utils.dart';
void main() {
group("RangeSelectable test.", () {
test("getDayType() returns correct type for different dates", () {
final now = DateTime.now();
final startPeriod = now.subtract(const Duration(days: 3));
final endPeriod = now.add(const Duration(days: 3));
final selectedPeriod = DatePeriod(startPeriod, endPeriod);
final firstDate = now.subtract(const Duration(days: 10));
final lastDate = now.add(const Duration(days: 10));
final disabledDate = now.subtract(const Duration(days: 5));
// ignore: prefer_function_declarations_over_variables
final selectablePredicate = (DateTime d)
=> !DateTimeUtils.sameDate(d, disabledDate);
final selectableLogic = RangeSelectable(
selectedPeriod, firstDate, lastDate,
selectableDayPredicate: selectablePredicate);
final notSelectedEnabledDateType = selectableLogic.getDayType(firstDate);
expect(notSelectedEnabledDateType, DayType.notSelected);
final disabledDateType = selectableLogic.getDayType(disabledDate);
expect(disabledDateType, DayType.disabled);
final startPeriodDateType = selectableLogic.getDayType(startPeriod);
expect(startPeriodDateType, DayType.start);
final endPeriodDateType = selectableLogic.getDayType(endPeriod);
expect(endPeriodDateType, DayType.end);
final periodDays = endPeriod.difference(startPeriod).inDays;
// Count of the day period which is not start and not end.
final middleDaysCount = periodDays - 2;
final middleDates = List.generate(middleDaysCount,
(i) => startPeriod.add(Duration(days: i + 1)));
for (DateTime date in middleDates) {
final middlePeriodDateType = selectableLogic.getDayType(date);
expect(middlePeriodDateType, DayType.middle,
reason: "Incorrect DayType for the date ${date.toString()} "
"in period ${startPeriod.toString()} - ${endPeriod.toString()}");
}
});
});
} | 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master | mirrored_repositories/flutter-examples/flutter_date_pickers-master/test/day_selectable_test.dart | import 'package:flutter_date_pickers/src/day_type.dart';
import 'package:flutter_date_pickers/src/i_selectable_picker.dart';
import 'package:flutter_test/flutter_test.dart';
import 'date_time_utils.dart';
void main() {
group("DaySelectable test.", () {
test("getDayType() returns correct type for different dates", () {
final selectedDate = DateTime.now();
final firstDate = selectedDate.subtract(const Duration(days: 10));
final lastDate = selectedDate.add(const Duration(days: 10));
final disabledDate = selectedDate.subtract(const Duration(days: 1));
// ignore: prefer_function_declarations_over_variables
final selectablePredicate = (DateTime d)
=> !DateTimeUtils.sameDate(d, disabledDate);
final selectableLogic = DaySelectable(
selectedDate, firstDate, lastDate,
selectableDayPredicate: selectablePredicate);
final selectedDateType = selectableLogic.getDayType(selectedDate);
final notSelectedEnabledDateType = selectableLogic.getDayType(firstDate);
final disabledDateType = selectableLogic.getDayType(disabledDate);
expect(selectedDateType, DayType.single);
expect(notSelectedEnabledDateType, DayType.notSelected);
expect(disabledDateType, DayType.disabled);
});
});
} | 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master | mirrored_repositories/flutter-examples/flutter_date_pickers-master/test/week_selectabl_test.dart | import 'package:flutter_date_pickers/src/day_type.dart';
import 'package:flutter_date_pickers/src/i_selectable_picker.dart';
import 'package:flutter_test/flutter_test.dart';
import 'date_time_utils.dart';
void main() {
group("WeekSelectable test.", () {
test("getDayType() returns correct type for different dates", () {
final selectedDate = DateTime(2020, 10, 5); // Monday
final firstDayOfWeekIndex = 1; // Week starts from Monday
final firstDate = selectedDate.subtract(const Duration(days: 10));
final lastDate = selectedDate.add(const Duration(days: 10));
final disabledDate = selectedDate.subtract(const Duration(days: 5));
// ignore: prefer_function_declarations_over_variables
final selectablePredicate = (DateTime d)
=> !DateTimeUtils.sameDate(d, disabledDate);
final selectableLogic = WeekSelectable(
selectedDate, firstDayOfWeekIndex, firstDate, lastDate,
selectableDayPredicate: selectablePredicate);
final notSelectedEnabledDateType = selectableLogic.getDayType(firstDate);
expect(notSelectedEnabledDateType, DayType.notSelected);
final disabledDateType = selectableLogic.getDayType(disabledDate);
expect(disabledDateType, DayType.disabled);
final weekStart = DateTimeUtils
.getFirstDayOfWeek(selectedDate, firstDayOfWeekIndex);
final weekEnd = DateTimeUtils
.getLastDayOfWeek(selectedDate, firstDayOfWeekIndex);
final startPeriodDateType = selectableLogic.getDayType(weekStart);
expect(startPeriodDateType, DayType.start);
final endPeriodDateType = selectableLogic.getDayType(weekEnd);
expect(endPeriodDateType, DayType.end);
// Count of the week days which is not start and not end (7 - 2).
final middleDaysCount = 5;
final middleDates = List.generate(middleDaysCount,
(i) => weekStart.add(Duration(days: i + 1)));
for (DateTime date in middleDates) {
final middlePeriodDateType = selectableLogic.getDayType(date);
expect(middlePeriodDateType, DayType.middle,
reason: "Incorrect DayType for the date ${date.toString()} "
"in week ${weekStart.toString()} - ${weekEnd.toString()}");
}
});
});
} | 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/example | mirrored_repositories/flutter-examples/flutter_date_pickers-master/example/lib/event.dart | /// Event for the date pickers.
class Event {
/// Event's date.
final DateTime date;
/// Event's title.
final String dis;
///
Event(this.date, this.dis)
: assert(date != null),
assert(dis != null);
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/example | mirrored_repositories/flutter-examples/flutter_date_pickers-master/example/lib/color_picker_dialog.dart | import 'package:flutter/material.dart';
import 'package:flutter_material_color_picker/flutter_material_color_picker.dart';
/// Dialog with some Material colors ([materialColors]) to pick one of them.
class ColorPickerDialog extends StatefulWidget {
/// Initially selected color.
///
/// If pre-selected color is not from [materialColors] [Colors.blue] will be
/// used.
final Color selectedColor;
///
const ColorPickerDialog({
Key? key,
required this.selectedColor
}) : super(key: key);
@override
State<StatefulWidget> createState() => _ColorPickerDialogState();
}
class _ColorPickerDialogState extends State<ColorPickerDialog> {
Color _mainColor = Colors.blue;
@override
void initState() {
super.initState();
bool isSelectedMaterial = materialColors.contains(widget.selectedColor);
if (isSelectedMaterial) {
_mainColor = widget.selectedColor;
}
}
@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
return AlertDialog(
contentPadding: const EdgeInsets.all(6.0),
title: const Text("Color picker"),
content: MaterialColorPicker(
selectedColor: _mainColor,
allowShades: false,
onMainColorChange: _onMainColorChange,
),
actions: [
TextButton(
child: const Text('CANCEL'),
onPressed: () {
Navigator.of(context).pop(null);
},
),
TextButton(
child: const Text('SUBMIT'),
onPressed: () {
Navigator.of(context).pop(_mainColor);
},
),
],
);
}
void _onMainColorChange (Color? newColor) {
if (newColor == null) return;
setState(() {
_mainColor = newColor;
});
}
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/example | mirrored_repositories/flutter-examples/flutter_date_pickers-master/example/lib/color_selector_btn.dart | import 'package:flutter/material.dart';
// default with and height for the button container
const double _kBtnSize = 24.0;
/// Round colored button with title to select some style color.
class ColorSelectorBtn extends StatelessWidget {
/// Title near color button.
final String title;
/// Color of the button.
final Color color;
/// onTap callback.
final VoidCallback showDialogFunction;
/// Size of the circle.
///
/// By default is [_kBtnSize].
final double colorBtnSize;
///
const ColorSelectorBtn(
{Key? key,
required this.title,
required this.color,
required this.showDialogFunction,
this.colorBtnSize = _kBtnSize})
: super(key: key);
@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
return Expanded(
child: Row(
children: <Widget>[
GestureDetector(
onTap: showDialogFunction,
child: Container(
height: colorBtnSize,
width: colorBtnSize,
decoration: BoxDecoration(color: color, shape: BoxShape.circle),
),
),
const SizedBox(
width: 8.0,
),
Expanded(
child: Text(
title,
overflow: TextOverflow.ellipsis,
)),
],
),
);
}
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/example | mirrored_repositories/flutter-examples/flutter_date_pickers-master/example/lib/main.dart | import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'date_pickers_widgets/day_picker_page.dart';
import 'date_pickers_widgets/days_picker_page.dart';
import 'date_pickers_widgets/month_picker_page.dart';
import 'date_pickers_widgets/range_picker_page.dart';
import 'date_pickers_widgets/week_picker_page.dart';
import 'event.dart';
void main() {
runApp(MyApp());
}
///
class MyApp extends StatelessWidget {
@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: GlobalMaterialLocalizations.delegates,
supportedLocales: [
const Locale('en', 'US'), // American English
const Locale('ru', 'RU'), // Russian
const Locale("pt") // Portuguese
],
debugShowCheckedModeBanner: false,
title: 'Date pickers demo',
theme: ThemeData(
primarySwatch: Colors.blueGrey,
),
home: MyHomePage(
title: 'flutter_date_pickers Demo',
),
);
}
}
/// Start page.
class MyHomePage extends StatefulWidget {
/// Page title.
final String title;
///
MyHomePage({
required this.title,
Key? key,
}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
DateTime startOfPeriod = DateTime.now().subtract(Duration(days: 10));
DateTime endOfPeriod = DateTime.now().add(Duration(days: 10));
int _selectedTab = 0;
final List<Widget> datePickers = <Widget>[
DayPickerPage(events: events,),
DaysPickerPage(),
WeekPickerPage(events: events,),
RangePickerPage(events: events,),
MonthPickerPage()
];
@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
widget.title,
style: TextStyle(letterSpacing: 1.15),
),
),
body: datePickers[_selectedTab],
bottomNavigationBar: Theme(
data: Theme.of(context).copyWith(
canvasColor: Colors.blueGrey,
textTheme: Theme.of(context).textTheme.copyWith(
caption: TextStyle(color: Colors.white.withOpacity(0.5)))),
child: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.date_range), label: "Day"),
BottomNavigationBarItem(
icon: Icon(Icons.date_range), label: "Days"),
BottomNavigationBarItem(
icon: Icon(Icons.date_range), label: "Week"),
BottomNavigationBarItem(
icon: Icon(Icons.date_range), label: "Range"),
BottomNavigationBarItem(
icon: Icon(Icons.date_range), label: "Month"),
],
fixedColor: Colors.yellow,
currentIndex: _selectedTab,
onTap: (newIndex) {
setState(() {
_selectedTab = newIndex;
});
},
),
),
);
}
}
/// Mock events.
final List<Event> events = [
Event(DateTime.now(), "Today event"),
Event(DateTime.now().subtract(Duration(days: 3)), "Ev1"),
Event(DateTime.now().subtract(Duration(days: 13)), "Ev2"),
Event(DateTime.now().subtract(Duration(days: 30)), "Ev3"),
Event(DateTime.now().add(Duration(days: 3)), "Ev4"),
Event(DateTime.now().add(Duration(days: 13)), "Ev5"),
Event(DateTime.now().add(Duration(days: 30)), "Ev6"),
]; | 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/example/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/example/lib/date_pickers_widgets/day_picker_page.dart | import 'package:flutter/material.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart' as dp;
import 'package:flutter_date_pickers/flutter_date_pickers.dart';
import '../color_picker_dialog.dart';
import '../color_selector_btn.dart';
import '../event.dart';
/// Page with [dp.DayPicker].
class DayPickerPage extends StatefulWidget {
/// Custom events.
final List<Event> events;
///
const DayPickerPage({
Key? key,
this.events = const []
}) : super(key: key);
@override
State<StatefulWidget> createState() => _DayPickerPageState();
}
class _DayPickerPageState extends State<DayPickerPage> {
DateTime _selectedDate = DateTime.now();
DateTime _firstDate = DateTime.now().subtract(Duration(days: 45));
DateTime _lastDate = DateTime.now().add(Duration(days: 45));
Color selectedDateStyleColor = Colors.blue;
Color selectedSingleDateDecorationColor = Colors.red;
@override
void didChangeDependencies() {
super.didChangeDependencies();
Color? bodyTextColor = Theme.of(context).accentTextTheme.bodyText1?.color;
if (bodyTextColor != null) selectedDateStyleColor = bodyTextColor;
selectedSingleDateDecorationColor = Theme.of(context).accentColor;
}
@override
Widget build(BuildContext context) {
// add selected colors to default settings
dp.DatePickerRangeStyles styles = dp.DatePickerRangeStyles(
selectedDateStyle: Theme.of(context)
.accentTextTheme
.bodyText1
?.copyWith(color: selectedDateStyleColor),
selectedSingleDateDecoration: BoxDecoration(
color: selectedSingleDateDecorationColor,
shape: BoxShape.circle
),
dayHeaderStyle: DayHeaderStyle(
textStyle: TextStyle(
color: Colors.red
)
)
);
return Flex(
direction: MediaQuery.of(context).orientation == Orientation.portrait
? Axis.vertical
: Axis.horizontal,
children: <Widget>[
Expanded(
child: dp.DayPicker.single(
selectedDate: _selectedDate,
onChanged: _onSelectedDateChanged,
firstDate: _firstDate,
lastDate: _lastDate,
datePickerStyles: styles,
datePickerLayoutSettings: dp.DatePickerLayoutSettings(
maxDayPickerRowCount: 2,
showPrevMonthEnd: true,
showNextMonthStart: true
),
selectableDayPredicate: _isSelectableCustom,
eventDecorationBuilder: _eventDecorationBuilder,
),
),
Container(
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 12.0, vertical: 12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Selected date styles",
style: Theme.of(context).textTheme.subtitle1,
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 12.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ColorSelectorBtn(
title: "Text",
color: selectedDateStyleColor,
showDialogFunction: _showSelectedDateDialog,
colorBtnSize: 42.0,
),
SizedBox(
width: 12.0,
),
ColorSelectorBtn(
title: "Background",
color: selectedSingleDateDecorationColor,
showDialogFunction: _showSelectedBackgroundColorDialog,
colorBtnSize: 42.0,
),
],
),
),
Text("Selected: $_selectedDate")
],
),
),
),
],
);
}
// select text color of the selected date
void _showSelectedDateDialog() async {
Color? newSelectedColor = await showDialog(
context: context,
builder: (_) => ColorPickerDialog(
selectedColor: selectedDateStyleColor,
));
if (newSelectedColor != null) {
setState(() {
selectedDateStyleColor = newSelectedColor;
});
}
}
// select background color of the selected date
void _showSelectedBackgroundColorDialog() async {
Color? newSelectedColor = await showDialog(
context: context,
builder: (_) => ColorPickerDialog(
selectedColor: selectedSingleDateDecorationColor,
));
if (newSelectedColor != null) {
setState(() {
selectedSingleDateDecorationColor = newSelectedColor;
});
}
}
void _onSelectedDateChanged(DateTime newDate) {
setState(() {
_selectedDate = newDate;
});
}
// ignore: prefer_expression_function_bodies
bool _isSelectableCustom (DateTime day) {
return day.weekday < 6;
}
dp.EventDecoration? _eventDecorationBuilder(DateTime date) {
List<DateTime> eventsDates = widget.events
.map<DateTime>((Event e) => e.date)
.toList();
bool isEventDate = eventsDates.any((DateTime d) =>
date.year == d.year
&& date.month == d.month
&& d.day == date.day);
BoxDecoration roundedBorder = BoxDecoration(
border: Border.all(
color: Colors.deepOrange,
),
borderRadius: BorderRadius.all(Radius.circular(3.0))
);
return isEventDate
? dp.EventDecoration(boxDecoration: roundedBorder)
: null;
}
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/example/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/example/lib/date_pickers_widgets/days_picker_page.dart | import 'package:flutter/material.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart' as dp;
import '../color_picker_dialog.dart';
import '../color_selector_btn.dart';
import '../event.dart';
/// Page with [dp.DayPicker] where many single days can be selected.
class DaysPickerPage extends StatefulWidget {
/// Custom events.
final List<Event> events;
///
const DaysPickerPage({
Key? key,
this.events = const []
}) : super(key: key);
@override
State<StatefulWidget> createState() => _DaysPickerPageState();
}
class _DaysPickerPageState extends State<DaysPickerPage> {
List<DateTime> _selectedDates = [];
DateTime _firstDate = DateTime.now().subtract(Duration(days: 45));
DateTime _lastDate = DateTime.now().add(Duration(days: 45));
Color selectedDateStyleColor = Colors.blue;
Color selectedSingleDateDecorationColor = Colors.red;
@override
void initState() {
super.initState();
final now = DateTime.now();
_selectedDates = [
now,
now.subtract(Duration(days: 10)),
now.add(Duration(days: 7))
];
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
Color? bodyTextColor = Theme.of(context).accentTextTheme.bodyText1?.color;
if (bodyTextColor != null) selectedDateStyleColor = bodyTextColor;
selectedSingleDateDecorationColor = Theme.of(context).accentColor;
}
@override
Widget build(BuildContext context) {
// add selected colors to default settings
dp.DatePickerRangeStyles styles = dp.DatePickerRangeStyles(
selectedDateStyle: Theme.of(context)
.accentTextTheme
.bodyText1
?.copyWith(color: selectedDateStyleColor),
selectedSingleDateDecoration: BoxDecoration(
color: selectedSingleDateDecorationColor, shape: BoxShape.circle));
return Flex(
direction: MediaQuery.of(context).orientation == Orientation.portrait
? Axis.vertical
: Axis.horizontal,
children: <Widget>[
Expanded(
child: dp.DayPicker.multi(
selectedDates: _selectedDates,
onChanged: _onSelectedDateChanged,
firstDate: _firstDate,
lastDate: _lastDate,
datePickerStyles: styles,
datePickerLayoutSettings: dp.DatePickerLayoutSettings(
maxDayPickerRowCount: 2,
showPrevMonthEnd: true,
showNextMonthStart: true
),
selectableDayPredicate: _isSelectableCustom,
eventDecorationBuilder: _eventDecorationBuilder,
),
),
Container(
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 12.0, vertical: 12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Selected date styles",
style: Theme.of(context).textTheme.subtitle1,
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 12.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ColorSelectorBtn(
title: "Text",
color: selectedDateStyleColor,
showDialogFunction: _showSelectedDateDialog,
colorBtnSize: 42.0,
),
SizedBox(
width: 12.0,
),
ColorSelectorBtn(
title: "Background",
color: selectedSingleDateDecorationColor,
showDialogFunction: _showSelectedBackgroundColorDialog,
colorBtnSize: 42.0,
),
],
),
),
Text("Selected: $_selectedDates")
],
),
),
),
],
);
}
// select text color of the selected date
void _showSelectedDateDialog() async {
Color? newSelectedColor = await showDialog(
context: context,
builder: (_) => ColorPickerDialog(
selectedColor: selectedDateStyleColor,
));
if (newSelectedColor != null) {
setState(() {
selectedDateStyleColor = newSelectedColor;
});
}
}
// select background color of the selected date
void _showSelectedBackgroundColorDialog() async {
Color? newSelectedColor = await showDialog(
context: context,
builder: (_) => ColorPickerDialog(
selectedColor: selectedSingleDateDecorationColor,
));
if (newSelectedColor != null) {
setState(() {
selectedSingleDateDecorationColor = newSelectedColor;
});
}
}
void _onSelectedDateChanged(List<DateTime> newDates) {
setState(() {
_selectedDates = newDates;
});
}
// ignore: prefer_expression_function_bodies
bool _isSelectableCustom (DateTime day) {
return day.weekday < 6;
}
dp.EventDecoration? _eventDecorationBuilder(DateTime date) {
List<DateTime> eventsDates = widget.events
.map<DateTime>((Event e) => e.date)
.toList();
bool isEventDate = eventsDates.any((DateTime d) =>
date.year == d.year
&& date.month == d.month
&& d.day == date.day);
BoxDecoration roundedBorder = BoxDecoration(
border: Border.all(
color: Colors.deepOrange,
),
borderRadius: BorderRadius.all(Radius.circular(3.0))
);
return isEventDate
? dp.EventDecoration(boxDecoration: roundedBorder)
: null;
}
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/example/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/example/lib/date_pickers_widgets/month_picker_page.dart | import 'package:flutter/material.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart' as dp;
import '../color_picker_dialog.dart';
import '../color_selector_btn.dart';
/// Page with the [dp.MonthPicker].
class MonthPickerPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => _MonthPickerPageState();
}
class _MonthPickerPageState extends State<MonthPickerPage> {
DateTime _firstDate = DateTime.now().subtract(Duration(days: 350));
DateTime _lastDate = DateTime.now().add(Duration(days: 350));
DateTime _selectedDate = DateTime.now();
Color selectedDateStyleColor = Colors.blue;
Color selectedSingleDateDecorationColor = Colors.red;
@override
void didChangeDependencies() {
super.didChangeDependencies();
Color? bodyTextColor = Theme.of(context).accentTextTheme.bodyText1?.color;
if (bodyTextColor != null) selectedDateStyleColor = bodyTextColor;
selectedSingleDateDecorationColor = Theme.of(context).accentColor;
}
@override
Widget build(BuildContext context) {
// add selected colors to default settings
dp.DatePickerStyles styles = dp.DatePickerStyles(
selectedDateStyle: Theme.of(context)
.accentTextTheme
.bodyText1
?.copyWith(color: selectedDateStyleColor),
selectedSingleDateDecoration: BoxDecoration(
color: selectedSingleDateDecorationColor, shape: BoxShape.circle));
return Flex(
direction: MediaQuery.of(context).orientation == Orientation.portrait
? Axis.vertical
: Axis.horizontal,
children: <Widget>[
Expanded(
child: dp.MonthPicker(
selectedDate: _selectedDate,
onChanged: _onSelectedDateChanged,
firstDate: _firstDate,
lastDate: _lastDate,
datePickerStyles: styles,
),
),
Container(
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 12.0, vertical: 12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Selected date styles",
style: Theme.of(context).textTheme.subtitle1,
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 12.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ColorSelectorBtn(
title: "Text",
color: selectedDateStyleColor,
showDialogFunction: _showSelectedDateDialog,
colorBtnSize: 42.0,
),
SizedBox(
width: 12.0,
),
ColorSelectorBtn(
title: "Background",
color: selectedSingleDateDecorationColor,
showDialogFunction: _showSelectedBackgroundColorDialog,
colorBtnSize: 42.0,
),
],
),
),
Text("Selected: $_selectedDate")
],
),
),
),
],
);
}
// select text color of the selected date
void _showSelectedDateDialog() async {
Color? newSelectedColor = await showDialog(
context: context,
builder: (_) => ColorPickerDialog(
selectedColor: selectedDateStyleColor,
));
if (newSelectedColor != null) {
setState(() {
selectedDateStyleColor = newSelectedColor;
});
}
}
// select background color of the selected date
void _showSelectedBackgroundColorDialog() async {
Color? newSelectedColor = await showDialog(
context: context,
builder: (_) => ColorPickerDialog(
selectedColor: selectedSingleDateDecorationColor,
));
if (newSelectedColor != null) {
setState(() {
selectedSingleDateDecorationColor = newSelectedColor;
});
}
}
void _onSelectedDateChanged(DateTime newDate) {
setState(() {
_selectedDate = newDate;
});
}
}
| 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/example/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/example/lib/date_pickers_widgets/range_picker_styled.dart | import 'package:flutter/material.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart';
import '../event.dart';
/// Page with the [RangePicker] styled according to issue:
/// https://github.com/MariaMelnik/flutter_date_pickers/issues/49
class RangePickerPageStyled extends StatefulWidget {
/// Custom events.
final List<Event> events;
///
const RangePickerPageStyled({
Key? key,
this.events = const []
}) : super(key: key);
@override
State<StatefulWidget> createState() => _RangePickerPageStyledState();
}
class _RangePickerPageStyledState extends State<RangePickerPageStyled> {
DateTime _firstDate = DateTime.now().subtract(Duration(days: 345));
DateTime _lastDate = DateTime.now().add(Duration(days: 345));
DatePeriod _selectedPeriod = DatePeriod(
DateTime.now().subtract(Duration(days: 4)),
DateTime.now().add(Duration(days: 8))
);
Color selectedPeriodStartColor = Colors.blue;
Color selectedPeriodLastColor = Colors.blue;
Color selectedPeriodMiddleColor = Colors.blue;
@override
void initState() {
super.initState();
DateTime selectedPeriodStart = DateTime.now().subtract(Duration(days: 4));
DateTime selectedPeriodEnd = DateTime.now().add(Duration(days: 8));
_selectedPeriod = DatePeriod(selectedPeriodStart, selectedPeriodEnd);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
// defaults for styles
selectedPeriodLastColor = Theme.of(context).accentColor;
selectedPeriodMiddleColor = Theme.of(context).accentColor;
selectedPeriodStartColor = Theme.of(context).accentColor;
}
@override
Widget build(BuildContext context) {
Color middleBgColor = Color.fromRGBO(237, 237, 250, 1);
DecorationImage circleImg = DecorationImage(
image: AssetImage('images/bg.png'),
fit: BoxFit.contain
);
// add selected colors to default settings
DatePickerRangeStyles styles = DatePickerRangeStyles(
selectedPeriodLastDecoration: BoxDecoration(
color: middleBgColor,
gradient: LinearGradient(
colors: [middleBgColor, Colors.transparent],
stops: [0.5, 0.5]
),
image: circleImg,
borderRadius: const BorderRadius.only(
topRight: Radius.circular(24.0),
bottomRight: Radius.circular(24.0))
),
selectedPeriodStartDecoration: BoxDecoration(
color: middleBgColor,
gradient: LinearGradient(
colors: [Colors.transparent, middleBgColor,],
stops: [0.5, 0.5]
),
image: circleImg,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(24.0),
bottomLeft: Radius.circular(24.0)
),
),
selectedPeriodMiddleDecoration: BoxDecoration(
color: middleBgColor,
shape: BoxShape.rectangle
),
);
return Flex(
direction: MediaQuery.of(context).orientation == Orientation.portrait
? Axis.vertical
: Axis.horizontal,
children: <Widget>[
Expanded(
child: RangePicker(
selectedPeriod: _selectedPeriod,
onChanged: _onSelectedDateChanged,
firstDate: _firstDate,
lastDate: _lastDate,
datePickerStyles: styles,
eventDecorationBuilder: _eventDecorationBuilder,
selectableDayPredicate: _isSelectableCustom,
onSelectionError: _onSelectionError,
datePickerLayoutSettings: DatePickerLayoutSettings(
showNextMonthStart: true,
showPrevMonthEnd: true
),
),
),
Container(
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 12.0, vertical: 12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Selected date styles",
style: Theme.of(context).textTheme.subtitle1,
),
_selectedBlock()
],
),
),
),
],
);
}
// Block with show information about selected date
// and boundaries of the selected period.
Widget _selectedBlock() => Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
_selectedPeriod != null
? Column(children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8.0, bottom: 4.0),
child: Text("Selected period boundaries:"),
),
Text(_selectedPeriod.start.toString()),
Text(_selectedPeriod.end.toString()),
])
: Container()
],
);
void _onSelectedDateChanged(DatePeriod newPeriod) {
setState(() {
_selectedPeriod = newPeriod;
});
}
EventDecoration? _eventDecorationBuilder(DateTime date) {
List<DateTime> eventsDates = widget.events
.map<DateTime>((Event e) => e.date)
.toList();
bool isEventDate = eventsDates.any((DateTime d) =>
date.year == d.year
&& date.month == d.month
&& d.day == date.day);
BoxDecoration roundedBorder = BoxDecoration(
border: Border.all(
color: Colors.green,
),
borderRadius: BorderRadius.all(Radius.circular(3.0))
);
return isEventDate
? EventDecoration(boxDecoration: roundedBorder)
: null;
}
// ignore: prefer_expression_function_bodies
bool _isSelectableCustom (DateTime day) {
DateTime now = DateTime.now();
DateTime yesterday = now.subtract(Duration(days: 1));
DateTime tomorrow = now.add(Duration(days: 1));
bool isYesterday = _sameDate(day, yesterday);
bool isTomorrow = _sameDate(day, tomorrow);
return !isYesterday && !isTomorrow;
// return true;
// return day.weekday < 6;
// return day.day != DateTime.now().add(Duration(days: 7)).day ;
}
void _onSelectionError(UnselectablePeriodException exception) {
DatePeriod errorPeriod = exception.period;
// If user supposed to set another start of the period.
bool selectStart = _selectedPeriod.start != errorPeriod.start;
DateTime newSelection = selectStart
? errorPeriod.start
: errorPeriod.end;
DatePeriod newPeriod = DatePeriod(newSelection, newSelection);
setState(() {
_selectedPeriod = newPeriod;
});
}
// 0 is Sunday, 6 is Saturday
DayHeaderStyle _dayHeaderStyleBuilder(int weekday) {
bool isWeekend = weekday == 0 || weekday == 6;
return DayHeaderStyle(
textStyle: TextStyle(
color: isWeekend ? Colors.red : Colors.teal
)
);
}
}
bool _sameDate(DateTime first, DateTime second) =>
first.year == second.year
&& first.month == second.month
&& first.day == second.day; | 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/example/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/example/lib/date_pickers_widgets/range_picker_page.dart | import 'package:flutter/material.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart';
import '../color_picker_dialog.dart';
import '../color_selector_btn.dart';
import '../event.dart';
/// Page with the [RangePicker].
class RangePickerPage extends StatefulWidget {
/// Custom events.
final List<Event> events;
///
const RangePickerPage({
Key? key,
this.events = const []
}) : super(key: key);
@override
State<StatefulWidget> createState() => _RangePickerPageState();
}
class _RangePickerPageState extends State<RangePickerPage> {
DateTime _firstDate = DateTime.now().subtract(Duration(days: 345));
DateTime _lastDate = DateTime.now().add(Duration(days: 345));
DatePeriod _selectedPeriod = DatePeriod(
DateTime.now().subtract(Duration(days: 30)),
DateTime.now().subtract(Duration(days: 12))
);
Color selectedPeriodStartColor = Colors.blue;
Color selectedPeriodLastColor = Colors.blue;
Color selectedPeriodMiddleColor = Colors.blue;
@override
void didChangeDependencies() {
super.didChangeDependencies();
selectedPeriodLastColor = Theme.of(context).accentColor;
selectedPeriodMiddleColor = Theme.of(context).accentColor;
selectedPeriodStartColor = Theme.of(context).accentColor;
}
@override
Widget build(BuildContext context) {
// add selected colors to default settings
DatePickerRangeStyles styles = DatePickerRangeStyles(
selectedPeriodLastDecoration: BoxDecoration(
color: selectedPeriodLastColor,
borderRadius: const BorderRadius.only(
topRight: Radius.circular(24.0),
bottomRight: Radius.circular(24.0))),
selectedPeriodStartDecoration: BoxDecoration(
color: selectedPeriodStartColor,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(24.0),
bottomLeft: Radius.circular(24.0)
),
),
selectedPeriodMiddleDecoration: BoxDecoration(
color: selectedPeriodMiddleColor, shape: BoxShape.rectangle),
nextIcon: const Icon(Icons.arrow_right),
prevIcon: const Icon(Icons.arrow_left),
dayHeaderStyleBuilder: _dayHeaderStyleBuilder
);
return Flex(
direction: MediaQuery.of(context).orientation == Orientation.portrait
? Axis.vertical
: Axis.horizontal,
children: <Widget>[
Expanded(
child: RangePicker(
initiallyShowDate: DateTime.now(),
selectedPeriod: _selectedPeriod,
onChanged: _onSelectedDateChanged,
firstDate: _firstDate,
lastDate: _lastDate,
datePickerStyles: styles,
eventDecorationBuilder: _eventDecorationBuilder,
selectableDayPredicate: _isSelectableCustom,
onSelectionError: _onSelectionError,
),
),
Container(
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 12.0, vertical: 12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Selected date styles",
style: Theme.of(context).textTheme.subtitle1,
),
_stylesBlock(),
_selectedBlock()
],
),
),
),
],
);
}
// Block with show information about selected date
// and boundaries of the selected period.
Widget _selectedBlock() => Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
_selectedPeriod != null
? Column(children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8.0, bottom: 4.0),
child: Text("Selected period boundaries:"),
),
Text(_selectedPeriod.start.toString()),
Text(_selectedPeriod.end.toString()),
])
: Container()
],
);
// block with color buttons inside
Widget _stylesBlock() => Padding(
padding: const EdgeInsets.symmetric(vertical: 12.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ColorSelectorBtn(
title: "Start",
color: selectedPeriodStartColor,
showDialogFunction: _showSelectedStartColorDialog),
SizedBox(
width: 12.0,
),
ColorSelectorBtn(
title: "Middle",
color: selectedPeriodMiddleColor,
showDialogFunction: _showSelectedMiddleColorDialog),
SizedBox(
width: 12.0,
),
ColorSelectorBtn(
title: "End",
color: selectedPeriodLastColor,
showDialogFunction: _showSelectedEndColorDialog),
],
),
);
// select background color for the first date of the selected period
void _showSelectedStartColorDialog() async {
Color? newSelectedColor = await showDialog(
context: context,
builder: (_) => ColorPickerDialog(
selectedColor: selectedPeriodStartColor,
));
if (newSelectedColor != null) {
setState(() {
selectedPeriodStartColor = newSelectedColor;
});
}
}
// select background color for the last date of the selected period
void _showSelectedEndColorDialog() async {
Color? newSelectedColor = await showDialog(
context: context,
builder: (_) => ColorPickerDialog(
selectedColor: selectedPeriodLastColor,
));
if (newSelectedColor != null) {
setState(() {
selectedPeriodLastColor = newSelectedColor;
});
}
}
// select background color for the middle dates of the selected period
void _showSelectedMiddleColorDialog() async {
Color? newSelectedColor = await showDialog(
context: context,
builder: (_) => ColorPickerDialog(
selectedColor: selectedPeriodMiddleColor,
));
if (newSelectedColor != null) {
setState(() {
selectedPeriodMiddleColor = newSelectedColor;
});
}
}
void _onSelectedDateChanged(DatePeriod newPeriod) {
setState(() {
_selectedPeriod = newPeriod;
});
}
EventDecoration? _eventDecorationBuilder(DateTime date) {
List<DateTime> eventsDates = widget.events
.map<DateTime>((Event e) => e.date)
.toList();
bool isEventDate = eventsDates.any((DateTime d) =>
date.year == d.year
&& date.month == d.month
&& d.day == date.day);
BoxDecoration roundedBorder = BoxDecoration(
border: Border.all(
color: Colors.green,
),
borderRadius: BorderRadius.all(Radius.circular(3.0))
);
return isEventDate
? EventDecoration(boxDecoration: roundedBorder)
: null;
}
// ignore: prefer_expression_function_bodies
bool _isSelectableCustom (DateTime day) {
DateTime now = DateTime.now();
DateTime yesterday = now.subtract(Duration(days: 1));
DateTime tomorrow = now.add(Duration(days: 1));
bool isYesterday = sameDate(day, yesterday);
bool isTomorrow = sameDate(day, tomorrow);
return !isYesterday && !isTomorrow;
// return true;
// return day.weekday < 6;
// return day.day != DateTime.now().add(Duration(days: 7)).day ;
}
void _onSelectionError(UnselectablePeriodException exception) {
DatePeriod errorPeriod = exception.period;
// If user supposed to set another start of the period.
bool selectStart = _selectedPeriod.start != errorPeriod.start;
DateTime newSelection = selectStart
? errorPeriod.start
: errorPeriod.end;
DatePeriod newPeriod = DatePeriod(newSelection, newSelection);
setState(() {
_selectedPeriod = newPeriod;
});
}
// 0 is Sunday, 6 is Saturday
DayHeaderStyle _dayHeaderStyleBuilder(int weekday) {
bool isWeekend = weekday == 0 || weekday == 6;
return DayHeaderStyle(
textStyle: TextStyle(
color: isWeekend ? Colors.red : Colors.teal
)
);
}
}
bool sameDate(DateTime first, DateTime second) {
return first.year == second.year && first.month == second.month && first.day == second.day;
} | 0 |
mirrored_repositories/flutter-examples/flutter_date_pickers-master/example/lib | mirrored_repositories/flutter-examples/flutter_date_pickers-master/example/lib/date_pickers_widgets/week_picker_page.dart | import 'package:flutter/material.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart';
import '../color_picker_dialog.dart';
import '../color_selector_btn.dart';
import '../event.dart';
/// Page with the [WeekPicker].
class WeekPickerPage extends StatefulWidget {
/// Custom events.
final List<Event> events;
///
const WeekPickerPage({
Key? key,
this.events = const []
}) : super(key: key);
@override
State<StatefulWidget> createState() => _WeekPickerPageState();
}
class _WeekPickerPageState extends State<WeekPickerPage> {
DateTime _selectedDate = DateTime.now();
DateTime _firstDate = DateTime.now().subtract(Duration(days: 45));
DateTime _lastDate = DateTime.now().add(Duration(days: 45));
DatePeriod? _selectedPeriod;
Color selectedPeriodStartColor = Colors.blue;
Color selectedPeriodLastColor = Colors.blue;
Color selectedPeriodMiddleColor = Colors.blue;
@override
void didChangeDependencies() {
super.didChangeDependencies();
// defaults for styles
selectedPeriodLastColor = Theme.of(context).accentColor;
selectedPeriodMiddleColor = Theme.of(context).accentColor;
selectedPeriodStartColor = Theme.of(context).accentColor;
}
@override
Widget build(BuildContext context) {
// add selected colors to default settings
DatePickerRangeStyles styles = DatePickerRangeStyles(
selectedPeriodLastDecoration: BoxDecoration(
color: selectedPeriodLastColor,
borderRadius: BorderRadius.only(
topRight: Radius.circular(10.0),
bottomRight: Radius.circular(10.0))),
selectedPeriodStartDecoration: BoxDecoration(
color: selectedPeriodStartColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10.0), bottomLeft: Radius.circular(10.0)),
),
selectedPeriodMiddleDecoration: BoxDecoration(
color: selectedPeriodMiddleColor, shape: BoxShape.rectangle),
);
return Flex(
direction: MediaQuery.of(context).orientation == Orientation.portrait
? Axis.vertical
: Axis.horizontal,
children: <Widget>[
Expanded(
child: WeekPicker(
selectedDate: _selectedDate,
onChanged: _onSelectedDateChanged,
firstDate: _firstDate,
lastDate: _lastDate,
datePickerStyles: styles,
onSelectionError: _onSelectionError,
selectableDayPredicate: _isSelectableCustom,
eventDecorationBuilder: _eventDecorationBuilder,
),
),
Container(
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 12.0, vertical: 12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Selected date styles",
style: Theme.of(context).textTheme.subtitle1,
),
_stylesBlock(),
_selectedBlock()
],
),
),
),
],
);
}
// block witt color buttons insede
Widget _stylesBlock() => Padding(
padding: const EdgeInsets.symmetric(vertical: 12.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ColorSelectorBtn(
title: "Start",
color: selectedPeriodStartColor,
showDialogFunction: _showSelectedStartColorDialog),
SizedBox(
width: 12.0,
),
ColorSelectorBtn(
title: "Middle",
color: selectedPeriodMiddleColor,
showDialogFunction: _showSelectedMiddleColorDialog),
SizedBox(
width: 12.0,
),
ColorSelectorBtn(
title: "End",
color: selectedPeriodLastColor,
showDialogFunction: _showSelectedEndColorDialog),
],
),
);
// Block with information about selected date
// and boundaries of the selected period.
Widget _selectedBlock() => Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Text("Selected: $_selectedDate"),
),
_selectedPeriod != null
? Column(children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8.0, bottom: 4.0),
child: Text("Selected period boundaries:"),
),
Text(_selectedPeriod!.start.toString()),
Text(_selectedPeriod!.end.toString()),
])
: Container()
],
);
// select background color for the first date of the selected period
void _showSelectedStartColorDialog() async {
Color? newSelectedColor = await showDialog(
context: context,
builder: (_) => ColorPickerDialog(
selectedColor: selectedPeriodStartColor,
));
if (newSelectedColor != null) {
setState(() {
selectedPeriodStartColor = newSelectedColor;
});
}
}
// select background color for the last date of the selected period
void _showSelectedEndColorDialog() async {
Color? newSelectedColor = await showDialog(
context: context,
builder: (_) => ColorPickerDialog(
selectedColor: selectedPeriodLastColor,
));
if (newSelectedColor != null) {
setState(() {
selectedPeriodLastColor = newSelectedColor;
});
}
}
// select background color for the middle dates of the selected period
void _showSelectedMiddleColorDialog() async {
Color newSelectedColor = await showDialog(
context: context,
builder: (_) => ColorPickerDialog(
selectedColor: selectedPeriodMiddleColor,
));
if (newSelectedColor != null) {
setState(() {
selectedPeriodMiddleColor = newSelectedColor;
});
}
}
void _onSelectedDateChanged(DatePeriod newPeriod) {
setState(() {
_selectedDate = newPeriod.start;
_selectedPeriod = newPeriod;
});
}
void _onSelectionError(Object e){
if (e is UnselectablePeriodException) print("catch error: $e");
}
// ignore: prefer_expression_function_bodies
bool _isSelectableCustom (DateTime day) {
// return day.weekday < 6;
return day.day != DateTime.now().add(Duration(days: 7)).day ;
}
EventDecoration? _eventDecorationBuilder(DateTime date) {
List<DateTime> eventsDates = widget.events
.map<DateTime>((Event e) => e.date)
.toList();
bool isEventDate = eventsDates.any((DateTime d) => date.year == d.year
&& date.month == d.month
&& d.day == date.day);
if (!isEventDate) return null;
BoxDecoration roundedBorder = BoxDecoration(
color: Colors.blue,
border: Border.all(
color: Colors.blue,
),
borderRadius: BorderRadius.all(Radius.circular(3.0))
);
TextStyle? whiteText = Theme.of(context)
.textTheme
.bodyText2
?.copyWith(color: Colors.white);
return isEventDate
? EventDecoration(boxDecoration: roundedBorder, textStyle: whiteText)
: null;
}
}
| 0 |
mirrored_repositories/flutter-examples/animation_example | mirrored_repositories/flutter-examples/animation_example/lib/home_page.dart | import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage>
with SingleTickerProviderStateMixin {
late AnimationController controller;
var target = 0.0;
final Map<double, String> data = {
0.25: "north",
0.50: "west",
0.75: "south",
1.00: 'east'
};
@override
void initState() {
super.initState();
controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 900),
);
}
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
showResult(double target) {
final snak = SnackBar(
content: Text(
"Pen is pointed to ${data[target]}",
style: const TextStyle(
fontSize: 20, fontWeight: FontWeight.w500, color: Colors.green),
),
duration: const Duration(seconds: 2),
);
ScaffoldMessenger.of(context).showSnackBar(snak);
}
return Scaffold(
backgroundColor: Colors.white,
body: Container(
width: double.infinity,
height: size.height,
child: Stack(
alignment: Alignment.center,
children: [
Positioned(
top: 80,
child: TextButton(
onPressed: () {},
child: const Text(
"North",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w500,
color: Colors.white),
),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.green)))),
Positioned(
top: size.height * 0.48,
left: -15,
child: Transform.rotate(
angle: 190.1,
child: TextButton(
onPressed: () {},
child: const Text(
"East",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w500,
color: Colors.white),
),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.green))),
)),
Positioned(
top: size.height * 0.48,
right: -15,
child: Transform.rotate(
angle: -190.1,
child: TextButton(
onPressed: () {},
child: const Text(
"West",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w500,
color: Colors.white),
),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.green))),
)),
Positioned(
bottom: 80,
child: TextButton(
onPressed: () {},
child: const Text(
"North",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w500,
color: Colors.white),
),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.green)))),
AnimatedBuilder(
animation: controller,
child: Container(
child: GestureDetector(
onTap: () {
setState(() {
controller.repeat();
});
Future.delayed(Duration(seconds: 10), () {
setState(() {
target += 0.25;
controller.animateTo(target,
duration: Duration(seconds: 1));
showResult(target);
if (target == 1.00) {
target = 0.0;
}
});
});
},
child: Image.asset(
'assets/images/penimg2.png',
width: size.width * 0.5,
)),
),
builder: (context, Widget? widget) {
return Transform.rotate(
angle: controller.value * 2 * 3.14,
child: widget,
);
},
),
],
),
),
);
}
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.